Commit 6f54b1b7 authored by ale's avatar ale

sqlalchemy 0.5 fixes; simplify the Receiver class

parent 6a12952d
......@@ -23,7 +23,7 @@ flags.define_string('upload_path', '-U', '--upload-path',
metadata = MetaData()
Session = scoped_session(sessionmaker(autoflush=True, transactional=True))
Session = scoped_session(sessionmaker(autoflush=True, autocommit=True))
# Status:
......
......@@ -44,15 +44,6 @@ def stream(file_object):
return CustomFieldStorage
#class FileMonitor(object):
# def __init__(self, fp):
# self.fp = fp
# def read(self, n=-1):
# buf = self.fp.read(n)
# logging.info("Read %s" % buf)
# return buf
class Generate:
def GET(self):
m = md5()
......@@ -61,7 +52,7 @@ class Generate:
token = m.hexdigest()
aobj = AsyncUpload(token=token, size=0, cur_size=0,
exp_size=0, status='N')
Session.save(aobj)
Session.add(aobj)
Session.commit()
web.header('Content-Type', 'application/json')
web.output(simplejson.dumps(token))
......@@ -92,56 +83,47 @@ class Receiver:
temp_path = temp_obj.path()
rough_size = int(web.ctx.environ.get('CONTENT_LENGTH', '0'))
temp_obj.exp_size = rough_size
Session.update(temp_obj)
Session.add(temp_obj)
Session.commit()
logging.info("upload: new AsyncUpload: %s" % token)
try:
class Buffered(object):
def __init__(self, obj):
self.obj = obj
self.last_sz = 0
def update(self, pos):
self.obj.cur_size = pos
if pos - self.last_sz > 65535:
Session.update(self.obj)
Session.commit()
self.last_sz = pos
buffobj = Buffered(temp_obj)
def callback(file, *k, **p):
buffobj.update(file.tell())
fp = ProgressFile(temp_path, 'wb')
fp.set_callback(callback)
custom_field_storage_class = stream(fp)
custom_field_storage = custom_field_storage_class(
environ=web.ctx.environ,
keep_blank_values=1,
fp=web.ctx.environ['wsgi.input']
)
fp.close()
final_size = os.path.getsize(temp_path)
temp_obj.size = temp_obj.cur_size = final_size
temp_obj.status = 'C'
Session.update(temp_obj)
completed = True
logging.info("%s: uploaded %d bytes" % (token, final_size))
finally:
if not completed:
logging.info('upload of %s aborted, removing temporary file' % token)
temp_obj.status = 'E'
temp_obj.remove()
Session.update(temp_obj)
Session.commit()
class Buffered(object):
def __init__(self, obj):
self.obj = obj
self.last_sz = 0
def update(self, pos):
self.obj.cur_size = pos
if pos - self.last_sz > 16384:
Session.add(self.obj)
Session.commit()
self.last_sz = pos
buffobj = Buffered(temp_obj)
def callback(file, *k, **p):
buffobj.update(file.tell())
fp = ProgressFile(temp_path, 'wb')
fp.set_callback(callback)
custom_field_storage_class = stream(fp)
custom_field_storage = custom_field_storage_class(
environ=web.ctx.environ,
keep_blank_values=1,
fp=web.ctx.environ['wsgi.input']
)
fp.close()
final_size = os.path.getsize(temp_path)
logging.info("%s: uploaded %d bytes" % (token, final_size))
temp_obj.exp_size = temp_obj.size = temp_obj.cur_size = final_size
temp_obj.status = 'C'
Session.add(temp_obj)
Session.commit()
web.output('OK')
urls = (
'/receiver/(.*)', 'Receiver',
'/ping/(.*)', 'Ping',
'/gen', 'Generate',
'/_uploadserver/receiver/(.*)', 'Receiver',
'/_uploadserver/ping/(.*)', 'Ping',
'/_uploadserver/gen', 'Generate',
)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment