bolt-cms

Upload Files to custom location before date folder


I have built a custom contenttype with an image field in Bolt 2.0.

    image:
        type: image

If no folder is specified the uploaded file goes to a folder named by the year-month

Result: 2014-11/myFileName.jpg

With the tag upload I can change this to something else.

    image:
        type: image
        upload: "News/" 

Result: News/myFileName.jpg

Is it possible to get the year-month folders after my costom path?

Result: News/2014-11/myFileName.jpg


Solution

  • The answer to this is yes, but not very simply so if you want a configurable way to do this you need to wait for 2.1 of Bolt where we're going to add variables to the upload: setting.

    If you don't mind setting up your own bootstrap file and modifying the application then you can do it now.

    The date prefix is generated by the $app['upload.prefix'] setting and currently returns the date string. What you need to do to modify this is change this to your own closure. I haven't tested this on a project so tweak if needed but after:

    $app->initialize();
    
    // Redefine the closure
    $app['upload.prefix'] = function() {
        $setting = $app['request']->get('handler');
        $parts = explode('://', $setting);
        $prefix = rtrim($parts[0], '/') . '/';
        return $prefix.date('Y-m') . '/';
    
    };
    $app->run();
    

    What we're doing here is reading the setting which is passed in the request and then concatenating the default date prefix onto the end of it.

    As mentioned earlier 2.1 will see variable support introduced into the paths so options like

     upload: news/{%month%}/{%day%}
     upload: uploads/{%contenttype%}/{%id%}
    

    will be easily definable in the contenttypes.yml file so If you don't mind waiting for a couple of months then this is obviously much simpler.