83 lines
2.8 KiB
Python
83 lines
2.8 KiB
Python
|
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()
|