Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Processing file uploads before object is saved

I've got a model like this:

class Talk(BaseModel):
  title        = models.CharField(max_length=200)
  mp3          = models.FileField(upload_to = u'talks/', max_length=200)
  seconds      = models.IntegerField(blank = True, null = True)

I want to validate before saving that the uploaded file is an MP3, like this:

def is_mp3(path_to_file):
  from mutagen.mp3 import MP3
  audio = MP3(path_to_file)
  return not audio.info.sketchy

Once I'm sure I've got an MP3, I want to save the length of the talk in the seconds attribute, like this:

audio = MP3(path_to_file)
self.seconds = audio.info.length

The problem is, before saving, the uploaded file doesn't have a path (see this ticket, closed as wontfix), so I can't process the MP3.

I'd like to raise a nice validation error so that ModelForms can display a helpful error ("You idiot, you didn't upload an MP3" or something).

Any idea how I can go about accessing the file before it's saved?

p.s. If anyone knows a better way of validating files are MP3s I'm all ears - I also want to be able to mess around with ID3 data (set the artist, album, title and probably album art, so I need it to be processable by mutagen).

like image 554
Dominic Rodger Avatar asked May 09 '10 18:05

Dominic Rodger


People also ask

What is upload vulnerability?

What are file upload vulnerabilities? File upload vulnerabilities are when a web server allows users to upload files to its filesystem without sufficiently validating things like their name, type, contents, or size.

How do you validate upload files?

Using JavaScript, you can easily check the selected file extension with allowed file extensions and can restrict the user to upload only the allowed file types. For this we will use fileValidation() function. We will create fileValidation() function that contains the complete file type validation code.

How does multipart file upload work?

Multipart upload is the process of creating an object by breaking the object data into parts and uploading the parts to HCP individually. The result of a multipart upload is a single object that behaves the same as objects for which the data was stored by means of a single PUT object request.

Where does Django save uploaded files?

MEDIA_ROOT We have used the same join functions in Django static files tutorial that we have passed in the file path of media directory. This directory will store all the files a user uploads in Django. Although you can directly give the path as a string in MEDIA_ROOT.


1 Answers

You can access the file data in request.FILES while in your view.

I think that best way is to bind uploaded files to a form, override the forms clean method, get the UploadedFile object from cleaned_data, validate it anyway you like, then override the save method and populate your models instance with information about the file and then save it.

like image 142
Davor Lucic Avatar answered Nov 15 '22 04:11

Davor Lucic