2016-03-21 20:54:17 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import hashlib
|
2016-03-21 21:49:39 +00:00
|
|
|
import uuid
|
|
|
|
|
|
|
|
from django.core.files.uploadedfile import InMemoryUploadedFile
|
2016-03-21 20:54:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def sha1_checksum(handler):
|
2016-03-21 21:49:39 +00:00
|
|
|
if isinstance(handler, InMemoryUploadedFile):
|
|
|
|
temp_name = '/tmp/_{}'.format(uuid.uuid4())
|
|
|
|
f = open(temp_name, 'wb+')
|
|
|
|
f.write(handler.read())
|
2016-04-27 18:10:38 +00:00
|
|
|
f.seek(0)
|
2016-03-21 21:49:39 +00:00
|
|
|
else:
|
|
|
|
f = open(handler.name, 'rb')
|
|
|
|
|
2016-03-21 20:54:17 +00:00
|
|
|
checksum = hashlib.sha1(f.read())
|
|
|
|
return checksum.hexdigest()
|