4

I recently came across this question and answer https://stackoverflow.com/a/44926557/12322095 regarding Flask file uploads.

This worked perfectly fine until I uploaded an image with the same name again. It didn't change the image or overlayed it.

My question here is what if a user uploads an image with the same name, is there any way we could show an error message or maybe automatically change the name to something else.

For automatic changing the name, I researched and it can be done via resolve_conflict but I couldn't understand how to implement it.

v25
  • 7,096
  • 2
  • 20
  • 36
Tamanbir
  • 76
  • 7
  • Hey there :D to be short there is. For the longer answer, pls follow the rules for a proper question. It's not the solution that needs to be code only, but your question must contain some too. – Henry Harutyunyan Apr 30 '20 at 22:48
  • @HenryHarutyunyan sorry i am new here and i did not want to Duplicate it. so that's why gave a reference. please help if can – Tamanbir Apr 30 '20 at 22:50
  • I see, but still, you need to show some work done. How do you use it? It's okay to refer to other issues, but you need to show what you've done and what issues you have. Pls if possible post here part of the code that uploads the image. – Henry Harutyunyan Apr 30 '20 at 22:54
  • One option is to use `os.path.exists(os.path.join(app.config['UPLOAD_FOLDER'], filename))` but again, idk where you'll need to put it since there isn't any implementation details from you. – Henry Harutyunyan Apr 30 '20 at 22:55
  • @HenryHarutyunyan my code is ditto as the the reference – Tamanbir Apr 30 '20 at 22:56
  • ok I see. This might be useful https://stackoverflow.com/help/how-to-ask – Henry Harutyunyan Apr 30 '20 at 22:56

1 Answers1

6

my code is ditto as the the reference

You need to create some kind of uniuqe ID to append to the filename, before saving the file.

This can be done with something like:

from uuid import uuid4
def make_unique(string):
    ident = uuid4().__str__()
    return f"{ident}-{string}"

Which adds an unique UUID to the beginning of the string:

>>> make_unique('something.txt')
'f374b80c-b09e-468f-adc6-3d9df175cee7-something.txt'

To use this in the upload code, just run the filename through that function before you save. Be sure to put the filename through the secure_filename function first though:

        if file and allowed_file(file.filename):
            original_filename = secure_filename(file.filename)
            unique_filename = make_unique(original_filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], unique_filename))

Although this works for the purpose of avoiding duplicates, in a larger application you may wish to extend this approach.

If you store the values of original_filename and unique_filename in the database, then this allows you to do the following in the download route:

from flask import send_file
# ...
f = os.path.join(app.config['UPLOAD_FOLDER'], unique_filename)
send_file(f, attachment_filename=original_filename)

This has the advantage that the file is stored on your server with a unique identifier, but the user would never know this, as the file is served back to them with the originally uploaded filename.

In fact you may wish to go further, and simply save the file on your end with the UUID string, rather than appending the filename; instead of using the make_unique function above, change that third line to:

unique_filename = uuid4().__str__()

This will still serve the file with the correct mimetype, as send_file guesses the mimetype based on the provided attachment_filename.

v25
  • 7,096
  • 2
  • 20
  • 36
  • [Is it safe to turn a UUID into a short code? (only use first 8 chars)](https://stackoverflow.com/a/4568766). No – Smart Manoj Jul 03 '21 at 12:03
  • @SmartManoj Thanks for pointing this out. I've edited my answer to account for this :) – v25 Oct 23 '21 at 18:03