Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file size per file type? #304

Closed
7 of 10 tasks
Guanche opened this issue Mar 21, 2018 · 4 comments
Closed
7 of 10 tasks

file size per file type? #304

Guanche opened this issue Mar 21, 2018 · 4 comments

Comments

@Guanche
Copy link

Guanche commented Mar 21, 2018

In raising this issue, I confirm the following (please check boxes):

  • I have read and understood the Wiki. Especially deploy and configuration articles.
  • I have checked that the bug I am reporting can be replicated, or that the feature I am suggesting isn't already present.
  • I have checked the pull requests tab for existing solutions/implementations to my issue/suggestion.
  • I realise that server-side connectors are provided by various contributors. The implementations are vary due to programming language features/limitations or other factors. Thus a particular connector may not implement, or partially implement, the API features.
  • I realise that any changes in configuration options and/or plugin parameters affect the plugin behavior. I specified all the differences from defaults in details.

I use the following server-side connector (check one):

  • PHP connector by @servocoder

My familiarity with the project is as follows (check one):

  • I have never used the project.
  • I have used the project briefly.
  • I have used the project extensively, but have not contributed previously.
  • I am an active contributor to the project.

Is it possible? To set up a filesize limit in bytes by different file types?
It would be useful. For example, one size limit for images, another one for videos, etc.

Thanks.

@psolom
Copy link
Owner

psolom commented Mar 21, 2018

This feature isn't implemented and it's quite specific to implement.

The good news is that you can solve is by yourself.

  1. Extend UploadHandler and Storage classes of the storage you would like to utilize. In the examples below I extend local storage classes.
namespace Custom;

use RFM\Repository\Local\Storage as LocalStorage;

class Storage extends LocalStorage
{
    /**
     * Initiate uploader instance and handle uploads.
     *
     * @inheritdoc
     */
    public function initUploader($model)
    {
        return new \Custom\UploadHandler([
            'model' => $model,
            'storage' => $this,
        ]);
    }
}
namespace Custom;

use RFM\Repository\Local\UploadHandler as LocalUploader;

class UploadHandler extends LocalUploader
{
    public function validate($uploaded_file, $file, $error, $index)
    {
        $extension = pathinfo($file->name, PATHINFO_EXTENSION);

        switch ($extension) {
            case 'pdf':
                $maxFileSize = 2000000;
                break;
            case 'doc':
                $maxFileSize = 1000000;
                break;
            default:
                $maxFileSize = $this->options['max_file_size'];
        }

        if ($maxFileSize && $file->size > $maxFileSize) {
            $file->error = ['UPLOAD_FILES_SMALLER_THAN', [round($maxFileSize / 1000 / 1000, 2) . ' Mb']];
            return false;
        }

        return parent::validate($uploaded_file, $file, $error, $index);
    }
}
  1. Open your entrypoint script (connectors/php/filemanager.php by default) and replace the storage class:
$local = new \Custom\Storage($config);

instead of:

 $local = new \RFM\Repository\Local\Storage($config);
  1. You can see I use Custom namespace. If you use composer as well you have to autoload your new classes. In my case I put both new classes into the connectors/php/customization folder and added the following to the composer.json file:
  "autoload": {
    "psr-4": {
      "Custom\\": "connectors/php/customization"
    }
  }

Don't forget to run composer dumpautoload to apply new autoload path.

Of course you can simply use include/require directives alternatively, without composer.

  1. Important note.

I found a nasty bug in the RFM plugin code, that doesn't allow to display server-side errors which happen while upload, see #305 (kind of ones that fires in the extended UploadHandler class). I have just fixed this bug and it will be included into the next release. But for now you have to clone/dowload the latest version of RFM from the master branch.

GOOD LUCK

@psolom
Copy link
Owner

psolom commented Mar 21, 2018

One more note.

The upload.fileSizeLimit server-side configuration option will be used as a general upload limit for file types which are not listed in the extended UploadHandler class.

If you don't want to set a general upload limit you can set this option to false.

$config=[];
$config['upload']['fileSizeLimit'] = false;

$local = new \Custom\Storage($config);

@Guanche
Copy link
Author

Guanche commented Mar 23, 2018

Thank you very much, master.
It will be sure very usefull for everyone.

I'm totally new with all of this (the plugin, github, composer...), even with some of the php used too. Learning everyday.

Greetings from the Canaries.

@psolom
Copy link
Owner

psolom commented Mar 23, 2018

Sure, good luck in learning

@psolom psolom closed this as completed Mar 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants