mirror of
https://github.com/kuaifan/dootask.git
synced 2025-12-12 19:24:17 +00:00
perf: 优化实现文件夹下载以及多文件压缩下载功能
This commit is contained in:
parent
3fdcbf92b6
commit
51b1d78d8a
@ -998,12 +998,18 @@ class FileController extends AbstractController
|
||||
}
|
||||
|
||||
$files = [];
|
||||
AbstractModel::transaction(function() use ($user, $ids, &$files) {
|
||||
foreach ($ids as $id) {
|
||||
$files[] = File::getFilesTree(intval($id), $user, 0);
|
||||
$totalSize = 0;
|
||||
AbstractModel::transaction(function() use ($user, $ids, &$files, &$totalSize) {
|
||||
foreach ($ids as $k => $id) {
|
||||
$files[] = File::getFilesTree(intval($id), $user, 1);
|
||||
$totalSize += $files[$k]->totalSize;
|
||||
}
|
||||
});
|
||||
|
||||
if ($totalSize > File::zipMaxSize) {
|
||||
throw new ApiException('文件总大小已超过1GB,请分批下载');
|
||||
}
|
||||
|
||||
return Base::retSuccess('success');
|
||||
}
|
||||
|
||||
@ -1026,13 +1032,26 @@ class FileController extends AbstractController
|
||||
$user = User::auth();
|
||||
$ids = Request::input('ids');
|
||||
|
||||
if (!is_array($ids) || empty($ids)) {
|
||||
abort(403, "Please select the file or folder to download.");
|
||||
}
|
||||
if (count($ids) > 100) {
|
||||
abort(403, "You can download a maximum of 100 files or folders at a time.");
|
||||
}
|
||||
|
||||
$files = [];
|
||||
AbstractModel::transaction(function() use ($user, $ids, &$files) {
|
||||
foreach ($ids as $id) {
|
||||
$files[] = File::getFilesTree(intval($id), $user, 0);
|
||||
$totalSize = 0;
|
||||
AbstractModel::transaction(function() use ($user, $ids, &$files, &$totalSize) {
|
||||
foreach ($ids as $k => $id) {
|
||||
$files[] = File::getFilesTree(intval($id), $user, 1);
|
||||
$totalSize += $files[$k]->totalSize;
|
||||
}
|
||||
});
|
||||
|
||||
if ($totalSize > File::zipMaxSize) {
|
||||
abort(403, "The total size of the file exceeds 1GB. Please download it in batches.");
|
||||
}
|
||||
|
||||
$zip = new \ZipArchive();
|
||||
// 下载文件名
|
||||
$downName = count($ids) > 1 ? 'file_'. date("YmdHis") : $files[0]->name;
|
||||
@ -1041,7 +1060,7 @@ class FileController extends AbstractController
|
||||
Base::makeDir(dirname($zipPath));
|
||||
|
||||
if ($zip->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
|
||||
return Base::retError('Failed to create compressed file');
|
||||
abort(403, "Failed to create compressed file.");
|
||||
}
|
||||
|
||||
array_walk($files, function($file) use ($zip) {
|
||||
|
||||
@ -95,6 +95,11 @@ class File extends AbstractModel
|
||||
'avi', 'mov', 'wmv', 'mkv', '3gp', 'rm',
|
||||
];
|
||||
|
||||
/**
|
||||
* 压缩包下载大小限制
|
||||
*/
|
||||
const zipMaxSize = 1024 * 1024 * 1024; // 1G
|
||||
|
||||
|
||||
/**
|
||||
* 获取文件列表
|
||||
@ -838,7 +843,7 @@ class File extends AbstractModel
|
||||
*
|
||||
* @param int $fileId
|
||||
* @param User $user
|
||||
* @param int $permission
|
||||
* @param int $permission 0-访问权限、1-读写权限、1000-所有者或创建者
|
||||
* @param string $path
|
||||
* @param int $totalSize
|
||||
* @return object
|
||||
@ -861,11 +866,7 @@ class File extends AbstractModel
|
||||
} else {
|
||||
$totalSize += $file->size;
|
||||
}
|
||||
|
||||
if ($totalSize > 1024 * 1024 * 1024) { // 1GB
|
||||
throw new ApiException('文件总大小已超过1GB,请分批下载');
|
||||
}
|
||||
|
||||
$file->totalSize = $totalSize;
|
||||
return $file;
|
||||
}
|
||||
|
||||
|
||||
@ -1434,7 +1434,6 @@ export default {
|
||||
this.$store.dispatch('downUrl', $A.apiUrl(`file/download/zip?ids[]=${idsParam}`));
|
||||
}).catch(({msg}) => {
|
||||
$A.modalError(msg)
|
||||
reject(msg);
|
||||
});
|
||||
})
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user