audentio / laravel-uploader
1.2.4
2024-07-09 21:08 UTC
Requires
- php: ^8.0
- ext-fileinfo: *
- audentio/laravel-base: ^1.1|^2.0
- illuminate/contracts: ^9.0|^10.0
- illuminate/support: ^9.0|^10.0
- intervention/image: 3.0.0-alpha.3
- ksubileau/color-thief-php: ^2.0
This package is auto-updated.
Last update: 2024-09-09 21:26:38 UTC
README
安装
composer require audentio/laravel-uploader
入门指南
- 运行
php artisan vendor:publish
并选择库选项 - 创建一个
Upload
模型并实现\Audentio\LaravelUploader\Models\Interfaces\UploadModelInterface
接口,使用\Audentio\LaravelUploader\Models\Traits\UploadModelTrait
特性,创建 GraphQL 类型/资源,并在 config/audentioUploader.php 中引用这些 - 在 routes/api.php 中,将
\Audentio\LaravelUploader\LaravelUploader::routes();
添加到末尾以注册上传路由
为新的内容类型设置上传
模型
确保在您的模型类上实现 Audentio\LaravelUploader\Models\Interfaces\UploadContentInterface
接口,并使用 Audentio\LaravelUploader\Models\Traits\UploadContentTrait
特性。
最后,您需要在模型上定义一个 _getUploaderConfig
方法,用于定义所有上传内容字段和变体。例如
protected function _getUploaderConfig(): array { return [ 'thumbnail' => [ 'variants' => [ 'thumb' => [ 'width' => 528, 'height' => 280, ], ], ], ]; }
每个内容字段允许的选项包括 allowed_types
、max_files
、max_size
和 variants
。
每个变体允许的选项包括 type
(fill
或 fit
)、width
和 height
。
GraphQL 资源
字段
在您的 GraphQL 资源的 getOutputFields
和 getInputFields
中,您需要添加以下内容
输出字段
Model::addUploadGraphQLOutputFields($this->getGraphQLTypeName(), $fields);
输入字段
Model::addUploadGraphQLInputFields($baseScope, $fields);
突变
设置上传参数(在初始化模型之前)
$uploads = Model::setupUploadArgs($args['model']);
验证上传(在保存模型之前)
if (!$model->validateUploads($uploads, $errors)) { $this->validationError($info, $errors, 'model'); }
保存上传(在保存模型后,最好在同一事务中)
$model->attachUploads($uploads);
上传文件
获取上传
GET /api/upload/{id}
创建上传
POST /api/upload
头部
参数
upload
上传的文件content_type
内容类型(通常应与 GraphQL 类型名称匹配)content_field
上传字段名称(将在与uploads
关联的 GraphQL 类型输入字段中定义)
响应示例
{ "success": true, "message": null, "payload": { "upload": { "id": "2e5a78f3-2dfe-4298-a73d-7de03937541a", "content_type": "Model", "content_field": "thumbnail", "variants": [ { "file_hash": "75daeb8e884c63a7d3f3ecc3e325f606", "file_type": "image\/png", "file_size": 52375, "width": 276, "height": 146, "variant": "thumb", "url": "https:\/\/bucket-name.s3.us-west-1.amazonaws.com\/Uploads\/Model\/thumbnail\/2e5a78f3-2dfe-4298-a73d-7de03937541a\/thumb_fileName.png" } ] } } }