For the site I just created, I created a custom FileField, AudioFileField. In AudioFileForm now I want to check if the file is an audio file or not. For this, I use sox, commandlinetool, which I invoke through a subprocess. In the forms of the to_python function, I copied the code from ImageFileField:
if hasattr(data, 'temporary_file_path'):
file = data.temporary_file_path()
else:
if hasattr(data, 'read'):
file = StringIO(data.read())
else:
file = StringIO(data['content'])
check = subprocess.Popen([sox,'--i','-t','%s'%self.path], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
filetype = check.communicate()[0]
if not filetype:
raise forms.ValidationError('File is not an audiofile')
Thinking about this, I thought it might be useful to just make TemporaryFileUploadHandler used for AudioFileField. This will save me the trouble of writing my own code to create a temporary file. How can i do this?
marue source
share