Jump to content

Custom Upload Names


adrian

Recommended Posts

Ok, sorry for the delay on this. I have added the ability to rename files uploaded via the API. It works on my testing using something simple like:

$page->images->add("/local_path_to_image/image.jpg"); 

$page->of(false);
$page->save();

I have attached a revised version of the module, rather than pushing to github for the moment. Please let me know if it works ok for you.

I am not sure how your upload form is working, but at the moment the module will rename the file in its original uploaded location before copying it to the final ID based folder under /assets/. I am not sure if this will work in all scenarios, but I had to use a "before" hook on "Pagefile::install" so that I could still change the filename using: 

$event->setArgument(0, $newFilename);

Using a before instead of after hook means that I am working with the file in its original location. It also means the module can't handle images from remote servers with full http:// paths. Does anyone have any ideas how I might be able to rename after it has been move to the assets/xxxx/ folder? None of the InputfieldFile hooks get called with API uploads, so they are out. I am thinking maybe I might need Pagefile::setFilename hookable, but I'd love to hear any ideas on how to do this.

On an unrelated note, I have also added the ability to drag/drop sort the rules in the module settings, making it easier to change the order as needed, which can be important if you have a lot of very specific rules that need to take precedence over more general ones later in the order.  

CustomUploadNames.zip

  • Like 2
Link to comment
Share on other sites

One more tweak this morning. There is now the option to preserve the uploaded filename for certain rules. This will allow you to set a general renaming rule for your entire site, but then add a rule for a specific page/template/field that does not rename the uploaded file. Just simply build the rule, but leave the Filename Format field empty.

I am not sure if this is what @peterfoeng was looking for in his post above, but hopefully people will find it useful regardless.

For the moment, this new version is only available as the attachment here.

CustomUploadNames.zip

  • Like 1
Link to comment
Share on other sites

Hey Ryan,

I will update the GitHub repo for sure.

I was hoping to improve the hook for uploading via the API first though - I'd really like to delay the rename until after the file has been copied into the assets/xxxx folder so I am not messing with any file manipulation code that a front-end form might use. If it uploads the file to a temp directory and then makes the api call to install the file, eg: 

$page->images->add("/local_temp_path_to_image/image.jpg");

then the form won't know the new filename and so won't be able to delete the file from the temp location. Also, some of the front-end upload scripts posted to this forum suggest moving the file to the final assets/xxxx folder before using ->add, in which case the Pagefile::install won't even be triggered. I'd also like to be able to support images being added from an external URL.

Any thoughts on how best to achieve all these requirements? My post #28 above talks more about the issues with Pagefile::install and the need to run it as a before hook, but the problems that also causes. I am sure I must be missing an obvious option.

Link to comment
Share on other sites

Adrian, what about just using the Pagefile::rename function from a hook after Pagefile::install ? I may not be understanding the issue in full. But couldn't you just do something like this:

public function hookAfterPagefileInstall($event) {
  $pagefile = $event->object;
  $pagefile->rename('filename.' . $pagefile->ext); 
}
  • Like 1
Link to comment
Share on other sites

Thanks Ryan - very easy and effective. I didn't think about the rename function - was wondering how to rename the file as well as the pagefile in one easy process.

I have pushed a new version to GitHub which uses this for API uploads and as a result can now support renaming files that are added from remote URLs.

I think this should now work in all API / form scenarios, but if anyone comes across any issues, please let me know.

  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...

Hi everyone,

Another overhaul today. A few bug fixes and a bunch of new features. I have updated the first post here to detail them better, but key things are the ability to make use of the following variables in the filename format: $page, $template, $field, and $file.

To accommodate some of possibilities this opens up, eg: $file->description, there is new logic to handle renaming after page save if the filename format can't be properly evaluated at upload because the field isn't available yet, or is still blank. If possible, renaming will still occur during upload - "on save" is just a fallback if needed.

Another key addition is the ability to give each rule the option to rename the file again each time the page is saved. There are obvious issues with this if images or files are being linked directly by their URL, so care is needed with this option checked. It does allow for some incredible flexibility though. You could even have a field in your template called "Filename" allowing the user to rename the file whenever they want. In most scenarios of course this would not be at all desirable, but it suited my needs, so thought it might come in handy for someone else as well :) If nothing else, it might be useful during development, but could be disabled once the site is live.

Let me know if you find any bugs with this new functionality.

  • Like 3
Link to comment
Share on other sites

  • 6 months later...

