Added User Pages, which you can now display galleries on. Also detected
[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 from pylons import config
7
8 """
9 Notes:
10 # Here's one way to move stuff...
11 shutil.copyfileobj(temp.file, dest_file)
12
13 # we should probably store the basename of the original filename in the database somewhere
14 temp_base = os.path.basename(temp.filename)
15
16 # You can get a mime type like so:
17 from mimetypes import guess_type
18 guess_type(temp.filename)[0]
19 """
20
21 def get_path(space, hash):
22 return "/" + os.path.join( space, hash[:2], hash[2:] )
23
24
25 def save_file(space, temp):
26
27 dest_root = os.path.join( config['app_conf']['static_root'], space )
28
29 # we don't know where we're going to save this stuff yet,
30 # so I guess we'll write it to another tempfile. One we know the path of.
31 # I'm assuming the tempfile we get from pylons is set to delete itself
32 # when it closes, and has no visible path. Maybe I'm wrong?
33 intermediate_file_descriptor, intermediate_path = tempfile.mkstemp()
34
35 # that function gives me an integer file descriptor for some reason.
36 intermediate_file = os.fdopen(intermediate_file_descriptor, "wb")
37
38 sha1 = hashlib.sha1()
39 while 1:
40 data = temp.file.read(chunk_size)
41 if not data:
42 break
43 sha1.update(data)
44 intermediate_file.write(data)
45
46 temp.file.close()
47 intermediate_file.close()
48 hash = sha1.hexdigest()
49
50 # git convention: first two characters are the directory
51 dest_dir = os.path.join( dest_root, hash[:2] )
52 dest_path = os.path.join( dest_dir, hash[2:] )
53
54 makedirs(dest_dir)
55 shutil.move(intermediate_path, dest_path)
56
57 return hash
58
59
60
61 def makedirs(dir):
62 try:
63 os.makedirs(dir)
64 except OSError:
65 pass
66