mirror of
https://github.com/kuaifan/dootask.git
synced 2025-12-15 05:12:49 +00:00
fix: 修复打包下载问题
This commit is contained in:
parent
fa149fcaa9
commit
6db82c8176
@ -2,8 +2,10 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\Api;
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use Hhxsv5\LaravelS\Swoole\Task\Task;
|
||||||
use App\Exceptions\ApiException;
|
use App\Exceptions\ApiException;
|
||||||
use App\Models\AbstractModel;
|
use App\Models\AbstractModel;
|
||||||
|
use App\Tasks\FilePackTask;
|
||||||
use App\Models\File;
|
use App\Models\File;
|
||||||
use App\Models\FileContent;
|
use App\Models\FileContent;
|
||||||
use App\Models\FileLink;
|
use App\Models\FileLink;
|
||||||
@ -1055,39 +1057,7 @@ class FileController extends AbstractController
|
|||||||
abort(403, "The total size of the file exceeds 1GB. Please download it in batches.");
|
abort(403, "The total size of the file exceeds 1GB. Please download it in batches.");
|
||||||
}
|
}
|
||||||
|
|
||||||
$zip = new \ZipArchive();
|
Task::deliver(new FilePackTask($user, $files, $downName));
|
||||||
$zipName = 'temp/download/' . date("Ym") . '/' . $user->userid . '/' . $downName;
|
|
||||||
$zipPath = storage_path('app/'.$zipName);
|
|
||||||
Base::makeDir(dirname($zipPath));
|
|
||||||
|
|
||||||
if ($zip->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
|
|
||||||
abort(403, "Failed to create compressed file.");
|
|
||||||
}
|
|
||||||
|
|
||||||
go(function() use ($zip, $files, $downName) {
|
|
||||||
Coroutine::sleep(0.1);
|
|
||||||
// 压缩进度
|
|
||||||
$progress = 0;
|
|
||||||
$zip->registerProgressCallback(0.05, function($ratio) use ($downName, &$progress) {
|
|
||||||
$progress = round($ratio * 100);
|
|
||||||
File::filePushMsg('compress', [
|
|
||||||
'name'=> $downName,
|
|
||||||
'progress' => $progress
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
|
|
||||||
foreach ($files as $file) {
|
|
||||||
File::addFileTreeToZip($zip, $file);
|
|
||||||
}
|
|
||||||
$zip->close();
|
|
||||||
if ($progress < 100) {
|
|
||||||
File::filePushMsg('compress', [
|
|
||||||
'name'=> $downName,
|
|
||||||
'progress' => 100
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return Base::retSuccess('success');
|
return Base::retSuccess('success');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -926,12 +926,10 @@ class File extends AbstractModel
|
|||||||
*/
|
*/
|
||||||
public static function filePushMsg($action, $data = null, $userid = null)
|
public static function filePushMsg($action, $data = null, $userid = null)
|
||||||
{
|
{
|
||||||
//
|
|
||||||
$userid = User::auth()->userid();
|
$userid = User::auth()->userid();
|
||||||
if (empty($userid)) {
|
if (empty($userid)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//
|
|
||||||
$msg = [
|
$msg = [
|
||||||
'type' => 'file',
|
'type' => 'file',
|
||||||
'action' => $action,
|
'action' => $action,
|
||||||
@ -941,7 +939,6 @@ class File extends AbstractModel
|
|||||||
'userid' => $userid,
|
'userid' => $userid,
|
||||||
'msg' => $msg
|
'msg' => $msg
|
||||||
];
|
];
|
||||||
$task = new PushTask($params, false);
|
Task::deliver(new PushTask($params));
|
||||||
Task::deliver($task);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
68
app/Tasks/FilePackTask.php
Normal file
68
app/Tasks/FilePackTask.php
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Tasks;
|
||||||
|
|
||||||
|
use App\Models\File;
|
||||||
|
use App\Module\Base;
|
||||||
|
use ZipArchive;
|
||||||
|
|
||||||
|
@error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件打包
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class FilePackTask extends AbstractTask
|
||||||
|
{
|
||||||
|
protected $user;
|
||||||
|
protected $files;
|
||||||
|
protected $downName;
|
||||||
|
public function __construct($user, $files, $downName)
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
$this->user = $user;
|
||||||
|
$this->files = $files;
|
||||||
|
$this->downName = $downName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function start()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function end()
|
||||||
|
{
|
||||||
|
// 压缩进度
|
||||||
|
$progress = 0;
|
||||||
|
$filePackName = $this->downName;
|
||||||
|
|
||||||
|
$zip = new ZipArchive();
|
||||||
|
$zipName = 'temp/download/' . date("Ym") . '/' . $this->user->userid . '/' . $filePackName;
|
||||||
|
$zipPath = storage_path('app/'.$zipName);
|
||||||
|
Base::makeDir(dirname($zipPath));
|
||||||
|
|
||||||
|
if ($zip->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$zip->registerProgressCallback(0.05, function($ratio) use ($filePackName, &$progress) {
|
||||||
|
$progress = round($ratio * 100);
|
||||||
|
File::filePushMsg('compress', [
|
||||||
|
'name'=> $filePackName,
|
||||||
|
'progress' => $progress
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
foreach ($this->files as $file) {
|
||||||
|
File::addFileTreeToZip($zip, $file);
|
||||||
|
}
|
||||||
|
$zip->close();
|
||||||
|
if ($progress < 100) {
|
||||||
|
File::filePushMsg('compress', [
|
||||||
|
'name'=> $filePackName,
|
||||||
|
'progress' => 100
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user