how to compare uploaded file in already exist in django model - python

I am getting a file from the user and filter data from the model if the user uploaded file exists or not, but I am getting "None" while filtering.
from .models import SomeModel
uploaded_file = request.FILES.get('upload_file')
model_data = SomeModel.objects.filter(my_file=uploaded_file)
if model_data == None:
someModel.objects.create(my_file=uploaded_file)
else:
print('file already exist.')
print(model_data) # prints None
I have uploaded the same file too many times but it always creates new data in the model.
I know every time when uploaded file Django creates a new file in the media folder.
how can I filter if uploaded_file already exists in the model?

I would not suggest both of these approaches. Why?
Because multiple users may upload files with same name. Even same user can upload it multiple times with same name (So many users simply upload Untitles.xlsx.
Every time a file is uploaded, it is stored in a temporary location for the GET dictionary (try /tmp on linux). So your temporary name/location may be different if the same named file exists.
The intention might be to reupload the same file. In that case you might want to update.
So a safer option would be to get the checksum of the file (md5/sha1/sha256) and even store that in the database. That will tell you if the file is indeed the same.
You can check the currently uploaded file for checksum.

Related

How should I handle duplicate filenames when uploading a file with Flask

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.
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.

tinys3 not recognizing folder on upload (django) (s3)

My code:
csv = pd.read_html(table)[0].to_csv('datasource_files/testtable7.csv',index=False,header=False)
conn = tinys3.Connection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY,endpoint='s3-us-west-2.amazonaws.com/')
csv_file=open('datasource_files/testtable7.csv')
csv_file=open('datasource_files/testtable7.csv','rb')
csv_name= 'datasource_files/testtable7.csv'
conn.upload(csv_name,csv_file,'datafix1')
ds = DataSource.objects.create(file=csv_name,datatype="CSV",creator=mike, title="title",description="desc")
DataSource is a Django model, file is a models.FileField(). Currently, ds.file is http://datafix1.s3-us-west-2.amazonaws.com/datasource_files/testtable7.csv but that file doesn't exist. In order to access the file uploaded I need to go to http://datafix1.s3-us-west-2.amazonaws.com//datasource_files/testtable7.csv (which has an empty directory appended at the beginning of the pathname, because tinys3 is not recognizing the already existing (important) "datasource_files" folder (at least I believe that is the reason, I may be wrong). Can anyone help? Thanks in advance.
I used boto3 instead, works like a charm

how to get uploaded file name in django

Problem Background
I am new to django. I am trying to upload the file from client and save it.
For this pupose i have created below model.
from django.db import models
class UploadFile(models.Model):
uploadfile = models.FileField(upload_to='toProcess/')
I am using this model as below to save the file.
newfile = UploadFile(uploadfile = request.FILES['file'])
newfile.save()
It is saving file. But now I want to process saved file. In django, if the file with the same name alreday exists then it is adding some unique postfix to the original file name. I am happy with this approch and do not want to write a new method to create a unique filename.
Probelm-
How to get the new unique name calculated by django for a file?
Mean If I will upload a same file two times say "abc.pdf" then it will save the first uploaded file as "abc.pdf" and second uploaded file as "abc_somesuffix.pdf". How to know what is the name of saved file?
As far as I know, the filename is stored in the name attribute of the model field, in your case
newfile.uploadfile.name
and the path of the file is stored in
newfile.uploadfile.path
Please see the official Django docs for further reference, as well as many other SO Q&A (e.g. this one)
In case you want to adopt your own format for the file name you can specify a callable in the upload_to parameter of the model field, as explained here

Openshift how to validate file before saving into app-root/data

I'm trying to find a way to use the function "identify" from imagemagick without saving the image file in app-root/data. Basically I want to validate the image file before actually saving the file into the destination. If saved into app-root/data, i can easily just do this:
(temp, tempError) = (subprocess.Popen(['identify', '../data/' + filename + extension], stdout=subprocess.PIPE)).communicate()
But this would require the image to be uploaded first before identifying. Any ways to do this?
You should save the file into the /tmp directory first, then validate it, and if it passes, then move it to your ~/app-root/data directory.
You can check out this Filesystem Overview on the Developer Portal for more information: https://developers.openshift.com/en/managing-filesystem.html

Extract files from zip folder and store these files in blobstore

i want to upload zip folder from file input in form the i want to extract the contents of this uploaded zip folder,and store the contents (files)of this zip in the blobstore in order to download them after putting these files in one folder,but the problem is that i can't deal with the zip folder directly(to read it), i tried as this:
form = cgi.FieldStorage()
file_upload = form['file']
zip1=file_upload.filename
zipstream=StringIO.StringIO(zip1.read())
But the problem still that i can't read the zip as previous,also i tried to read zip folder directly like this:
z1=zipfile.ZipFile(zip1,"r")
But there was an error in this way.Please can any one help me.Thanks in advance.
Based on your comment, it sounds like you need to take a closer look at the cgi module documentation, which includes the following:
If a field represents an uploaded file, accessing the value via the value attribute or the getvalue() method reads the entire file in memory as a string. This may not be what you want. You can test for an uploaded file by testing either the filename attribute or the file attribute. You can then read the data at leisure from the file attribute...
This suggests that you need to modify your code to look something like:
form = cgi.FieldStorage()
file_upload = form['file']
z1 = zipfile.ZipFile(file_upload.file, 'r')
There are additional examples in the documentation.
You don't have to extract files from the zip in order to make them available for download - see this post for an example of serving direct from a zip. You can adapt that code if you want to extract the files and store them individually in the blobstore.

Categories

Resources