Skip to content

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
{
    // ...
}


v3.0.4

we also have a new method to allow / disallow uploading the file
this method controls all upload types file 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;
}

v3.1.0

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;
}