From 3ab71ad847a8ce4097ade583ecf11ba74ab2bad1 Mon Sep 17 00:00:00 2001 From: Giulio Date: Tue, 30 Jul 2019 13:08:45 +0200 Subject: [PATCH] Import --- app.py | 82 ++++++++++++++++++++++++++++++++++++++++++++ listener.py | 11 ++++++ static/min.css | 1 + templates/index.html | 71 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 165 insertions(+) create mode 100644 app.py create mode 100644 listener.py create mode 100644 static/min.css create mode 100644 templates/index.html diff --git a/app.py b/app.py new file mode 100644 index 0000000..4dfff63 --- /dev/null +++ b/app.py @@ -0,0 +1,82 @@ +import redis, os, random, string, re +from datetime import timedelta +from flask import Flask, request, render_template, send_from_directory +from werkzeug.utils import secure_filename +r = redis.Redis('localhost') + +app = Flask(__name__) +DEFAULT_TTL = 86400 +MAX_TTL = 259200 +UPLOAD_DIR = '/storage/' +UPLOAD_SIZE = 200 * 1024 * 1024 + +def tdelta(input): + keys = ["weeks", "days", "hours", "minutes", "seconds"] + regex = "".join(["((?P<%s>\d+)%s ?)?" % (k, k[0]) for k in keys]) + kwargs = {} + for k,v in re.match(regex, input).groupdict(default="0").items(): + kwargs[k] = int(v) + return int(timedelta(**kwargs).total_seconds()) + +@app.route('/', methods=['GET']) +def index(): + return render_template('index.html') + +@app.route('/', defaults={'ttl': 3600, 'filename': None, 'isttl': False, 'isfilename': False}, methods=['PUT']) +@app.route('/', defaults={'ttl': None, 'isttl': False, 'isfilename': True}, methods=['PUT']) +@app.route('//', defaults={'isttl': True, 'isfilename': True}, methods=['PUT']) +def upload(ttl, filename, isttl, isfilename): + if isttl and ttl.isdigit(): + if int(ttl) > MAX_TTL or int(ttl) < 1: + ttl = MAX_TTL + else: + ttl = int(ttl) + elif isttl and not ttl.isdigit(): + if tdelta(ttl) > MAX_TTL or tdelta(ttl) < 1: + ttl = DEFAULT_TTL + else: + ttl = tdelta(ttl) + if isfilename: + filename = secure_filename(filename) + else: + filename = 'file.dat' + if len(request.data) > UPLOAD_SIZE: + return 'File too large', 422 + else: + key = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.ascii_lowercase) for _ in range(6)) + r.set(key, filename, ttl) + with open(UPLOAD_DIR + key, 'wb') as f: + f.write(request.data) + return 'https://files.re/' + key + '/' + filename + '\n' + +@app.route('/', defaults={'filename': None}, methods=['GET']) +@app.route('//', methods=['GET']) +def download(file, filename): + if filename == None: + filename = r.get(file) + else: + filename = secure_filename(filename) + if r.get(file) is not None: + file = secure_filename(file) + return send_from_directory(directory=UPLOAD_DIR, filename=file, as_attachment=True, attachment_filename=filename) + else: + return 'Not found', 404 + +@app.route('/view/', methods=['GET']) +def view(file): + if r.get(file) is not None: + file = secure_filename(file) + return send_from_directory(directory=UPLOAD_DIR, filename=file, mimetype="text/plain") + else: + return 'Not found', 404 + +@app.route('/ttl/', methods=['GET']) +def ttl(file): + ttl = r.ttl(file); + if ttl is not None: + return str(ttl) + else: + return 'Not found', 404 + +if __name__ == "__main__": + app.run() diff --git a/listener.py b/listener.py new file mode 100644 index 0000000..dad0a3c --- /dev/null +++ b/listener.py @@ -0,0 +1,11 @@ +import redis, os + +UPLOAD_DIR = '/storage/' + +r = redis.StrictRedis() +pubsub = r.pubsub() +pubsub.psubscribe("*") +for msg in pubsub.listen(): + if msg['channel'].decode('utf-8') == '__keyevent@0__:expired': + os.remove(UPLOAD_DIR + msg['data'].decode('utf-8')) + print("Deleted file " + msg['data'].decode('utf-8')) diff --git a/static/min.css b/static/min.css new file mode 100644 index 0000000..14d1f26 --- /dev/null +++ b/static/min.css @@ -0,0 +1 @@ +/* Copyright 2014 Owen Versteeg; MIT licensed */body,textarea,input,select{background:0;border-radius:0;font:16px sans-serif;margin:0}.smooth{transition:all .2s}.btn,.nav a{text-decoration:none}.container{margin:0 20px;width:auto}label>*{display:inline}form>*{display:block;margin-bottom:10px}.btn{background:#999;border-radius:6px;border:0;color:#fff;cursor:pointer;display:inline-block;margin:2px 0;padding:12px 30px 14px}.btn:hover{background:#888}.btn:active,.btn:focus{background:#777}.btn-a{background:#0ae}.btn-a:hover{background:#09d}.btn-a:active,.btn-a:focus{background:#08b}.btn-b{background:#3c5}.btn-b:hover{background:#2b4}.btn-b:active,.btn-b:focus{background:#2a4}.btn-c{background:#d33}.btn-c:hover{background:#c22}.btn-c:active,.btn-c:focus{background:#b22}.btn-sm{border-radius:4px;padding:10px 14px 11px}.row{margin:1% 0;overflow:auto}.col{float:left}.table,.c12{width:100%}.c11{width:91.66%}.c10{width:83.33%}.c9{width:75%}.c8{width:66.66%}.c7{width:58.33%}.c6{width:50%}.c5{width:41.66%}.c4{width:33.33%}.c3{width:25%}.c2{width:16.66%}.c1{width:8.33%}h1{font-size:3em}.btn,h2{font-size:2em}.ico{font:33px Arial Unicode MS,Lucida Sans Unicode}.addon,.btn-sm,.nav,textarea,input,select{outline:0;font-size:14px}textarea,input,select{padding:8px;border:1px solid #ccc}textarea:focus,input:focus,select:focus{border-color:#5ab}textarea,input[type=text]{-webkit-appearance:none;width:13em}.addon{padding:8px 12px;box-shadow:0 0 0 1px #ccc}.nav,.nav .current,.nav a:hover{background:#000;color:#fff}.nav{height:24px;padding:11px 0 15px}.nav a{color:#aaa;padding-right:1em;position:relative;top:-1px}.nav .pagename{font-size:22px;top:1px}.btn.btn-close{background:#000;float:right;font-size:25px;margin:-54px 7px;display:none}@media(min-width:1310px){.container{margin:auto;width:1270px}}@media(max-width:870px){.row .col{width:100%}}@media(max-width:500px){.btn.btn-close{display:block}.nav{overflow:hidden}.pagename{margin-top:-11px}.nav:active,.nav:focus{height:auto}.nav div:before{background:#000;border-bottom:10px double;border-top:3px solid;content:'';float:right;height:4px;position:relative;right:3px;top:14px;width:20px}.nav a{padding:.5em 0;display:block;width:50%}}.table th,.table td{padding:.5em;text-align:left}.table tbody>:nth-child(2n-1){background:#ddd}.msg{padding:1.5em;background:#def;border-left:5px solid #59d} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..0483dc7 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,71 @@ + + + + + + t.lsd.cat ~ Temp file storage + + + +

t.lsd.cat

+

MAX SIZE 500MB ~ MAX TLL 3 days

+
+

Usage

+

Files can be directly uploaded from the command line using:

+


+    curl --upload-file <localfile> https://t.lsd.cat/
+  
+

The above example will upload a file using the default TTL of 24 hours. For a custom TTL:

+

+    curl --upload-file <localfile> https://t.lsd.cat/160/       # 160 seconds
+    curl --upload-file <localfile> https://t.lsd.cat/2d3h5s/    # 2 days, 3 hours and 2 seconds
+  
+

It is also possible to know the remaining TLL of an uploaded file:

+

+    curl https://t.lsd.cat/ttl/<id>
+  
+

Files can also be renamed depending on the download request:

+

+    curl -O https://t.lsd.cat/<id>              # Download with original filename
+    curl -O https://t.lsd.cat/<id>/<filename>   # Download with <filename>
+  
+
+ + + +