-
-
Notifications
You must be signed in to change notification settings - Fork 184
Have More Control On Uploads
Muah edited this page Sep 16, 2018
·
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\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\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;
}
- note if you use any auto image optimizer, now the user will stay on hold until all the uploads are optimized just to find out that he is not allowed to upload any, therefor a new method optimizeUpload() is added so you can do exactly the same but after you've checked for the allowance.
auto optimize the uploaded file same as the middleware but with more control
/**
* 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;
}