-
-
Notifications
You must be signed in to change notification settings - Fork 182
Have More Control On Uploads
Muah edited this page Dec 2, 2019
·
18 revisions
up until now you were limited to what come pre-made with the package, but what if you wanted to do some extra operations to the uploaded file ?!! for example
re-encode a video file,
convert an audio file,
change the width & height of an image,
etc..
so to achieve that a new config key is added
// before
'controller' => '\ctf0\MediaManager\App\Controllers\MediaController',
// after
'controller' => '\App\Http\Controllers\MyAwesomeController',
which you can use to switch the package controller to a custom one of your own, ex.
<?php
namespace App\Http\Controllers;
use ctf0\MediaManager\App\Controllers\MediaController;
class MyAwesomeController extends MediaController
{
// ...
}
we also have a new method to allow / disallow uploading the file
this method controls all upload typesfile upload, url upload, image editing upload
/**
* allow/disallow user upload.
*
* @param (Symfony\Component\HttpFoundation\File\UploadedFile || null) $file
*
* @return boolean
*/
protected function allowUpload($file = null)
{
return true;
}
auto optimize the uploaded file same as the middleware but with more control
- if you were using any auto image optimizer, the user was going to stay on hold until all the uploads are optimized just to find out that he is not allowed to upload any, therefor optimizeUpload() is added so you can do exactly the same but after you've checked for the allowance.
/**
* do something to file b4 its saved to the server.
*
* @param (Symfony\Component\HttpFoundation\File\UploadedFile) $file
*
* @return $file
*/
protected function optimizeUpload(UploadedFile $file)
{
app(\Spatie\ImageOptimizer\OptimizerChain::class)->optimize($file->getPathname());
return $file;
}