Package Data | |
---|---|
Maintainer Username: | reg2005 |
Maintainer Contact: | cmp08@ya.ru (reg2005) |
Package Create Date: | 2017-01-22 |
Package Last Update: | 2017-02-05 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-08-31 15:05:49 |
Package Statistics | |
---|---|
Total Downloads: | 83 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 1 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 0 |
Upload, validate, storage, manage by API for Laravel 5.1/5.2
config/app.php
'providers' => [
'reg2005\UploadManager\UploadManagerServiceProvider',
Intervention\Image\ImageServiceProvider::class
]
...
'aliases' => [
'Image' => Intervention\Image\Facades\Image::class
]
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;
}
}
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