Question

I'm using Tempfile to store a generated PDF before uploading to a new destination.

    pdf_file = WickedPdf.new.pdf_from_string(msgbody)
    tempfile = Tempfile.new(['Bob', '.pdf'], Rails.root.join('public','pdf-test'))
    tempfile.binmode
    tempfile.write pdf_file
    tempfile.close

While this works fine, the resulting file names, eg- bob20140331-19260-1g6rzr1.pdf are not user friendly.

I understand that Tempfile creates a unique name and why, but I ultimately need to change the name to make it more intuitive/easier to digest for my users.

Is there a recommended way to do so? Even if its to simply remove the middle (19260)? Thanks for your time and assistance.

Was it helpful?

Solution

A Tempfile is used to create a temporary file with a unique file name, which will be cleaned up by the garbage collector or when the ruby interpreter exits.

Tempfiles behave like File objects, but I am not sure if you can rename files and if you can, if the automatic cleanup described above will still work. Additionally you might break the constraint of unique file names if you change the temporary file name manually.

I suggest creating an ordinary file and specify the entire name by yourself (the succ method can be helpful to prevent name clashes).

Another solution might be setting the file name during or after the upload process, you mentioned.

OTHER TIPS

Note sure if there is one with Tempfile, but can you not rename the file after creation time via FileUtils module? That way you could achieve that the file that was created still has a valid and user-friendly name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top