How should this be modified to have Datetime field in its output format instead of Unix timestamp in "Filename Format"?

I have just committed an update that makes this work as expected.

Because in the admin panel outputformatting is off by default, it needed to be manually turned on. I don't think there should be a problems with doing this in Custom Upload Names and some quick testing shows that it seems to be working fine, but please let me know if you discover any weird side-effects.

  • Like 1
Link to comment
Share on other sites

I also just added another formatting option that allows you to include the current date time in the filename, eg:

{$page->name}-[Y-m-d_H-i-s]-suffix

Anything inside square brackets will be treated as a php date format string and will be applied to the date/time when the file is uploaded and renamed.

You could instead use $page->created or $page->modified, but since these don't have a configurable output format, this seemed like a more flexible approach.

I think this should be a useful addition in several scenarios.

Link to comment
Share on other sites

Hi,

I think I found another little thingie.

Yesterday I've decided to use an image-field in a repeater, custom name was enabled. I think you already know where I'm going :-)

The image didn't get saved, disabling did help.

Greetings.

Yeah, unfortunately I do know about this already - was just hoping it wouldn't come up :)

I made an initial attempt to fix it, but couldn't figure out a solution at the time. Then the new PageTable field was developed which Ryan recommends over repeaters in almost all scenarios so I didn't worry about it too much.

I'll take another look and see what I can do, but you really should try PageTables - so much nicer (and less problematic) than repeaters :)

Link to comment
Share on other sites

  • 1 month later...

Supposed I upload multiple files at a time and want them to be rename to the following format format:

mysite-[Y-m-d]-{$file->description}

The problem is that all the files get renamed immediately (before hitting the save button) like this:

tmp-filename.pdf
tmp-filename-1.pdf
tmp-filename-2.pdf
...

Now, how can I tell which tmp-filename belongs to which original file? Sure, I could still open the files by clicking on their filename. But what if the new file format would only be applied after hitting the save button?

Link to comment
Share on other sites

Hi charger - sorry, I am not sure what the problem actually is. Because you have chosen to use $file->description the module has to use the tmp-filename option until the page is saved, when the description field is available. If you used $page-name or something else that is already available when the file is uploaded, it will rename fully as soon as it is uploaded. Maybe you have already figured that out though? 

Why do you need to know which file is which before you save the page? Once the page is saved the names will match the entered descriptions. 

Perhaps if you could explain your usage scenario I might be able to come up with a solution for you.

I am on vacation for the next two weeks though, starting in a few hours, so I might not be able to do anything until then, unless a clear idea emerges and it is easy to implement.

I guess I could go with renaming on page save, but that always seemed less elegant - shouldn't be too hard to make it an option though.

Link to comment
Share on other sites

The problem is that if I upload 10 files at a time and they all get renamed to tmp-filename.pdf before saving, then I have no chance to see the original filename and thus write the according description to it.

An example: I upload a file that is originally named 30_10_2014_EventHappyMonday.pdf. In order to know what to fill in in the description field (in this case it would be "Event Happy Monday"), it helps a lot if I still see the original filename and not tmp-filename.pdf.

Enjoy your holidays!

Link to comment
Share on other sites

Sorry now I understand - it's the writing of the description that is the problem - makes total sense :)

I often use this module with images (so not an issue), and when I do it with PDFs and other files I haven't ever used $file->description so I haven't had this issue yet.

The tricky thing about trying to fix this is that filename formats that use fields that aren't available at upload (like description) need to be renamed on save, but if you don't want the name to also change on each future file save (which you wouldn't given that you are including the date in the filename), the module needs to know it is the first time the file has been saved, which is why I use the tmp-filename intermediate renaming. Only files named tmp-filename get renamed on save. All others are ignored unless the "Rename on Save" option is checked.

It's almost 2am here and this is not a simple fix, so I am sorry, but I think it will have to wait a couple of weeks, but I'll definitely put it on my list and I'll let you know when I have a solution.

Link to comment
Share on other sites

Hey charger - woke up with an obvious solution. I just committed an update that should take care of your needs.

Now the temporary filename is: original-filename.upload.tmp.pdf

Hope that works well for you.

Thanks for letting me know about this.

  • Like 3
Link to comment
Share on other sites

Not sure about actually dreaming, but definitely in that blurry time while falling asleep and then lying there after just waking up - brain seems more clear and tries to actually figure out the problem, rather than the trial and error approach that can sometimes happen when you're at the computer trying to get something to work.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...