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