f169847257718b18702306a316864eb1b53a8d48
[audio.git] / src / archive / utils.py
1 from hashlib import sha1
2 import os
3 import os.path
4 from django.core.files.storage import FileSystemStorage
5 from django.core.files.uploadedfile import UploadedFile
6
7
8 class ExistingFile(UploadedFile):
9
10     def __init__(self, path, *args, **kwargs):
11         self.path = path
12         return super(ExistingFile, self).__init__(*args, **kwargs)
13
14     def temporary_file_path(self):
15         return self.path
16
17     def close(self):
18         pass
19
20
21 class OverwriteStorage(FileSystemStorage):
22
23     def _save(self, name, content):
24         if self.exists(name):
25             self.delete(name)
26         return super(OverwriteStorage, self)._save(name, content)
27
28     def get_available_name(self, name, max_length):
29         return name
30
31
32 def sha1_file(f):
33     sha = sha1()
34     for piece in iter(lambda: f.read(1024*1024), ''):
35         sha.update(piece)
36     return sha.hexdigest()
37
38
39 def all_files(root_path):
40     root_len = len(root_path)
41     for path, dirs, files in os.walk(root_path):
42         for fname in files:
43             yield os.path.join(path, fname)[root_len:].lstrip('/')
44