RE: File upload virus scanning ยป attachment-patch.txt
| 1 |
validate :validate_max_file_size, :validate_file_extension, **:virus_scan** |
|---|---|
| 2 |
|
| 3 |
# Scans the file for viruses |
| 4 |
def virus_scan |
| 5 |
|
| 6 |
# write to tmp file |
| 7 |
if @temp_file |
| 8 |
f = Tempfile.new('av')
|
| 9 |
f.binmode |
| 10 |
if @temp_file.respond_to?(:read) |
| 11 |
buffer = "" |
| 12 |
while (buffer = @temp_file.read(8192)) |
| 13 |
f.write(buffer) |
| 14 |
end |
| 15 |
else |
| 16 |
f.write(@temp_file) |
| 17 |
end |
| 18 |
|
| 19 |
# hand off to scanner |
| 20 |
f.flush |
| 21 |
result = %x[ /usr/local/bin/clamd-hook.sh #{f.path} ]
|
| 22 |
if result.squish == "0" |
| 23 |
logger.info('virus detected, file rejected')
|
| 24 |
errors.add(:base, 'Malware detected!') |
| 25 |
end |
| 26 |
|
| 27 |
f.close! |
| 28 |
|
| 29 |
end |
| 30 |
|
| 31 |
end |