forked from Ghatage/horcrux
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add drag and drop UI, Flask setup for REST calls
- Loading branch information
Anup Ghatage
committed
Oct 11, 2017
1 parent
0fcc044
commit 21626e4
Showing
5 changed files
with
176 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>Sample Page</title> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> | ||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> | ||
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> | ||
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.min.js"></script> | ||
<link rel="stylesheet" href="app.css"> | ||
</head> | ||
<body> | ||
|
||
<div class="container"> | ||
<div class="droparea">Drop Here</div> | ||
<div class="tablearea" id="tablearea"></div> | ||
</div> | ||
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> | ||
<script type="text/x-handlebars-template" id="tableTemplate"> | ||
<table class="table table-condensed table-striped"> | ||
{{#each filesizes}} | ||
<tr> | ||
<td> | ||
<a href="http://example.com"> | ||
<div style="height:100%;width:200%"> | ||
{{this}} | ||
</div> | ||
</a> | ||
</td> | ||
</tr> | ||
{{/each}} | ||
</table> | ||
</script> | ||
<script src="app.js"> | ||
</script> | ||
</body> | ||
</html> |