def validate_file_extension(audiofiles): try: for file_ in audiofiles: content_type = file_.content_type if filename_has_valid_extension(str(file_)): ext = str(file_).rsplit('.', 1)[-1].lower() if ext == 'flac': # At least Safari and Firefox do not set the proper mime type for .flac files # (use 'application/octet-stream' instead of 'audio/flac'). For this reason we also allow # this mime type for flac files. if not content_type.startswith("audio") and not content_type == 'application/octet-stream': raise forms.ValidationError('Uploaded file format not supported or not an audio file.') elif ext == 'ogg': # Firefox seems to set wrong mime type for ogg files to video/ogg instead of audio/ogg # For this reason we also allow this mime type for ogg files. if not content_type.startswith("audio") and not content_type == 'video/ogg': raise forms.ValidationError('Uploaded file format not supported or not an audio file.') else: if not content_type.startswith("audio"): raise forms.ValidationError('Uploaded file format not supported or not an audio file.') else: raise forms.ValidationError('Uploaded file format not supported or not an audio file.') except AttributeError: # Will happen when uploading with the flash uploader if not filename_has_valid_extension(str(audiofiles)): raise forms.ValidationError('Uploaded file format not supported or not an audio file.')
def test_filename_has_valid_extension(self): cases = [ ('filaneme.wav', True), ('filaneme.aiff', True), ('filaneme.aif', True), ('filaneme.mp3', True), ('filaneme.ogg', True), ('filaneme.flac', True), ('filaneme.xyz', False), ('wav', False), ] for filename, expected_result in cases: self.assertEqual(filename_has_valid_extension(filename), expected_result)
def validate_file_extension(audiofiles): try: for file_ in audiofiles: content_type = file_.content_type if filename_has_valid_extension(str(file_)): ext = str(file_).rsplit('.', 1)[-1].lower() if ext == 'flac': # At least Safari and Firefox do not set the proper mime type for .flac files # (use 'application/octet-stream' instead of 'audio/flac'). For this reason we also allow # this mime type for flac files. if not content_type.startswith( "audio" ) and not content_type == 'application/octet-stream': raise forms.ValidationError( 'Uploaded file format not supported or not an audio file.' ) elif ext == 'ogg': # Firefox seems to set wrong mime type for ogg files to video/ogg instead of audio/ogg # For this reason we also allow this mime type for ogg files. if not content_type.startswith( "audio") and not content_type == 'video/ogg': raise forms.ValidationError( 'Uploaded file format not supported or not an audio file.' ) else: if not content_type.startswith("audio"): raise forms.ValidationError( 'Uploaded file format not supported or not an audio file.' ) else: raise forms.ValidationError( 'Uploaded file format not supported or not an audio file.') except AttributeError: # Will happen when uploading with the flash uploader if not filename_has_valid_extension(str(audiofiles)): raise forms.ValidationError( 'Uploaded file format not supported or not an audio file.')
def test_filename_has_valid_extension(self): cases = [ ("filaneme.wav", True), ("filaneme.aiff", True), ("filaneme.aif", True), ("filaneme.mp3", True), ("filaneme.ogg", True), ("filaneme.flac", True), ("filaneme.xyz", False), ("wav", False), ] for filename, expected_result in cases: self.assertEqual(filename_has_valid_extension(filename), expected_result)
def validate_audiofile(self, attrs, source): value = attrs[source] if not filename_has_valid_extension(str(value)): serializers.ValidationError( 'Uploaded file format not supported or not an audio file.') return attrs
def validate_file_extension(value): if not filename_has_valid_extension(str(value)): raise forms.ValidationError('Uploaded file format not supported or not an audio file.')
def validate_audiofile(self, value): if not filename_has_valid_extension(str(value)): raise serializers.ValidationError('Uploaded file format not supported or not an audio file.') return value
def validate_file_extension(audiofiles): for file_ in audiofiles: content_type = file_.content_type if not (content_type.startswith("audio") and filename_has_valid_extension(str(file_))): raise forms.ValidationError('Uploaded file format not supported or not an audio file.')
def validate_audiofile(self, attrs, source): value = attrs[source] if not filename_has_valid_extension(str(value)): serializers.ValidationError('Uploaded file format not supported or not an audio file.') return attrs