From 21626e40f93f24fc37652c4ba227a0d2649f4d21 Mon Sep 17 00:00:00 2001 From: Anup Ghatage Date: Wed, 11 Oct 2017 16:30:00 -0500 Subject: [PATCH] Add drag and drop UI, Flask setup for REST calls --- horcrux.py | 8 +++---- main.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++ static/app.css | 26 +++++++++++++++++++++ static/app.js | 51 ++++++++++++++++++++++++++++++++++++++++++ static/index.html | 38 +++++++++++++++++++++++++++++++ 5 files changed, 176 insertions(+), 4 deletions(-) create mode 100644 main.py create mode 100644 static/app.css create mode 100644 static/app.js create mode 100644 static/index.html diff --git a/horcrux.py b/horcrux.py index 7ba397b..93b524b 100755 --- a/horcrux.py +++ b/horcrux.py @@ -4,9 +4,9 @@ from rabin import split_file_by_fingerprints import os, ipfsapi -def main(filename): +#def main(filename): # read_and_encrypt(filename) - read_and_decrypt(filename) +# read_and_decrypt(filename) # Will be called by upload function @@ -77,6 +77,6 @@ def read_and_decrypt(filename): # plaintext = EncryptLayer.decryptWrapper("One",cipher) # print("Decrypted: " + plaintext.decode('utf8')) -if __name__ == "__main__": - main("/Users/aghatage/Documents/PersonalDocs/horcrux/test.txt") +#if __name__ == "__main__": +# main("/Users/aghatage/Documents/PersonalDocs/horcrux/test.txt") diff --git a/main.py b/main.py new file mode 100644 index 0000000..048ccfa --- /dev/null +++ b/main.py @@ -0,0 +1,57 @@ +from flask import Flask, request, redirect, jsonify +import os +from werkzeug.utils import secure_filename + + +app = Flask(__name__) +app.config["UPLOAD_FOLDER"] = "./uploads" + + +@app.route("/") +def index(): + return redirect("/static/index.html") + + +@app.route("/sendfile", methods=["POST"]) +def send_file(): + fileob = request.files["file2upload"] + filename = secure_filename(fileob.filename) + save_path = "{}/{}".format(app.config["UPLOAD_FOLDER"], filename) + fileob.save(save_path) + + # open and close to update the access time. + with open(save_path, "r") as f: + pass + + read_and_encrypt(save_path) + + return "successful_upload" + + +@app.route("/filenames", methods=["GET"]) +def get_filenames(): + filenames = os.listdir("./static/uploads/") + + #modify_time_sort = lambda f: os.stat("./static/uploads/{}".format(f)).st_atime + + def modify_time_sort(file_name): + file_path = "./static/uploads/{}".format(file_name) + file_stats = os.stat(file_path) + last_access_time = file_stats.st_atime + return last_access_time + + filenames = sorted(filenames, key=modify_time_sort) + filesizes = [] + for file in filenames: + file_path = "./static/uploads/{}".format(file) + file_stats = os.stat(file_path) + filesizes.append(file + " " + str(file_stats.st_size) + " KB") + # filesizes.append(file) + + return_dict = dict(filesizes=filesizes) + + print(return_dict) + return jsonify(return_dict) + +if __name__ == '__main__': + app.run(debug=False) diff --git a/static/app.css b/static/app.css new file mode 100644 index 0000000..951ce1d --- /dev/null +++ b/static/app.css @@ -0,0 +1,26 @@ +.droparea { + width: 200px; + height: 200px; + border-style: solid; + border-width: 3px; + border-color: red; + float: left; + line-height: 200px; + text-align: center; +} + +.tablearea { + width: 200px; + min-height: 200px; + border-style: solid; + border-width: 1px; + border-color: gray; + float: left; + margin-left: 10px; +} + +.table a +{ + display:block; + text-decoration:none; +} diff --git a/static/app.js b/static/app.js new file mode 100644 index 0000000..1173876 --- /dev/null +++ b/static/app.js @@ -0,0 +1,51 @@ +$(function(){ + + var refreshFilenameList = function(data){ + var templateText = $("#tableTemplate").html(); + var template = Handlebars.compile(templateText); + var renderedText = template(data); + var renderedDom = $(renderedText); + $("#tablearea").empty(); + $("#tablearea").append(renderedDom); + }; + + var fileUploadSuccess = function(data){ + var url = "/filenames"; + var promise = $.get(url); + promise.then(refreshFilenameList); + }; + + var fileUploadFail = function(data){}; + + var dragHandler = function(evt){ + evt.preventDefault(); + }; + + var dropHandler = function(evt){ + evt.preventDefault(); + var files = evt.originalEvent.dataTransfer.files; + + var formData = new FormData(); + formData.append("file2upload", files[0]); + + var req = { + url: "/sendfile", + method: "post", + processData: false, + contentType: false, + data: formData + }; + + var promise = $.ajax(req); + promise.then(fileUploadSuccess, fileUploadFail); + }; + + var dropHandlerSet = { + dragover: dragHandler, + drop: dropHandler + }; + + $(".droparea").on(dropHandlerSet); + + fileUploadSuccess(false); // called to ensure that we have initial data +}); diff --git a/static/index.html b/static/index.html new file mode 100644 index 0000000..70972ac --- /dev/null +++ b/static/index.html @@ -0,0 +1,38 @@ + + + + Sample Page + + + + + + + + + + +
+
Drop Here
+
+
+ + + + +