1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | def clean_file(self):
file = self.cleaned_data.get('file', '')
#check a file in form for viruses
if file:
from tempfile import mkstemp
import pyclamav
import os
tmpfile = mkstemp()[1]
f = open(tmpfile, 'wb')
f.write(file.content)
f.close()
isvirus, name = pyclamav.scanfile(tmpfile)
os.unlink(tmpfile)
if isvirus:
raise forms.ValidationError( \
"WARNING! Virus \"%s\" was detected in this file. \
Check your system." % name)
return file
|
More like this
- Upload a zip file with newforms by kylefox 6 years, 3 months ago
- newforms: Add field-specific error in form.clean() by miracle2k 5 years, 11 months ago
- A form field for valdating PDF and Microsoft Word document by jimmylam 4 years, 7 months ago
- Custom FileField with content type and size validation by nemesis 2 years, 8 months ago
- Add ValidationError to a field instead of __all__ during Form.clean() by guettli 4 years, 8 months ago
Comments
I had to change line 10 to
f.write(file.read())to get it working with the latest development version of Django, but after that it did detect the EICAR ([European Institute for Computer Anti-Virus Research]) test virus signature.#