5595e88fd8305179609627085fc55d19db4af698
[zzz-floof.git] / floof / lib / file_storage.py
1 # This sucks. Make it not suck.
2 import os, shutil, tempfile, hashlib
3
4 chunk_size = 1024*1024 # TODO: is this a good chunk size?
5
6 """
7 Notes:
8 # Here's one way to move stuff...
9 shutil.copyfileobj(temp.file, dest_file)
10
11 # we should probably store the basename of the original filename in the database somewhere
12 temp_base = os.path.basename(temp.filename)
13
14 # You can get a mime type like so:
15 from mimetypes import guess_type
16 guess_type(temp.filename)[0]
17 """
18
19
20 def save_file(dest_root, temp):
21 # we don't know where we're going to save this stuff yet,
22 # so I guess we'll write it to another tempfile. One we know the path of.
23 # I'm assuming the tempfile we get from pylons is set to delete itself
24 # when it closes, and has no visible path. Maybe I'm wrong?
25 intermediate_file_descriptor, intermediate_path = tempfile.mkstemp()
26
27 # that function gives me an integer file descriptor for some reason.
28 intermediate_file = os.fdopen(intermediate_file_descriptor, "wb")
29
30 sha1 = hashlib.sha1()
31 while 1:
32 data = temp.file.read(chunk_size)
33 if not data:
34 break
35 sha1.update(data)
36 intermediate_file.write(data)
37
38 temp.file.close()
39 intermediate_file.close()
40 hash = sha1.hexdigest()
41
42 # git convention: first two characters are the directory
43 dest_dir = os.path.join( dest_root, hash[:2] )
44 dest_path = os.path.join( dest_dir, hash[2:] )
45
46 makedirs(dest_dir)
47 os.rename(intermediate_path, dest_path)
48
49 return hash
50
51
52
53 def makedirs(dir):
54 try:
55 os.makedirs(dir)
56 except OSError:
57 pass
58