-
Notifications
You must be signed in to change notification settings - Fork 2k
/
uploader.py
61 lines (55 loc) · 2.34 KB
/
uploader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
import cgi
import pylons
import datetime
import ckan.lib.munge as munge
class Upload(object):
def __init__(self, object_type, old_filename=None):
path = pylons.config.get('ckan.storage_path', '/tmp')
self.storage_path = os.path.join(path, 'storage', 'uploads', object_type)
try:
os.makedirs(self.storage_path)
except OSError, e:
pass
self.object_type = object_type
self.old_filename = old_filename
if old_filename:
self.old_filepath = os.path.join(self.storage_path, old_filename)
self.filename = None
self.filepath = None
def update_data_dict(self, data_dict, url_field, file_field, clear_field):
self.url = data_dict.get(url_field, '')
self.clear = data_dict.pop(clear_field, None)
self.upload_field_storage = data_dict.pop(file_field, None)
if isinstance(self.upload_field_storage, cgi.FieldStorage):
self.filename = self.upload_field_storage.filename
self.filename = str(datetime.datetime.utcnow()) + self.filename
self.filename = munge.munge_filename(self.filename)
self.filepath = os.path.join(self.storage_path, self.filename)
data_dict[url_field] = self.filename
self.upload_file = self.upload_field_storage.file
self.tmp_filepath = self.filepath + '~'
### keep the file if there has been no change
elif self.old_filename and not self.old_filename.startswith('http'):
if not self.clear:
data_dict[url_field] = self.old_filename
if self.clear and self.url == self.old_filename:
data_dict[url_field] = ''
def upload(self):
if self.filename:
output_file = open(self.tmp_filepath, 'wb')
self.upload_file.seek(0)
while True:
data = self.upload_file.read(2 ** 20) #mb chuncks
if not data:
break
output_file.write(data)
output_file.close()
os.rename(self.tmp_filepath, self.filepath)
self.clear = True
if (self.clear and self.old_filename
and not self.old_filename.startswith('http')):
try:
os.remove(self.old_filepath)
except OSError, e:
pass