reg2005 / laravel-upload-manager
Upload, validate, storage, manage by API for Laravel 5.1/5.2/5.3/5.4
1.0.2
2017-01-22 21:11 UTC
Requires
- intervention/image: ^2.3
- kalnoy/nestedset: ^4
- laravel/framework: ^5.1
This package is not auto-updated.
Last update: 2024-11-18 15:29:16 UTC
README
Upload, validate, storage, manage by API for Laravel 5.1/5.2
Requirement
- Laravel 5.1/5.2
Install
- composer require reg2005/laravel-upload-manager
config/app.php
'providers' => [
'reg2005\UploadManager\UploadManagerServiceProvider',
Intervention\Image\ImageServiceProvider::class
]
...
'aliases' => [
'Image' => Intervention\Image\Facades\Image::class
]
- php artisan vendor:publish --provider="reg2005\UploadManager\UploadManagerServiceProvider"
- php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"
- php artisan migrate
- Done
Usage
-
Upload and store a file.
use reg2005\UploadManager\UploadManager; class UploadController extend Controller { public function postUpload(Request $request) { $file = $request->file('avatar'); $manager = UploadManager::getInstance(); $upload = $manager->upload($file); $upload->save(); return $upload; } }
-
Fetch and store a file from a URL
use reg2005\UploadManager\UploadManager; class UploadController extend Controller { public function postUpload(Request $request) { $fileUrl = $request->input('url'); $manager = UploadManager::getInstance(); $upload = $manager->upload($fileUrl); $upload->save(); return $upload; } }
-
Update a upload object
use App\Upload; use reg2005\UploadManager\UploadManager; class UploadController extend Controller { public function postUpload(Request $request) { $uploadId = $request->input('id'); $file = $request->file('avatar'); $manager = UploadManager::getInstance(); $upload = Upload::find($uploadId); if($manager->update($upload, $file)) { $upload->save(); return $upload; } return ['result'=>false]; } }
-
Update a upload object from a URL
use App\Upload; use reg2005\UploadManager\UploadManager; class UploadController extend Controller { public function postUpload(Request $request) { $uploadId = $request->input('id'); $fileUrl = $request->input('url'); $manager = UploadManager::getInstance(); $upload = Upload::find($uploadId); if($manager->update($upload, $fileUrl)) { $upload->save(); return $upload; } return ['result'=>false]; } }
-
Validation
use reg2005\UploadManager\UploadManager; class UploadController extend Controller { public function postUpload(Request $request) { $file = $request->file('avatar'); $manager = UploadManager::getInstance(); $upload = $manager->withValidator('image')->upload($file); //加上验证组 if($upload) { $upload->save(); return $upload; } else { $errorMessages = $manager->getErrors(); //得到所有错误信息 $errorMessage = $manager->getFirstErrorMessage(); //得到第一条错误信息 throw new \Exception($errorMessage); } } }
-
Disk
use reg2005\UploadManager\UploadManager; class UploadController extend Controller { public function postUpload(Request $request) { $file = $request->file('avatar'); $manager = UploadManager::getInstance(); $upload = $manager ->withValidator('image') ->withDisk('selectel') // 储存到七牛磁盘里 ->upload($file); $upload->save(); return $upload; } }
-
$upload
use reg2005\UploadManager\UploadManager; class UploadController extend Controller { public function postUpload(Request $request) { $file = $request->file('avatar'); $manager = UploadManager::getInstance(); $upload = $manager ->withValidator('image') ->withDisk('localhost') ->upload($file, function($upload){ if($upload->size > 1024*1024) { $upload->disk = 'selectel'; } return $upload; }); $upload->save(); return $upload; } }
Configuration
-
config/upload.php
-
App\Upload
-
UploadStrategy.php
<?php namespace App\Extensions; use reg2005\UploadManager\UploadStrategy as BaseUploadStrategy; use reg2005\UploadManager\UploadStrategyInterface; class UploadStrategy extends BaseUploadStrategy implements UploadStrategyInterface { /** * @param $filename * @return string */ public function makeStorePath($filename) { $path = 'uploads/' . $filename; return $path; } /** * disk localuploads * @param $path * @return string */ public function getLocaluploadsUrl($path) { $url = url('uploads/' . $path); return $url; } /** * disk selectel * @param $path * @return string */ public function getselectelUrl($path) { $url = 'http://' . trim(\Config::get('filesystems.disks.selectel.domain'), '/') . '/' . trim($path, '/'); return $url; } }
config/upload.php
upload_strategy