wester /chunk-upload
使用PHP和本地以及FTP驱动程序轻松处理分块上传,具有高级验证功能和本地化支持。
Requires
- php: ^8.0
README
Wester chunk upload 是一个PHP库,用于处理分块上传,支持本地和FTP文件上传。
内置的文件验证器会让你感到安全。
目录
安装
composer require wester/chunk-upload
基本用法
以下是一个包的示例。
// You don't need this line in laravel or some other frameworks. require("./vendor/autoload.php"); use Wester\ChunkUpload\Chunk; use Wester\ChunkUpload\Validation\Exceptions\ValidationException; try { $chunk = new Chunk([ 'name' => 'video', // same as $_FILES['video'] 'chunk_size' => 4000, // must be equal to the value specified on the client side // Driver 'driver' => 'local', // [local, ftp] // Local driver details 'local_driver' => [ 'path' => __DIR__ . '/uploads/', // where to upload the final file 'tmp_path' => __DIR__ . '/uploads/temp/', // where to store the temp chunks ], // FTP driver details 'ftp_driver' => [ 'server' => '', 'username' => '', 'password' => '', 'path' => '/uploads/', // where to upload the final file 'tmp_path' => '/uploads/temp/', // where to store the temp chunks ], // File details 'file_name' => Chunk::RANDOM_FILE_NAME, 'file_extension' => Chunk::ORIGINAL_FILE_EXTENSION, // File validation 'validation' => ['extension:mp4,avi'], ]); $chunk->validate()->store(); if ($chunk->isLast()) { // done $chunk->getFilePath(); } else { $chunk->response()->json([ 'progress' => $chunk->getProgress() ]); } } catch (ValidationException $e) { $e->response(422)->json([ 'message' => $e->getMessage(), 'data' => $e->getErrors(), ]); } catch (\Exception $e) { $e->response(400)->abort(); }
驱动程序
该包支持默认的ftp文件上传。
可以使用local和ftp或自定义驱动程序。
'driver' => 'ftp',
-
实现驱动程序
您的自定义驱动程序应实现
\Wester\ChunkUpload\Drivers\Contracts\DriverInterface。'driver' => \My\Custom\Drivers\DriverName::class, 'custom_driver' => [ 'path' => '/uploads/', 'tmp_path' => '/uploads/temp/', ],
<?php namespace My\Custom\Drivers; class DriverName implements \Wester\ChunkUpload\Drivers\Contracts\DriverInterface { public function open() {}; public function close() {}; public function store($fileName) {}; public function delete() {}; public function move() {}; public function increase() {}; public function prevExists() {}; public function exists() {}; }
方法
-
store()存储块并将其合并。 -
validate()验证块。 -
getFilePath()获取最终文件路径。 -
getProgress()获取进度百分比(浮点数)。 -
isLast()检查是否是最后一个块。 -
getFileExtension()获取文件扩展名。 -
getFileName()获取不带扩展名的文件名。 -
getFullFileName()获取带扩展名的完整文件名。 -
getTempFilePath()获取临时文件路径。 -
getSize()获取当前块的大小。 -
getTotalNumber()获取块的总数。 -
setLanguage([...])将语言设置为提供的数组 -
response($status = null)返回\Wester\ChunkUpload\Response的实例$chunk->response(200)->json([...]); $chunk->response()->json([...]); // If an exception is caught... $e->response(400)->... $e->response(400)->abort(); $e->response()->abort(400); ...
属性
-
configs返回解析后的配置数组。$chunk->configs['name']; ...
-
header返回\Wester\ChunkUpload\Header的实例$chunk->header->chunkNumber; $chunk->header->chunkTotalNumber; $chunk->header->chunkSize; // equal to: x-chunk-size $chunk->header->fileName; $chunk->header->fileSize; $chunk->header->fileIdentity;
验证规则
-
扩展'validation' => ['extension:mp4,avi']
-
大小'validation' => ['size:237492']
-
最小'validation' => ['min:10000']
-
最大'validation' => ['max:90000']
语言
您可以轻松更改验证消息,就像Laravel一样。
$chunk->setLanguage([ 'min' => [ 'numeric' => 'The :attribute must be at least :min.', 'file' => 'The :attribute must be at least :min kilobytes.', ], 'max' => [ 'numeric' => 'The :attribute may not be greater than :max.', 'file' => 'The :attribute may not be greater than :max kilobytes.', ], 'size' => [ 'numeric' => 'The :attribute must be :size.', 'file' => 'The :attribute must be :size kilobytes.', ], 'mimes' => 'The :attribute must be a file of type: :values.', 'attributes' => [ 'x-file-name' => 'file', 'x-file-size' => 'file', ], ]);
标志
Chunk::RANDOM_FILE_NAME创建一个随机文件名。Chunk::ORIGINAL_FILE_NAME保留原始文件名。Chunk::ORIGINAL_FILE_EXTENSION保留原始文件扩展名。
您还可以指定自定义文件名和扩展名。
HTTP响应状态码
该包使用HTTP响应状态码来决定在上传请求成功或失败时下一步做什么。
请随意向您的客户端添加更多状态码。
如果返回其他状态码,则必须重新上传块,例如
超时和网络错误。
客户端
头部
应向服务器发送一些头部信息。
x-chunk-number正在上传的当前块编号。x-chunk-total-number块的总数。x-chunk-size每个块的最大大小。(每个块必须为4000字节,只有最后一个块可以小于这个大小)x-file-name上传的文件名。x-file-size上传的文件大小。x-file-identity用于文件的随机字符串,长度必须为32个字符。
头部示例。
{
"x-chunk-number" : 1,
"x-chunk-total-number" : 5,
"x-chunk-size" : 4000,
"x-file-name" : "my-file-name.mp4",
"x-file-size" : 20000,
"x-file-identity" : "rmghdygvdstcsjglltmbvkynxpeajgcg"
}
示例 & 包
您可以在wester-chunk-upload-examples存储库中找到示例。
JavaScript
JavaScript客户端实现。
React Native
React Native中的客户端实现。
贡献
- 如果您想在其他语言中添加更多实现,请将您的PR提交到wester-chunk-upload-examples仓库。
支持我们
只需给该仓库点个赞,就这样!😉