c69fe1c826a47e288307ee24ee112ec0d98fe4f9
[audio.git] / apps / archive / utils.py
1 from hashlib import sha1
2 from django.core.files.storage import FileSystemStorage
3 from django.core.files.uploadedfile import UploadedFile
4
5
6 class ExistingFile(UploadedFile):
7
8     def __init__(self, path, *args, **kwargs):
9         self.path = path
10         return super(ExistingFile, self).__init__(*args, **kwargs)
11
12     def temporary_file_path(self):
13         return self.path
14
15     def close(self):
16         pass
17
18
19 class OverwriteStorage(FileSystemStorage):
20
21     def _save(self, name, content):
22         if self.exists(name):
23             self.delete(name)
24         return super(OverwriteStorage, self)._save(name, content)
25
26     def get_available_name(self, name):
27         return name
28
29
30 def sha1_file(f):
31     sha = sha1()
32     for piece in iter(lambda: f.read(1024*1024), ''):
33         sha.update(piece)
34     return sha.hexdigest()