2020-11-21 19:53:01 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Service;
|
|
|
|
|
|
|
|
use App\Model\FileSplitUpload;
|
|
|
|
use Hyperf\Utils\Str;
|
|
|
|
use Hyperf\HttpMessage\Upload\UploadedFile;
|
2021-06-30 19:27:49 +08:00
|
|
|
use League\Flysystem\Filesystem;
|
2020-11-21 19:53:01 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 文件拆分上传服务
|
|
|
|
*
|
|
|
|
* @package App\Service
|
|
|
|
*/
|
|
|
|
class SplitUploadService
|
|
|
|
{
|
2020-12-02 17:15:32 +08:00
|
|
|
/**
|
|
|
|
* 文件拆分的大小
|
|
|
|
*/
|
2020-11-21 19:53:01 +08:00
|
|
|
const SPLIT_SIZE = 2 * 1024 * 1024;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 创建文件拆分相关信息
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $user_id 用户ID
|
2020-11-21 19:53:01 +08:00
|
|
|
* @param string $fileName 上传的文件名
|
|
|
|
* @param string $fileSize 上传文件大小
|
2020-12-02 17:15:32 +08:00
|
|
|
* @return array|bool
|
2020-11-21 19:53:01 +08:00
|
|
|
*/
|
|
|
|
public function create(int $user_id, string $fileName, string $fileSize)
|
|
|
|
{
|
|
|
|
$hash_name = implode('-', [uniqid(), rand(10000000, 99999999), Str::random(6)]);
|
|
|
|
$split_num = intval(ceil($fileSize / self::SPLIT_SIZE));
|
|
|
|
|
2021-04-20 16:30:57 +08:00
|
|
|
$data = [];
|
|
|
|
$data['file_type'] = 1;
|
|
|
|
$data['user_id'] = $user_id;
|
2020-11-21 19:53:01 +08:00
|
|
|
$data['original_name'] = $fileName;
|
2021-04-20 16:30:57 +08:00
|
|
|
$data['hash_name'] = $hash_name;
|
|
|
|
$data['file_ext'] = pathinfo($fileName, PATHINFO_EXTENSION);
|
|
|
|
$data['file_size'] = $fileSize;
|
|
|
|
$data['upload_at'] = time();
|
2020-11-21 19:53:01 +08:00
|
|
|
|
2021-04-22 16:14:34 +08:00
|
|
|
// 文件拆分数量
|
2021-04-20 16:30:57 +08:00
|
|
|
$data['split_num'] = $split_num;
|
2020-11-21 19:53:01 +08:00
|
|
|
$data['split_index'] = $split_num;
|
|
|
|
|
|
|
|
return FileSplitUpload::create($data) ? array_merge($data, ['split_size' => self::SPLIT_SIZE]) : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-02 17:15:32 +08:00
|
|
|
* 保存拆分上传的文件
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $user_id 用户ID
|
|
|
|
* @param UploadedFile $file 文件信息
|
|
|
|
* @param string $hashName 上传临时问价hash名
|
|
|
|
* @param int $split_index 当前拆分文件索引
|
|
|
|
* @param int $fileSize 文件大小
|
2020-12-02 17:15:32 +08:00
|
|
|
* @return bool
|
2020-11-21 19:53:01 +08:00
|
|
|
*/
|
|
|
|
public function upload(int $user_id, UploadedFile $file, string $hashName, int $split_index, int $fileSize)
|
|
|
|
{
|
|
|
|
$fileInfo = FileSplitUpload::select(['id', 'original_name', 'split_num', 'file_ext'])
|
|
|
|
->where([['user_id', '=', $user_id], ['hash_name', '=', $hashName], ['file_type', '=', 1]])
|
|
|
|
->first();
|
|
|
|
|
2021-06-30 19:27:49 +08:00
|
|
|
if (!$fileInfo) return false;
|
2020-11-21 19:53:01 +08:00
|
|
|
|
2021-06-30 19:27:49 +08:00
|
|
|
$save_path = "tmp/{$hashName}/" . "{$hashName}_{$split_index}_{$fileInfo->file_ext}.tmp";
|
|
|
|
try {
|
2021-07-23 21:34:29 +08:00
|
|
|
di()->get(Filesystem::class)->write($save_path, file_get_contents($file->getRealPath()));
|
2021-06-30 19:27:49 +08:00
|
|
|
} catch (\Exception $e) {
|
2020-11-21 19:53:01 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$info = FileSplitUpload::where('user_id', $user_id)->where('hash_name', $hashName)->where('split_index', $split_index)->first();
|
|
|
|
if (!$info) {
|
2021-04-20 16:30:57 +08:00
|
|
|
return (bool)FileSplitUpload::create([
|
|
|
|
'user_id' => $user_id,
|
|
|
|
'file_type' => 2,
|
|
|
|
'hash_name' => $hashName,
|
2020-11-21 19:53:01 +08:00
|
|
|
'original_name' => $fileInfo->original_name,
|
2021-04-20 16:30:57 +08:00
|
|
|
'split_index' => $split_index,
|
|
|
|
'split_num' => $fileInfo->split_num,
|
2021-06-30 19:27:49 +08:00
|
|
|
'save_dir' => $save_path,
|
2021-04-20 16:30:57 +08:00
|
|
|
'file_ext' => $fileInfo->file_ext,
|
|
|
|
'file_size' => $fileSize,
|
|
|
|
'upload_at' => time(),
|
|
|
|
]);
|
2020-11-21 19:53:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 文件合并
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $user_id 用户ID
|
2020-11-21 19:53:01 +08:00
|
|
|
* @param string $hash_name 上传临时问价hash名
|
2020-12-02 17:15:32 +08:00
|
|
|
* @return array|bool
|
2020-11-21 19:53:01 +08:00
|
|
|
*/
|
|
|
|
public function merge(int $user_id, string $hash_name)
|
|
|
|
{
|
2020-12-02 17:15:32 +08:00
|
|
|
$fileInfo = FileSplitUpload::select(['id', 'original_name', 'split_num', 'file_ext', 'file_size'])
|
|
|
|
->where('user_id', $user_id)
|
|
|
|
->where('hash_name', $hash_name)
|
|
|
|
->where('file_type', 1)
|
|
|
|
->first();
|
|
|
|
|
2021-06-30 19:27:49 +08:00
|
|
|
if (!$fileInfo) return false;
|
2020-11-21 19:53:01 +08:00
|
|
|
|
2020-12-02 17:15:32 +08:00
|
|
|
$files = FileSplitUpload::where('user_id', $user_id)
|
|
|
|
->where('hash_name', $hash_name)
|
|
|
|
->where('file_type', 2)
|
|
|
|
->orderBy('split_index', 'asc')
|
|
|
|
->get(['split_index', 'save_dir'])->toArray();
|
|
|
|
|
2021-06-30 19:27:49 +08:00
|
|
|
if (!$files || count($files) != $fileInfo->split_num) return false;
|
2020-11-21 19:53:01 +08:00
|
|
|
|
2021-06-30 19:27:49 +08:00
|
|
|
$file_merge = "tmp/{$hash_name}/{$fileInfo->original_name}.tmp";
|
2020-11-21 19:53:01 +08:00
|
|
|
|
2021-07-23 21:34:29 +08:00
|
|
|
$filesystem = di()->get(Filesystem::class);
|
2021-06-30 19:27:49 +08:00
|
|
|
$root_path = $filesystem->getConfig()->get('root');
|
2020-11-21 19:53:01 +08:00
|
|
|
foreach ($files as $file) {
|
2021-06-30 19:27:49 +08:00
|
|
|
file_put_contents(
|
|
|
|
"{$root_path}/{$file_merge}",
|
|
|
|
$filesystem->read($file['save_dir']),
|
|
|
|
FILE_APPEND
|
|
|
|
);
|
2020-11-21 19:53:01 +08:00
|
|
|
}
|
|
|
|
|
2020-12-02 17:15:32 +08:00
|
|
|
FileSplitUpload::select(['id', 'original_name', 'split_num', 'file_ext', 'file_size'])
|
|
|
|
->where('user_id', $user_id)->where('hash_name', $hash_name)
|
|
|
|
->where('file_type', 1)
|
2021-06-30 19:27:49 +08:00
|
|
|
->update(['save_dir' => $file_merge]);
|
2020-12-02 17:15:32 +08:00
|
|
|
|
2020-11-21 19:53:01 +08:00
|
|
|
return [
|
2021-06-30 19:27:49 +08:00
|
|
|
'path' => $file_merge,
|
2020-11-21 19:53:01 +08:00
|
|
|
'tmp_file_name' => "{$fileInfo->original_name}.tmp",
|
|
|
|
'original_name' => $fileInfo->original_name,
|
2021-04-20 16:30:57 +08:00
|
|
|
'file_size' => $fileInfo->file_size
|
2020-11-21 19:53:01 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|