The idea is to use an utility such as sndhdr to try to read a header of the file.
from sndhdr import what
import os
from settings import MEDIA_ROOT
...
if what( os.path.join( MEDIA_ROOT, file.name ) ) == None:
# header parse failed, so, it could be a non-audio file.
In this case you must be sure that the utility could not recognize a non-audio file as an audio file.
I never use the sndhdr utility, so, it may be better to use another one. For example there is also mutagen project.
Update.
The more professional approach.
- Let a user upload his files without the such validation.
- Create a special model field associated with one file: is_checked. Set the field
False by default.
- Don't let other users to downoad the files which have
is_checked == False
- Organize an asynchronous task that runs an
anti-malware soft that checked unchecked files. The software must be
called as a function from your pythonic task. The function must
answer the question: is a particular file has a malware part.
- If the current file has the malware part delete it and delete the record about the
file. Otherwise set up is_checked = True from your task.
Why a separated task? Because the such malware checking can be a long operation.