承接 coding-socks/laravel-upload-handler 相关项目开发

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

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

coding-socks/laravel-upload-handler

Composer 安装命令:

composer require coding-socks/laravel-upload-handler

包简介

This package helps integrate a Laravel application with chunk uploader libraries eg. DropzoneJS and Resumable.js

README 文档

README

Upload Handler Package For Laravel

Github Actions Badge Downloads Badge Version Badge Coverage Badge License Badge

This package helps integrate a Laravel application with chunk uploader libraries eg. DropzoneJS and jQuery-File-Upload from blueimp.

Uploading a large file in chunks can help reduce risks.

  • PHP from 5.3.4 limits the number of concurrent uploads and by uploading a file in one request can limit the availability of a service. (max_file_uploads)
  • For security reasons many systems limit the payload size, and the uploadable file size. PHP is not an exception. (upload_max_filesize)
  • It can be useful to check the meta information of a file and decline an upload upfront, so the user does not have to wait for minutes or seconds to upload a large file and then receive an error message for an invalid the file type or mime type.
  • Can include resume functionality which means an upload can be continued after a reconnection.

However, there is not a single RFC about chunked uploads and this caused many implementations. The most mature project at the moment is tus.

Similar projects:

Table of contents

Installation

You can easily install this package using Composer, by running the following command:

composer require coding-socks/laravel-upload-handler

Requirements

This package has the following requirements:

  • PHP ^7.3
  • Laravel ^6.10 || ^7.0 || ^8.0

Caret Version Range (^)

Usage

  1. Register a route
Route::any('/my-route', 'MyController@myFunction');
  1. Retrieve the upload handler. (The chunk upload handler can be retrieved from the container in two ways.)
  • Using dependency injection
use Illuminate\Http\Request;
use CodingSocks\UploadHandler\UploadHandler;

class MyController extends Controller
{
    public function myFunction(Request $request, UploadHandler $handler)
    {
        return $handler->handle($request);
    }
}
  • Resolving from the app container
use Illuminate\Http\Request;
use CodingSocks\UploadHandler\UploadHandler;

class MyController extends Controller
{
    public function myFunction(Request $request)
    {
        $handler = app()->make(UploadHandler::class);
        return $handler->handle($request);
    }
}

The handler exposes the following methods:

Method Description
handle Handle the given request

"Handle" is quite vague but there is a reason for that. This library tries to provide more functionality than just saving the uploaded chunks. It is also adds functionality for resumable uploads which depending on the client side library can differ very much.

Events

Once a file upload finished a \CodingSocks\UploadHandler\Event\FileUploaded is triggered. This event contains the disk and the path of the uploaded file.

You can also add a Closure as the second parameter of the handle method to add an inline listener. The listener is called with the disk and the path of the uploaded file.

$handler->handle($request, function ($disk, $path) {
    // Triggered when upload is finished
});

Changing the driver

You can change the default driver by setting an UPLOAD_DRIVER environment variable or publishing the config file and changing it directly.

Adding your own drivers

Much like Laravel's core components, you can add your own drivers for this package. You can do this by adding the following code to a service provider.

app()->make(UploadManager::class)->extend('my_driver', function () {
    return new MyCustomUploadDriver();
});

If you are adding a driver you need to extend the \CodingSocks\UploadHandler\Driver\BaseHandler abstract class, for which you can use the shipped drivers (e.g. \CodingSocks\UploadHandler\Driver\BlueimpUploadDriver) as an example as to how.

If you wrote a custom driver that others might find useful, please consider adding it to the package via a pull request.

Drivers

Below is a list of available drivers along with their individual specs:

Service Driver name Chunk upload Resumable
Monolith monolith no no
Blueimp blueimp yes yes
DropzoneJS dropzone yes no
Flow.js flow-js yes yes
ng-file-upload ng-file-upload yes no
Plupload plupload yes no
Resumable.js resumable-js yes yes
simple-uploader.js simple-uploader-js yes yes

Monolith driver

This driver is a fallback driver as it can handle normal file request. Save and delete capabilities are also added.

Blueimp driver

website

This driver handles requests made by the Blueimp jQuery File Upload client library.

DropzoneJS driver

website

This driver handles requests made by the DropzoneJS client library.

Flow.js driver

website

This driver handles requests made by the Flow.js client library.

Because of Issue #44 you must use forceChunkSize option.

ng-file-upload driver

website

This driver handles requests made by the ng-file-upload client library.

Plupload driver

website

This driver handles requests made by the Plupload client library.

Resumable.js driver

website

This driver handles requests made by the Resumable.js client library.

Because of Issue #44 you must use forceChunkSize option.

simple-uploader.js driver

website

This driver handles requests made by the simple-uploader.js client library.

Because of Issue #44 you must use forceChunkSize option.

Identifiers

In some cases an identifier is needed for the uploaded file when the client side library does not provide one. This identifier is important for resumable uploads as the library has to be able to check the status of the given file for a specific client. Without the identifier collisions can happen.

Service Driver name
Session identifier session
Auth identifier auth
NOP identifier nop

Session identifier

This identifier uses the client session and the original file name to create an identifier for the upload file.

Auth identifier

This identifier uses the id of the authenticated user and the original file name to create an identifier for the upload file.

It will throw UnauthorizedException when the user is unauthorized. However, it is still recommended using the auth middleware.

NOP identifier

This identifier uses the original file name to create an identifier for the upload file. This does not abstract the file identifier which can be useful for testing.

Contribution

All contributions are welcomed for this project, please refer to the CONTRIBUTING.md file for more information about contribution guidelines.

License

This product is licensed under the MIT license, please refer to the License file for more information.

coding-socks/laravel-upload-handler 适用场景与选型建议

coding-socks/laravel-upload-handler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.3k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2020 年 11 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 coding-socks/laravel-upload-handler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 coding-socks/laravel-upload-handler 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 3.3k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 6
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-11-14