Giulio 4 years ago
commit
3ab71ad847
4 changed files with 164 additions and 0 deletions
  1. 82 0
      app.py
  2. 11 0
      listener.py
  3. 0 0
      static/min.css
  4. 71 0
      templates/index.html

+ 82 - 0
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('/<filename>', defaults={'ttl': None, 'isttl': False, 'isfilename': True}, methods=['PUT'])
+@app.route('/<ttl>/<filename>', 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('/<file>', defaults={'filename': None}, methods=['GET'])
+@app.route('/<file>/<filename>', 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/<file>', 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/<file>', 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()

+ 11 - 0
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'))

File diff suppressed because it is too large
+ 0 - 0
static/min.css


+ 71 - 0
templates/index.html

@@ -0,0 +1,71 @@
+<!doctype html>
+<html lang="en">
+<head>
+  <meta charset="UTF-8">
+  <meta name="viewport" content="width=device-width, initial-scale=1">
+  <title>t.lsd.cat ~ Temp file storage</title>
+  <style>
+  pre {
+    position: relative;
+    margin: 0 0 1.625em;
+    white-space: pre;
+    word-wrap: normal;
+  }
+  pre code {
+    overflow-x: auto;
+    display: block;
+    padding: 0 1em;
+    background: #657b83;
+    background-image: -webkit-linear-gradient(#5e737a 50%, transparent 50%);
+    background-image: linear-gradient(#5e737a 50%, transparent 50%);
+    background-size: 100% 3em;
+    background-position: 0 1.5em;
+    background-origin: content-box;
+    color: #fdf6e3;
+    line-height: 1.5;
+    font-size: 1em;
+  }
+
+  body {
+    max-width: 70em;
+    margin: 0 auto;
+    padding: 2em 4em;
+    background: #EEE8D5;
+    font-size: 100%;
+  }
+
+  h1,h2 {
+    color: #657b83;
+  }
+  </style>
+</head>
+<body>
+  <h1>t.lsd.cat</h1>
+  <p><small><i>MAX SIZE 500MB</i> ~ <i>MAX TLL 3 days</i></small></p>
+  <hr>
+  <h2>Usage</h2>
+  <p>Files can be directly uploaded from the command line using:<p>
+  <pre><code>
+    curl --upload-file &lt;localfile&gt; https://t.lsd.cat/
+  </code></pre>
+  <p>The above example will upload a file using the default TTL of 24 hours. For a custom TTL:</p>
+  <pre><code>
+    curl --upload-file &lt;localfile&gt; https://t.lsd.cat/160/       # 160 seconds
+    curl --upload-file &lt;localfile&gt; https://t.lsd.cat/2d3h5s/    # 2 days, 3 hours and 2 seconds
+  </code></pre>
+  <p>It is also possible to know the remaining TLL of an uploaded file:</p>
+  <pre><code>
+    curl https://t.lsd.cat/ttl/&lt;id&gt;
+  </code></pre>
+  <p>Files can also be renamed depending on the download request:</p>
+  <pre><code>
+    curl -O https://t.lsd.cat/&lt;id&gt;              # Download with original filename
+    curl -O https://t.lsd.cat/&lt;id&gt;/&lt;filename&gt;   # Download with &lt;filename&gt;
+  </code></pre>
+  <hr>
+  <footer>
+    <address><a href="https://lsd.cat" target="_blank">https://lsd.cat</a> ~ <a href="https://git.lsd.cat/g/lsd-transfer" target="_blank">Source</a></address>
+  </footer>
+</body>
+</html>
+

Some files were not shown because too many files changed in this diff