Skip to content

Commit

Permalink
WIP new filemanager for large file amount #137
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar committed May 28, 2018
1 parent 353d0bc commit 2514e2f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 25 deletions.
30 changes: 21 additions & 9 deletions src/apis/StorageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use yii\web\BadRequestHttpException;
use yii\base\InvalidParamException;
use yii\web\NotFoundHttpException;
use yii\data\ActiveDataProvider;
use luya\admin\file\Query;

/**
* Filemanager and Storage API.
Expand Down Expand Up @@ -81,13 +83,16 @@ public function actionDataFolders()
*
* @return array
*/
public function actionDataFiles()
public function actionDataFiles($folderId = 0, $page = 0)
{
$cache = $this->getHasCache('storageApiDataFiles');
$perPage = 50;
$totalCount = (new Query())->where(['folder_id' => $folderId, 'is_hidden' => false, 'is_deleted' => false])->count();

if ($cache === false) {
$files = $this->getOrSetHasCache(['storageApiDataFiles', $folderId, $page], function() use ($folderId, $page, $perPage) {
$files = [];
foreach (Yii::$app->storage->findFiles(['is_hidden' => false, 'is_deleted' => false]) as $file) {
$fileQuery = (new Query())->where(['folder_id' => $folderId, 'is_hidden' => false, 'is_deleted' => false])->offset($page*50)->limit($perPage)->all();

foreach ($fileQuery as $file) {
$data = $file->toArray(['id', 'folderId', 'name', 'isImage', 'sizeReadable', 'extension', 'uploadTimestamp']);
if ($file->isImage) {
// add tiny thumbnail
Expand All @@ -107,13 +112,20 @@ public function actionDataFiles()
}
}
}
$files[] = $data;

$files[$data['id']] = $data;
}
$this->setHasCache('storageApiDataFiles', $files, new DbDependency(['sql' => 'SELECT MAX(id) FROM admin_storage_file WHERE is_deleted=false']), 0);
return $files;
}

return $files;
}, 0, new DbDependency(['sql' => 'SELECT MAX(id) FROM admin_storage_file WHERE is_deleted=false']));

return $cache;
return [
'perPage' => $perPage,
'totalCount' => $totalCount,
'count' => count($files),
'data' => $files,
'page' => (int) $page,
];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/resources/dist/css/admin.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/resources/dist/js/main.uglified.js

Large diffs are not rendered by default.

29 changes: 19 additions & 10 deletions src/resources/js/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -2391,10 +2391,16 @@

$scope.$watch('fileId', function(n, o) {
if (n != 0 && n != null && n !== undefined) {

$scope.ServiceFilesData.getFile(n).then(function(file) {
$scope.fileinfo = file;
});
/*
var filtering = $filter('filter')($scope.filesData, {id: parseInt(n)}, true);
if (filtering && filtering.length == 1) {
$scope.fileinfo = filtering[0];
}
*/
}
});
}],
Expand Down Expand Up @@ -2624,16 +2630,19 @@

// ServiceFilesData inheritance

$scope.filesData = ServiceFilesData.data;

$scope.$on('service:FilesData', function(event, data) {
$scope.filesData = data;
$scope.filesData = [];

// load files data for a given folder id
$scope.$watch('currentFolderId', function(folderId) {
$scope.getFilesForPageAndFolder(folderId, 0);
});

$scope.filesDataReload = function() {
return ServiceFilesData.load(true);
}

$scope.getFilesForPageAndFolder = function(folderId, pageId) {
$http.get('admin/api-admin-storage/data-files?folderId='+folderId+'&page='+pageId).then(function(response) {
$scope.filesData = response.data.data;
});
};

// ServiceFolderId

$scope.currentFolderId = ServiceFoldersDirecotryId.folderId;
Expand Down Expand Up @@ -2916,8 +2925,8 @@
$scope.closeFileDetail();
} else {

$http.get('admin/api-admin-storage/file-info?id='+file.id).then(function(response) {
$scope.fileDetailFull = response.data;
ServiceFilesData.getFile(file.id).then(function(responseFile) {
$scope.fileDetailFull = responseFile;
});

$scope.fileDetail = file;
Expand Down
36 changes: 33 additions & 3 deletions src/resources/js/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ adminServiceResolver = ['ServiceFoldersData', 'ServiceImagesData', 'ServiceFiles
ServiceFiltersData.load();
ServiceFoldersData.load();
ServiceImagesData.load();
ServiceFilesData.load();
//ServiceFilesData.load();
ServiceLanguagesData.load();
ServicePropertiesData.load();
AdminLangService.load();
Expand Down Expand Up @@ -127,6 +127,7 @@ zaa.factory("ServiceImagesData", ['$http', '$q', '$rootScope', function($http, $
service.data = null;

service.load = function(forceReload) {
/*
return $q(function(resolve, reject) {
if (service.data !== null && forceReload !== true) {
resolve(service.data);
Expand All @@ -138,6 +139,7 @@ zaa.factory("ServiceImagesData", ['$http', '$q', '$rootScope', function($http, $
});
}
});
*/
};

return service;
Expand All @@ -156,25 +158,53 @@ $scope.filesDataReload = function() {
}
*/
zaa.factory("ServiceFilesData", ['$http', '$q', '$rootScope', function($http, $q, $rootScope) {
zaa.factory("ServiceFilesData", ['$http', '$q', '$rootScope', '$log', function($http, $q, $rootScope, $log) {
var service = [];

service.data = null;
service.data = {};

service.load = function(forceReload) {
return $q(function(resolve, reject) {
if (service.data !== null && forceReload !== true) {
resolve(service.data);
} else {
$log.info('load full list of all files from storage data-files api.');
$http.get("admin/api-admin-storage/data-files").then(function(response) {
service.data = response.data;
$rootScope.$broadcast('service:FilesData', service.data);
resolve(service.data);
$log.info('files are loaded from storage api and written to data property.');
$log.info(service.data);
});
}
});
};

/**
* Get a given file from the storage system by its id.
*
* ```js
* ServiceFilesData.getFile(1).then(function(response) {
* console.log(response);
* });
*/
service.getFile = function(id, forceAsyncRequest) {
return $q(function(resolve, reject) {
$log.info('request file id ' + id);

if (service.data.hasOwnProperty(id)) {
$log.info('file exists in datat array.');
return resolve(service.data[id]);
}

$http.get('admin/api-admin-storage/file-info?id='+id).then(function(response) {
service.data[response.id] = response;

return resolve(response);
});
});
};

return service;
}]);

Expand Down
2 changes: 1 addition & 1 deletion src/resources/js/zaa.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ zaa.config(['$httpProvider', '$stateProvider', '$controllerProvider', '$urlMatch
/**
* resolver (or resolverProvider).
*
* > Warning: The config part is known injected `resolverProvider` event when the provider name is `resolver`.
* > Warning: The config part is known injected `resolverProvider` even when the provider name is `resolver`.
* > Info: can not rename this in admin 1.2 release due to usage in cms module old version branch
*
* Attach custom callback function to the custom state resolve. Use the resolverProvider in
Expand Down

0 comments on commit 2514e2f

Please sign in to comment.