def status(body_dict): blobs=db_session.query(Blob).order_by(Blob.id) blob_array = [] for blob in blobs: blob_array.append((blob.id, str(blob.last_change), str(blob.last_sync))) data = {"message_id":(uuid.uuid4().int & (1<<63)-1), "type":"STATUS", "node_id":node_id, "blobs":blob_array} emit_update(json.dumps(data))
def reupload(body_dict): global node_id, node_ip, node_port b = db_session.query(Blob).get(body_dict["file_id"]) data = {"message_id":(uuid.uuid4().int & (1<<63)-1), "type":"POST", "node_id":node_id, "node_ip":node_ip, "node_port":node_port, "file_id":b.id, "upload_date":str(b.upload_date), "file_last_update":str(b.last_change)} emit_update(json.dumps(data))
def request_sync(node_id, node_ip, node_port): print "requesting sync" blobs=db_session.query(Blob).order_by(Blob.id) blob_array = [] for blob in blobs: blob_array.append((blob.id, str(blob.last_change), str(blob.last_sync))) data = {"message_id":(uuid.uuid4().int & (1<<63)-1), "type":"SYNC", "node_id":node_id, "node_ip":node_ip, "node_port":node_port, "blobs":blob_array} emit_update(json.dumps(data))
def status(body_dict): blobs = db_session.query(Blob).order_by(Blob.id) blob_array = [] for blob in blobs: blob_array.append( (blob.id, str(blob.last_change), str(blob.last_sync))) data = { "message_id": (uuid.uuid4().int & (1 << 63) - 1), "type": "STATUS", "node_id": node_id, "blobs": blob_array } emit_update(json.dumps(data))
def reupload(body_dict): global node_id, node_ip, node_port b = db_session.query(Blob).get(body_dict["file_id"]) data = { "message_id": (uuid.uuid4().int & (1 << 63) - 1), "type": "POST", "node_id": node_id, "node_ip": node_ip, "node_port": node_port, "file_id": b.id, "upload_date": str(b.upload_date), "file_last_update": str(b.last_change) } emit_update(json.dumps(data))
def dowload_blob(blob_id): if request.method == 'GET': # Download file! # Retrieve file information from database using id blob = db_session.query(Blob).get(blob_id) response = make_response() response.headers['Pragma'] = 'public' response.headers['Content-Type'] = 'txt' response.headers['Content-Transfer-Encoding'] = 'binary' response.headers['Content-Description'] = 'File Transfer' response.headers['Content-Disposition'] = 'attachment; filename=%s' % blob.filename response.headers['Content-Length'] = blob.file_size response.data = blob.lob return response
def request_sync(node_id, node_ip, node_port): print "requesting sync" blobs = db_session.query(Blob).order_by(Blob.id) blob_array = [] for blob in blobs: blob_array.append( (blob.id, str(blob.last_change), str(blob.last_sync))) data = { "message_id": (uuid.uuid4().int & (1 << 63) - 1), "type": "SYNC", "node_id": node_id, "node_ip": node_ip, "node_port": node_port, "blobs": blob_array } emit_update(json.dumps(data))
def add_update_blob(body_dict): response = urllib2.urlopen("http://%s:%d/blob/%d/download" % (body_dict["node_ip"], body_dict["node_port"], body_dict["file_id"])) _, params = cgi.parse_header(response.headers.get('Content-Disposition', '')) fn = params['filename'] f_size = sys.getsizeof(response) f_blob = response.read() b=db_session.query(Blob).get(body_dict["file_id"]) if b == None: b = Blob(fn,f_blob, f_size) db_session.add(b) db_session.commit() b.id = body_dict["file_id"] else: b.lob = f_blob b.filesize = f_size date_format = '%Y-%m-%d %H:%M:%S.%f' b.last_change = datetime.strptime(body_dict["file_last_update"], date_format) b.last_sync = datetime.strptime(body_dict["file_last_sync"], date_format) db_session.commit()
def handle_conflict(body_dict): # Adding a new file old_blob = db_session.query(Blob).get(body_dict["file_id"]) old_filename=old_blob.filename.split(".") new_filename = "" try: new_filename=old_filename[0]+"_conflict"+"."+old_filename[1] except IndexError: new_filename=old_filename[0]+"_conflict" new_blob = Blob(new_filename, old_blob.lob, old_blob.file_size) db_session.add(new_blob) db_session.delete(old_blob) db_session.commit() data = {"message_id":(uuid.uuid4().int & (1<<63)-1), "type":"POST", "node_id":node_id, "node_ip":node_ip, "node_port":node_port, "file_id":new_blob.id, "upload_date":str(new_blob.upload_date), "file_last_update":str(new_blob.last_change)}
def add_update_blob(body_dict): response = urllib2.urlopen( "http://%s:%d/blob/%d/download" % (body_dict["node_ip"], body_dict["node_port"], body_dict["file_id"])) _, params = cgi.parse_header( response.headers.get('Content-Disposition', '')) fn = params['filename'] f_size = sys.getsizeof(response) f_blob = response.read() b = db_session.query(Blob).get(body_dict["file_id"]) if b == None: b = Blob(fn, f_blob, f_size) db_session.add(b) db_session.commit() b.id = body_dict["file_id"] else: b.lob = f_blob b.filesize = f_size date_format = '%Y-%m-%d %H:%M:%S.%f' b.last_change = datetime.strptime(body_dict["file_last_update"], date_format) b.last_sync = datetime.strptime(body_dict["file_last_sync"], date_format) db_session.commit()
def handle_conflict(body_dict): # Adding a new file old_blob = db_session.query(Blob).get(body_dict["file_id"]) old_filename = old_blob.filename.split(".") new_filename = "" try: new_filename = old_filename[0] + "_conflict" + "." + old_filename[1] except IndexError: new_filename = old_filename[0] + "_conflict" new_blob = Blob(new_filename, old_blob.lob, old_blob.file_size) db_session.add(new_blob) db_session.delete(old_blob) db_session.commit() data = { "message_id": (uuid.uuid4().int & (1 << 63) - 1), "type": "POST", "node_id": node_id, "node_ip": node_ip, "node_port": node_port, "file_id": new_blob.id, "upload_date": str(new_blob.upload_date), "file_last_update": str(new_blob.last_change) }
def show_blob(blob_id): b=db_session.query(Blob).get(blob_id) global online if request.method == 'GET': print "Online: ", online return render_template('show_file.html', node_port=node_port, node_ip=node_ip, node_id=node_id, blob=b) elif request.method == 'PUT': # Adding a new file fn = b.filename f = request.files['blob'] fn_new = secure_filename(f.filename) if not fn == fn_new: flash('File name not the same') return render_template('show_file.html', node_port=node_port, node_ip=node_ip, node_id=node_id, blob=b) else: # Adds information about the file in the database f_size = sys.getsizeof(f) f_blob = f.read() b.upload_date = b.last_change b.last_change = datetime.datetime.now() b.lob = f_blob b.file_size = f_size db_session.commit() data = {"message_id":(uuid.uuid4().int & (1<<63)-1), "type":"PUT", "node_id":node_id, "node_ip":node_ip, "node_port":node_port, "file_id":b.id, "upload_date":str(b.upload_date), "file_last_update":str(b.last_change)} global online if(online): rabbitmq.emit_update(json.dumps(data)) else: print "is offline" flash('File update successful.') return render_template('show_file.html', node_port=node_port, node_ip=node_ip, node_id=node_id, blob=b) elif request.method == 'DELETE': db_session.delete(b) db_session.commit() data = {"message_id":(uuid.uuid4().int & (1<<63)-1), "type":"DELETE", "node_id":node_id, "node_ip":node_ip, "node_port":node_port, "file_id":b.id, "upload_date":str(b.upload_date), "file_last_update":str(b.last_change), "file_last_sync":str(b.last_sync)} global online if(online): rabbitmq.emit_update(json.dumps(data)) else: print "is offline" flash('File is removed.') return redirect(url_for('blob')) elif request.method == 'POST': # This should totally never ever happen.. but it needs to support it, don't judge. flash('I think I broke something, call my mummy..') return render_template('show_file.html', node_port=node_port, node_ip=node_ip, node_id=node_id, blob=db_session.query(Blob).get(blob_id))
def blob(): print "Port: ", node_port print "IP: ", node_ip print "ID: ", node_id global online print "Online: ", online if request.method == 'GET': return render_template('show_files.html', node_port=node_port, node_ip=node_ip, node_id=node_id, blobs=db_session.query(Blob).order_by(Blob.id), online=online) elif request.method == 'POST': # Adding a new file f = request.files['blob'] fn = secure_filename(f.filename) # Adds information about the file in the database f_size = sys.getsizeof(f) f_blob = f.read() b = Blob(fn,f_blob, f_size) db_session.add(b) db_session.commit() flash('File upload successful.') data = {"message_id":(uuid.uuid4().int & (1<<63)-1), "type":"POST", "node_id":node_id, "node_ip":node_ip, "node_port":node_port, "file_id":b.id, "upload_date":str(b.upload_date), "file_last_update":str(b.last_change)} if(online): rabbitmq.emit_update(json.dumps(data)) else: print "is offline" return redirect(url_for('blob'))
def add_update_succeed(body_dict): date_format = '%Y-%m-%d %H:%M:%S.%f' b=db_session.query(Blob).get(body_dict["file_id"]) b.last_sync = datetime.strptime(body_dict["file_last_sync"], date_format) db_session.commit()
def delete_blob(body_dict): b=db_session.query(Blob).get(body_dict["file_id"]) if not b == None: db_session.delete(b) db_session.commit()
def add_update_succeed(body_dict): date_format = '%Y-%m-%d %H:%M:%S.%f' b = db_session.query(Blob).get(body_dict["file_id"]) b.last_sync = datetime.strptime(body_dict["file_last_sync"], date_format) db_session.commit()
def delete_blob(body_dict): b = db_session.query(Blob).get(body_dict["file_id"]) if not b == None: db_session.delete(b) db_session.commit()