1 # This sucks. Make it not suck.
2 import os
, shutil
, tempfile
, hashlib
4 chunk_size
= 1024*1024 # TODO: is this a good chunk size?
8 # Here's one way to move stuff...
9 shutil.copyfileobj(temp.file, dest_file)
11 # we should probably store the basename of the original filename in the database somewhere
12 temp_base = os.path.basename(temp.filename)
14 # You can get a mime type like so:
15 from mimetypes import guess_type
16 guess_type(temp.filename)[0]
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()
27 # that function gives me an integer file descriptor for some reason.
28 intermediate_file
= os
.fdopen(intermediate_file_descriptor
, "wb")
32 data
= temp
.file.read(chunk_size
)
36 intermediate_file
.write(data
)
39 intermediate_file
.close()
40 hash = sha1
.hexdigest()
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:] )
47 os
.rename(intermediate_path
, dest_path
)