-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.py
82 lines (69 loc) · 2.06 KB
/
get.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python3
# -*- coding: utf-8 -*-
""""
Author: Jan Harasym <[email protected]>
Website: dijit.sh
Creation Date: 2015-10-22
Last Modified: Sun 22 Nov 23:47:06 2015
Description:
Expected Use:
"""
from flask import Flask, request, redirect, url_for, render_template, make_response, abort
import json
import glob
import redis
from hashlib import sha256
import os
app = Flask(__name__)
dbconn = redis.Redis()
def redis_set(hash,image):
try:
db = redis.Redis()
db.setex(hash,image,1000)
# FIXME: Hardcoded mime-type should be derived, not static.
db.setex(hash + '_mime','image/jpeg',1000)
return(hash)
except:
return("Failed to submit to server")
def sha256sum(data):
d = sha256()
d.update(data)
return d.hexdigest()
@app.route('/')
def index():
return render_template('index.html')
# Route that will process the file upload
@app.route('/upload', methods=['POST'])
def upload():
# Get the name of the uploaded file
file = request.files['file']
filedata = request.files['file'].read()
print(request)
print(filedata)
print(sha256sum(filedata))
if file:
filename = sha256sum(filedata)
redis_set(filename,filedata)
# Redirect the user to the uploaded_file route, which
# will basicaly show on the browser the uploaded file
# FIXME: I don't have any idea how to do this.
# return redirect("/files/" + str(filename))
return redirect(url_for('redis_get',
hash=filename))
@app.route("/files/<hash>")
def redis_get(hash):
upload_data = dbconn.get(hash)
if upload_data is None:
abort(404)
mimetype = dbconn.get(hash + '_mime')
response = make_response(upload_data)
response.headers['Content-Type'] = mimetype
#response.headers['Content-Disposition'] = 'attachment; filename=img.jpg'
return(response)
def ajax_response(status, msg):
status_code = "ok" if status else "error"
return json.dumps(dict(
status=status_code,
msg=msg,
))
app.run(debug=True)