Scan files with clamdscan #77

Merged
Anna Sirota merged 17 commits from scan-file into main 2024-04-12 19:11:30 +02:00
Showing only changes of commit 34982b3f16 - Show all commits

View File

@ -17,15 +17,20 @@ def clamdscan(file_id: int):
file = files.models.File.objects.get(pk=file_id)
abs_path = os.path.join(settings.MEDIA_ROOT, file.source.path)
completed_process = files.utils.run_clamdscan(abs_path)
scan_result = {
'args': completed_process.args,
'stdout': completed_process.stdout.decode(),
'stderr': completed_process.stderr.decode(),
'returncode': completed_process.returncode,
}
logger.info('File pk=%s scanned: exit code %s', file.pk, completed_process.returncode)
results = {
'clamdscan': {
'args': completed_process.args,
'stdout': completed_process.stdout.decode(),
'stderr': completed_process.stderr.decode(),
'returncode': completed_process.returncode,
}
}
is_ok = completed_process.returncode == 0
file_validation, is_new = files.models.FileValidation.objects.get_or_create(
file=file, defaults={'results': {'clamdscan': scan_result}}
file=file, defaults={'results': results, 'is_ok': is_ok}
)
file_validation.is_ok = completed_process.returncode == 0
file_validation.save()
if not is_new:
file_validation.results = results
file_validation.is_ok = is_ok
file_validation.save(update_fields={'results', 'is_ok'})