35f7a745b607a156055c088a5ae25756b6745a77
[audio.git] / src / archive / utils.py
1 from hashlib import sha1
2 import os
3 import os.path
4 import subprocess
5 from django.core.files.storage import FileSystemStorage
6 from django.core.files.uploadedfile import UploadedFile
7
8
9 class ExistingFile(UploadedFile):
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     def _save(self, name, content):
23         if self.exists(name):
24             self.delete(name)
25         return super(OverwriteStorage, self)._save(name, content)
26
27     def get_available_name(self, name, max_length):
28         return name
29
30
31 def sha1_file(f):
32     sha = sha1()
33     for piece in iter(lambda: f.read(1024 * 1024), b""):
34         sha.update(piece)
35     return sha.hexdigest()
36
37
38 def all_files(root_path):
39     root_len = len(root_path)
40     for path, dirs, files in os.walk(root_path, followlinks=True):
41         for fname in files:
42             yield os.path.join(path, fname)[root_len:].lstrip("/")