Example #1
0
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'))
Example #2
0
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))