mirror of
https://github.com/alvierahman90/uploadr.git
synced 2025-01-12 02:04:20 +00:00
Add feature to (mostly) presserve filenames with versioning
This commit is contained in:
parent
1213158f6a
commit
922b11e3f4
@ -5,7 +5,7 @@
|
||||
<body>
|
||||
<h1>Upload success!</h1>
|
||||
<div class="indent">
|
||||
<p id="downloadLink" >File located at <a id="file_link" href="/download/{{ filename }}">/download/{{ filename }}<a>
|
||||
<p><a href="/qr/{{ filename }}"> QR Code link </a> </p>
|
||||
<p id="downloadLink" >File located at <a id="file_link" href="/download/{{ link }}">/download/{{ link }}<a>
|
||||
<p><a href="/qr/{{ link }}"> QR Code link </a> </p>
|
||||
</div>
|
||||
</body>
|
||||
|
67
uploadr.py
67
uploadr.py
@ -1,7 +1,9 @@
|
||||
import os
|
||||
import glob
|
||||
import time
|
||||
import flask
|
||||
import os
|
||||
import qrcode
|
||||
import urllib.parse
|
||||
from flask import Flask
|
||||
from flask import render_template
|
||||
from flask import request
|
||||
@ -10,23 +12,33 @@ from config import uploadr as config
|
||||
from werkzeug.utils import secure_filename
|
||||
app = Flask(__name__)
|
||||
app.config['SECRET_KEY'] = os.urandom(24)
|
||||
encode = urllib.parse.quote
|
||||
decode = urllib.parse.unquote
|
||||
|
||||
if not os.path.isdir(config.uploads):
|
||||
os.makedirs(config.uploads)
|
||||
def check_create(dir):
|
||||
if not os.path.isdir(dir):
|
||||
os.makedirs(dir)
|
||||
|
||||
if not os.path.isdir(config.qrcodes):
|
||||
os.makedirs(config.qrcodes)
|
||||
check_create(config.uploads)
|
||||
check_create(config.qrcodes)
|
||||
|
||||
@app.route("/")
|
||||
@app.route('/upload', methods=['GET','POST'])
|
||||
def upload_file():
|
||||
if request.method == 'POST':
|
||||
f =request.files['file']
|
||||
f = request.files['file']
|
||||
print(secure_filename(f.filename))
|
||||
filename = str(time.time()) + '.' + secure_filename(f.filename).split('.')[-1]
|
||||
f.save('./uploads/' + filename)
|
||||
filename = secure_filename(f.filename)
|
||||
time_uploaded = str(time.time())
|
||||
file_dir = config.uploads + '/' + filename
|
||||
check_create(file_dir)
|
||||
check_create(file_dir + '/' + time_uploaded)
|
||||
f.save(config.uploads
|
||||
+ '/' + encode(filename)
|
||||
+ '/' + time_uploaded
|
||||
+ '/' + filename)
|
||||
return render_template('upload_success.html',
|
||||
filename = filename,
|
||||
link = filename + '?version=' + time_uploaded,
|
||||
port = config.port,
|
||||
)
|
||||
else:
|
||||
@ -36,10 +48,11 @@ def upload_file():
|
||||
|
||||
@app.route('/qrcodes/<filename>')
|
||||
def qrcodes(filename):
|
||||
link = 'http://{0}:{1}/download/{2}'.format(
|
||||
link = 'http://{0}:{1}/download/{2}?version={3}'.format(
|
||||
config.domain
|
||||
, config.port
|
||||
, filename
|
||||
, encode(filename)
|
||||
, request.args.get('version')
|
||||
)
|
||||
image = qrcode.make(link)
|
||||
with open('qrcodes/' + filename+ '.png', mode = 'bw+') as file:
|
||||
@ -48,19 +61,33 @@ def qrcodes(filename):
|
||||
|
||||
@app.route('/qr/<filename>')
|
||||
def qr(filename=None):
|
||||
return render_template('qr.html', imagesrc = 'http://{0}:{1}/qrcodes/{2}'.format(
|
||||
config.domain
|
||||
, config.port
|
||||
, filename
|
||||
return render_template('qr.html'
|
||||
, imagesrc = 'http://{0}:{1}/qrcodes/{2}?version={3}'.format(
|
||||
config.domain
|
||||
, config.port
|
||||
, filename
|
||||
, request.args.get('version')
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
@app.route('/download/<filename>')
|
||||
def download(filename=None):
|
||||
if filename != None:
|
||||
return flask.send_from_directory(config.uploads, filename, as_attachment=True)
|
||||
else:
|
||||
return hello()
|
||||
provided_version = request.args.get('version')
|
||||
directory_base = config.uploads + '/' + filename + '/'
|
||||
directory = directory_base + str(provided_version)
|
||||
print(directory)
|
||||
print(os.path.isdir(directory))
|
||||
if not os.path.isdir(directory):
|
||||
existing_versions = glob.glob(directory_base + '*')
|
||||
print(existing_versions)
|
||||
existing_versions.sort()
|
||||
print(existing_versions)
|
||||
latest_version = existing_versions[-1]
|
||||
print(latest_version)
|
||||
directory = latest_version
|
||||
print(directory)
|
||||
|
||||
return flask.send_from_directory(directory, decode(filename), as_attachment=True)
|
||||
|
||||
@app.route('/css/<filename>')
|
||||
def css(filename=None):
|
||||
|
Loading…
Reference in New Issue
Block a user