admin: new route to check file on disk

This commit is contained in:
nemunaire 2018-08-19 22:19:49 +02:00 committed by Pierre-Olivier Mercier
parent 5b30788cff
commit 20dfd99ec0
4 changed files with 27 additions and 6 deletions

View File

@ -15,6 +15,8 @@ func init() {
router.GET("/api/files/:fileid", apiHandler(fileHandler(showFile))) router.GET("/api/files/:fileid", apiHandler(fileHandler(showFile)))
router.PUT("/api/files/:fileid", apiHandler(fileHandler(updateFile))) router.PUT("/api/files/:fileid", apiHandler(fileHandler(updateFile)))
router.DELETE("/api/files/:fileid", apiHandler(fileHandler(deleteFile))) router.DELETE("/api/files/:fileid", apiHandler(fileHandler(deleteFile)))
router.POST("/api/files/:fileid/check", apiHandler(fileHandler(checkFile)))
} }
func listFiles(_ httprouter.Params, body []byte) (interface{}, error) { func listFiles(_ httprouter.Params, body []byte) (interface{}, error) {
@ -48,3 +50,7 @@ func updateFile(file fic.EFile, body []byte) (interface{}, error) {
func deleteFile(file fic.EFile, _ []byte) (interface{}, error) { func deleteFile(file fic.EFile, _ []byte) (interface{}, error) {
return file.Delete() return file.Delete()
} }
func checkFile(file fic.EFile, _ []byte) (interface{}, error) {
return true, file.CheckFileOnDisk()
}

View File

@ -695,6 +695,7 @@ angular.module("FICApp")
.controller("FilesListController", function($scope, File, $location, $http, $rootScope) { .controller("FilesListController", function($scope, File, $location, $http, $rootScope) {
$scope.files = File.query(); $scope.files = File.query();
$scope.errfnd = 0;
$scope.fields = ["id", "path", "name", "checksum", "size"]; $scope.fields = ["id", "path", "name", "checksum", "size"];
$scope.clearFiles = function(id) { $scope.clearFiles = function(id) {
@ -704,17 +705,18 @@ angular.module("FICApp")
}; };
$scope.checksum = function(f) { $scope.checksum = function(f) {
$http({ $http({
url: "/api/files/" + f.id + "/checksum", url: "/api/files/" + f.id + "/check",
method: "GET" method: "POST"
}).then(function(response) { }).then(function(response) {
if (response.data) f.err = true;
$rootScope.newBox('danger', response.data);
}, function(response) { }, function(response) {
$scope.inSync = false; $scope.inSync = false;
$rootScope.newBox('danger', 'An error occurs when checking files:', response.data); $scope.errfnd += 1;
f.err = response.data.errmsg;
}) })
}; };
$scope.checksumAll = function() { $scope.checksumAll = function() {
$scope.errfnd = 0;
angular.forEach($scope.files, function(file) { angular.forEach($scope.files, function(file) {
$scope.checksum(file); $scope.checksum(file);
}); });

View File

@ -1,5 +1,6 @@
<h2> <h2>
Fichiers Fichiers
<small class="text-muted" ng-if="errfnd > 0"><span class="glyphicon glyphicon-exclamation-sign"></span> <ng-pluralize count="errfnd" when="{'one': '{} erreur trouvée', 'other': '{} erreurs trouvées'}"></ng-pluralize></small>
<button ng-click="checksumAll()" class="float-right btn btn-sm btn-secondary"><span class="glyphicon glyphicon-flash" aria-hidden="true"></span> Vérifier les fichiers</button> <button ng-click="checksumAll()" class="float-right btn btn-sm btn-secondary"><span class="glyphicon glyphicon-flash" aria-hidden="true"></span> Vérifier les fichiers</button>
</h2> </h2>
@ -13,9 +14,10 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr ng-repeat="file in files | filter: query" ng-click="show(file)"> <tr ng-repeat="file in files | filter: query" ng-class="{'bg-danger': file.err !== undefined && file.err !== true}" ng-click="show(file)">
<td ng-repeat="field in fields"> <td ng-repeat="field in fields">
{{ file[field] }} {{ file[field] }}
<span ng-if="field == 'id' && file.err !== undefined && file.err !== true" title="{{ file.err }}" class="glyphicon glyphicon-exclamation-sign"></span>
</td> </td>
</tr> </tr>
</tbody> </tbody>

View File

@ -261,3 +261,14 @@ func ClearFiles() (int64, error) {
func (f EFile) GetOrigin() string { func (f EFile) GetOrigin() string {
return f.origin return f.origin
} }
// CheckFileOnDisk recalculates the hash of the file on disk.
func (f EFile) CheckFileOnDisk() error {
if _, size, err := checkFileHash(path.Join(FilesDir, f.Path), f.Checksum); err != nil {
return err
} else if size == 0 {
return errors.New("Empty file!")
} else {
return nil
}
}