承接 heryfitiavana/rcu-laravel 相关项目开发

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

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

heryfitiavana/rcu-laravel

最新稳定版本:v1.0.0

Composer 安装命令:

composer require heryfitiavana/rcu-laravel

包简介

Resumable chunk upload for Laravel

README 文档

README

Resumable chunk upload for Laravel.

Installation

Via composer :

composer require heryfitiavana/rcu-laravel

Usage

CSRF

Add the csrf token to the Uploader in frontend

// get csrf token from meta tag : <meta name="csrf-token" content="{{ csrf_token() }}">
const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute("content");

new Uploader()
    .setHeaders({
        "X-CSRF-TOKEN": csrfToken,
    })
    /// ... others

Defining Routes

Add the following routes to your routes/web.php file:

use Heryfitiavana\RCU\Controllers\UploadController;

Route::get('/uploadStatus', [UploadController::class, 'uploadStatus']);
Route::post('/upload', [UploadController::class, 'upload']);

Default RCU configuration : RCUConfig

Custom RCU configuration

Custom RCU configuration

You can customize the package's behavior by defining a custom configuration array. Here's an example:

$customConfig = [
    "store" => new JsonStoreProvider('rcu/uploads.json'),
    "tmpDir" => "rcu/tmp",
    "outputDir" => "rcu/output",
    "onCompleted" => function ($data) {
    },
];

Use the configuration

Go to app/Providers/AppServiceProvider.php and add the following code

use Illuminate\Support\ServiceProvider;
use Heryfitiavana\RCU\Controllers\RCUControllerFactory;
use Heryfitiavana\RCU\Controllers\UploadController;
use Heryfitiavana\RCU\StoreProviders\JsonStoreProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        // other ...
        $this->app->singleton(UploadController::class, function ($app) {
            $customConfig = [
                "store" => new JsonStoreProvider('rcu/uploads.json'),
                "tmpDir" => "rcu/tmp",
                "outputDir" => "rcu/output",
                "onCompleted" => function ($data) {
                },
            ];
            return RCUControllerFactory::createController($customConfig);
        });
    }

    public function boot()
    {
    }
}

API

RCUConfig

[
    "store" => new JsonStoreProvider('rcu/uploads.json'),
    "tmpDir" => "rcu/tmp",
    "outputDir" => "rcu/output",
    "onCompleted" => function ($data) {
    },
]

store

  • Type: StoreProviderInterface
  • Default: JsonStoreProvider

The store parameter is used to store information about the upload, such as the number of the last uploaded chunk, the total number of chunks, etc. The default store is JSON, but you can implement your own by implementing the StoreProviderInterface.

tmpDir

  • Type: string
  • Default: rcu/tmp

Directory to save all binary chunks.

outputDir

  • Type: string
  • Default: rcu/output

Directory to save the complete file.

onCompleted

  • Type: (data: ['outputFile' => string, 'fileId' => string]) => void

This callback function can be used to perform any additional actions or operations after the upload is completed, such as updating a database record or sending a notification.

  • outputFile: Path of the uploaded file.
  • fileId: The ID of the file used to identify the upload. This is specified from frontend.

StoreProviderInterface

Upload = [
    "id" => string,
    "chunkCount" => int,
    "lastUploadedChunkNumber": int,
    "chunkFilenames": string[],
];

interface StoreProviderInterface
{
    public function getItem(String $id) : Upload | undefined;
    public function createItem(String $id, Int $chunkCount): Upload;
    public function updateItem(String $id, Upload $update) : Upload;
    public function removeItem(String $id) : void;
}

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-06-28