~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to mainpage/validators.py

  • Committer: kaputtnik
  • Date: 2019-09-03 06:16:23 UTC
  • mfrom: (544.2.25 pybb_attachments)
  • Revision ID: kaputtnik-20190903061623-xu4kvqpabnzmuskw
Allow file uploads in the forum; added some basic file checks to validate files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from django.core.exceptions import ValidationError
 
2
from django.conf import settings
 
3
 
 
4
import shutil
 
5
import subprocess
 
6
 
 
7
 
 
8
def virus_scan(uploaded_file):
 
9
    '''Scan uploaded_file for viruses with clamav.'''
 
10
 
 
11
    if settings.VIRUS_CHECK:
 
12
        try:
 
13
            tmp_file_path = uploaded_file.temporary_file_path()
 
14
            process_compl = subprocess.run(['/usr/bin/clamdscan',
 
15
                                            '--multiscan',
 
16
                                            '--fdpass',
 
17
                                            tmp_file_path])
 
18
            if process_compl.returncode == 1:
 
19
                raise ValidationError(
 
20
                    'This file seems to contain malicious code.'
 
21
                    )
 
22
            if process_compl.returncode == 2:
 
23
                raise ValidationError(
 
24
                    'Some error occured during virus scannning...'
 
25
                )
 
26
        except FileNotFoundError:
 
27
            raise ValidationError(
 
28
                'Please check the installation of clamav and make \
 
29
                sure clamdscan is working.'
 
30
            )