wester/chunk-upload
Composer 安装命令:
composer require wester/chunk-upload
包简介
Handle chunked uploads with local and ftp drivers simply in PHP with advanced validation features and localization.
README 文档
README
Wester chunk upload is a php library to handle chunked uploads which supports local and ftp file upload out of the box.
You'll feel safe with the built-in file validator.
Table of contents
- Installation
- Basic Usage
- Drivers
- Methods
- Properties
- Validation Rules
- Language
- Flags
- HTTP Response Status Codes
- Client Side
- Support Us
Installation
composer require wester/chunk-upload
Basic Usage
Here's an example of the package.
// 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(); }
Drivers
This package supports ftp file upload out of the box.
local and ftp or custom drivers can be used.
'driver' => 'ftp',
-
Implement The Driver
Your custom driver should implement the
\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() {}; }
Methods
-
store()stores the chunk and merges it. -
validate()validates the chunk. -
getFilePath()gets the final file path. -
getProgress()gets the progress percentage (float). -
isLast()checks if its the last chunk. -
getFileExtension()gets the file extension. -
getFileName()gets the file name without extension. -
getFullFileName()gets the full file name with extension. -
getTempFilePath()gets the temp file path. -
getSize()gets the current chunk size. -
getTotalNumber()gets the total number of chunks. -
setLanguage([...])sets the language to the provided array -
response($status = null)returns an instance of\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); ...
Properties
-
configsreturns an array of the parsed configs.$chunk->configs['name']; ...
-
headerreturns an instance of\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 Rules
-
extension'validation' => ['extension:mp4,avi']
-
size'validation' => ['size:237492']
-
min'validation' => ['min:10000']
-
max'validation' => ['max:90000']
Language
You can easily change the validation messages the same as 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', ], ]);
Flags
Chunk::RANDOM_FILE_NAMEcreates a random file name.Chunk::ORIGINAL_FILE_NAMEpreserves the original file name.Chunk::ORIGINAL_FILE_EXTENSIONpreserves the original file extension.
You can also specify a custom file name and extension.
HTTP Response Status Codes
This package uses the HTTP response status codes to decide what to do next if the request fails or succeeds when uploading.
-
Success
200All of the chunks have been uploaded completely.201The server is waiting for the next chunk to be sent.
-
Errors
The following status codes will interrupt the process.
400404415422500501
Feel free to add more status codes to your client side.
If another status code is returned the chunk must be re-uploaded such as
timeoutandnetwork error.
Client Side
Headers
There are some headers that should be sent to the server.
x-chunk-numberThe current chunk number which is being uploaded.x-chunk-total-numberThe total number of chunks.x-chunk-sizeMaximum size of each chunk. (each chunk must be 4000 bytes and only the last chunk can be less than that)x-file-nameThe uploaded file name.x-file-sizeThe uploaded file size.x-file-identityRandom string for the file which must be 32 characters in length.
An example of the headers.
{
"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"
}
Examples & Packages
You can find examples in wester-chunk-upload-examples repository.
Javascript
The client side implementation in Javascript.
React Native
The client side implementation in React Native.
Contribution
- If you want to add more implementations in other languages please submit your PR to the wester-chunk-upload-examples repository.
Support Us
Just star the repository, that's it! 😉
wester/chunk-upload 适用场景与选型建议
wester/chunk-upload 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.49k 次下载、GitHub Stars 达 17, 最近一次更新时间为 2020 年 09 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「upload」 「validation」 「chunk」 「resumable」 「chunked-upload」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 wester/chunk-upload 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wester/chunk-upload 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 wester/chunk-upload 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
Adds request-parameter validation to the SLIM 3.x PHP framework
Laravel Media Popup to upload/view the files
A jQuery augmented PHP library for creating and validating HTML forms
A Laravel validator for delimiter-separated list of emails.
A CakePHP behavior to validate foreign keys
统计信息
- 总下载量: 11.49k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 17
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-09-07