承接 wester/chunk-upload 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

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

  • configs returns an array of the parsed configs.

    $chunk->configs['name'];
    ...
  • header returns 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_NAME creates a random file name.
  • Chunk::ORIGINAL_FILE_NAME preserves the original file name.
  • Chunk::ORIGINAL_FILE_EXTENSION preserves 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

    • 200 All of the chunks have been uploaded completely.
    • 201 The server is waiting for the next chunk to be sent.
  • Errors

    The following status codes will interrupt the process.

    • 400
    • 404
    • 415
    • 422
    • 500
    • 501

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 timeout and network error.

Client Side

Headers

There are some headers that should be sent to the server.

  • x-chunk-number The current chunk number which is being uploaded.
  • x-chunk-total-number The total number of chunks.
  • x-chunk-size Maximum size of each chunk. (each chunk must be 4000 bytes and only the last chunk can be less than that)
  • x-file-name The uploaded file name.
  • x-file-size The uploaded file size.
  • x-file-identity Random 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

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 wester/chunk-upload 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 11.49k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 17
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 17
  • Watchers: 2
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-09-07