def handle_upload(f, fileattrs): """ Handle a chunked or non-chunked upload. """ logger.info(fileattrs) chunked = False dest_folder = os.path.join(settings.UPLOAD_DIRECTORY, fileattrs["qquuid"]) dest = os.path.join(dest_folder, fileattrs["qqfilename"]) # Chunked if int(fileattrs["qqtotalparts"]) > 1: chunked = True dest_folder = os.path.join(settings.CHUNKS_DIRECTORY, fileattrs["qquuid"]) dest = os.path.join(dest_folder, fileattrs["qqfilename"], str(fileattrs["qqpartindex"])) logger.info("Chunked upload received") utils.save_upload(f, dest) logger.info("Upload saved: %s" % dest) # If the last chunk has been sent, combine the parts. if chunked and (fileattrs["qqtotalparts"] - 1 == fileattrs["qqpartindex"]): logger.info("Combining chunks: %s" % os.path.dirname(dest)) utils.combine_chunks( fileattrs["qqtotalparts"], fileattrs["qqtotalfilesize"], source_folder=os.path.dirname(dest), dest=os.path.join(settings.UPLOAD_DIRECTORY, fileattrs["qquuid"], fileattrs["qqfilename"]), ) logger.info("Combined: %s" % dest) shutil.rmtree(os.path.dirname(os.path.dirname(dest)))
def handle_upload(f, fileattrs): """ Handle a chunked or non-chunked upload. """ logger.info(fileattrs) chunked = False dest_folder = os.path.join(settings.UPLOAD_DIRECTORY, fileattrs['qquuid']) dest = os.path.join(dest_folder, fileattrs['qqfilename']) # Chunked if fileattrs.get('qqtotalparts') and int(fileattrs['qqtotalparts']) > 1: chunked = True dest_folder = os.path.join(settings.CHUNKS_DIRECTORY, fileattrs['qquuid']) dest = os.path.join(dest_folder, fileattrs['qqfilename'], str(fileattrs['qqpartindex'])) logger.info('Chunked upload received') utils.save_upload(f, dest) logger.info('Upload saved: %s' % dest) # If the last chunk has been sent, combine the parts. if chunked and (fileattrs['qqtotalparts'] - 1 == fileattrs['qqpartindex']): logger.info('Combining chunks: %s' % os.path.dirname(dest)) utils.combine_chunks(fileattrs['qqtotalparts'], fileattrs['qqtotalfilesize'], source_folder=os.path.dirname(dest), dest=os.path.join(settings.UPLOAD_DIRECTORY, fileattrs['qquuid'], fileattrs['qqfilename'])) logger.info('Combined: %s' % dest) shutil.rmtree(os.path.dirname(os.path.dirname(dest)))
def post(request, callback): form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): file_attrs = form.cleaned_data dest_path = os.path.join(settings.UPLOAD_DIRECTORY, file_attrs['qquuid']) dest_file = os.path.join(dest_path, file_attrs['qqfilename']) chunk = False if file_attrs['qqtotalparts'] is not None and int( file_attrs['qqtotalparts']) > 1: dest_file = os.path.join(dest_file + '.chunks', str(file_attrs['qqpartindex'])) chunk = True utils.save_upload(file_attrs['qqfile'], dest_file) if chunk and (file_attrs['qqtotalparts'] - 1 == file_attrs['qqpartindex']): dest_file = os.path.join(dest_path, file_attrs['qqfilename']) utils.combine_chunks(file_attrs['qqtotalparts'], file_attrs['qqtotalfilesize'], source_folder=dest_file + '.chunks', dest=dest_file) shutil.rmtree(dest_file + '.chunks') chunk = False if not chunk: try: callback(file_path=dest_file, uuid=file_attrs['qquuid']) except CallbackError as e: return make_response(status=400, content=json.dumps({ 'success': False, 'error': '%s' % repr(e) })) except Exception as e: return make_response(status=500, content=json.dumps({ 'success': False, 'error': 'Exception thrown by callback' })) finally: shutil.rmtree(dest_path) return make_response(content=json.dumps({'success': True})) else: return make_response(status=400, content=json.dumps({ 'success': False, 'error': '%s' % repr(form.errors) }))
def post(request, callback): form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): file_attrs = form.cleaned_data dest_path = os.path.join(settings.UPLOAD_DIRECTORY, file_attrs['qquuid']) dest_file = os.path.join(dest_path, file_attrs['qqfilename']) chunk = False if file_attrs['qqtotalparts'] != None and int(file_attrs['qqtotalparts']) > 1: dest_file = os.path.join(dest_file+'.chunks', str(file_attrs['qqpartindex'])) chunk = True utils.save_upload(file_attrs['qqfile'], dest_file) if chunk and (file_attrs['qqtotalparts'] - 1 == file_attrs['qqpartindex']): dest_file = os.path.join(dest_path, file_attrs['qqfilename']) utils.combine_chunks(file_attrs['qqtotalparts'], file_attrs['qqtotalfilesize'], source_folder=dest_file+'.chunks', dest=dest_file) shutil.rmtree(dest_file+'.chunks') chunk = False if not chunk: try: callback(file_path=dest_file, uuid=file_attrs['qquuid']) except CallbackError, e: return make_response(status=400, content=json.dumps({ 'success': False, 'error': '%s' % repr(e) })) except Exception, e: return make_response(status=500, content=json.dumps({ 'success': False, 'error': 'Exception thrown by callback' })) finally: