Renaming uploaded files

I have recently been uploading PDF files of academic presentations (to this topic on seasonal climate forecasting as it happens).

The file naming convention I utilize is:

  • 0000-leadauthor-[etal-]title-title-title.pdf

So far so good. Until I discover I that I have a lead author’s name wrong. But when I try to upload the same file under a corrected file name, discourse notices that the file is otherwise identical — I guess there is some hashing going on here— and supplies a reference to the original file instead, with the file name unchanged and still incorrect.

My work around is to alter the following setting to 1 (the default is 48), remove all references in the markup, and wait until the resulting file expunging has occurred:

  • Settings > Files > clean orphan uploads grace period hours

I also tried setting this particular parameter to 0 but that did not seem to work. In which case, the tool tip might better be modified to read: “To disable the grace period set to 0”. Or alternatively as appropriate: “To enable near immediate effect set to 0”.

I cannot tell how often this kind of maintenance is actually run but I was presuming hourly? Maybe it is daily instead? That would be useful to know too.

TIA for any assistance about how to speed up this file renaming process! I do have a work around but it is rather clumsy and slow.

Hourly…

https://github.com/discourse/discourse/blob/master/app/jobs/scheduled/clean_up_uploads.rb

Not being familiar with ruby, but that also looks like 0 means 1. Maybe the parameter tip should also read: “A value of 0 is interpreted as 1.” or something similar. Particularly as zero often has the special meaning of disable in other circumstances. Thanks @neounix.

I think what you want to do is update the upload record at the rails console, if you’re self hosted. I doubt there’s a way to do that with the API, but haven’t looked

We are self-hosted. I have been offered shell account access, but have not followed that up. Tx for the hint.

Wait. I’m not quite sure what you’re trying to do. Is it to change the name people see in the posts in which they’re uploaded? If that’s the case, then maybe you want to do something like

rake posts:remap["old-name.pdf","newname.pdf"]

As described in Replace a string in all posts. I think that for your purposes the original filename in the Upload model doesn’t really matter.

But you can change that with

u=Upload.find_by(original_filename: 'mypretty-w9.pdf')
u.original_filename='better-name.pdf'
u.save

I tried rebaking a post with that upload and nothing happened.

EDIT:

def rename_upload(old_name, new_name)
  u=Upload.find_by(original_filename: old_name)
  u.original_filename=new_name
  u.save
end

If you paste that, then you can

rename_upload('name.pdf','better-name.pdf')

It’ll fix it so that the downloads get the better filename.

In my former life as an academic I had my specific way of naming my PDFs that was different from anyone else’s; I’m pretty sure that my wife has her own. My guess is that no one will like your filenames even when you get them “right”. :wink:

Not what I was trying to do. The leadauthor string was part of the file name. Nothing more. But when I revised the file name (because my understand of the family naming conventions for Spanish academics is severely limited) and tried to upload the renamed file, discourse noticed that it had an identical file in its manifest and swapped to the original target and file name. I guess that could be considered a feature? :roll_eyes: It was not until I had purged that file from the site database that I could upload the one with the corrected file name. I really appreciate your help and interest @pfaffman.

Where do you see the “filename”? The actual filename in the filesystem is a random string.

After testing in my instance, you can change the filename on [filename.pdf|attachment](upload://filehash.pdf) and, while it shows the filename.pdf on the post, it still downloads as the original filename. I guess it’s the same thing he wants: to rename the filename used when downloading it.

Not really critical, but I’d also like to be able to rename uploads.

Oh. Ok. So the three lines of rails code do what you want. It doesn’t change what you see in posts, but if you click the file it saves with the “better-name.pdf”.

2 Likes

Thanks to everyone who responded.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.