initial commit with docker
This commit is contained in:
commit
164b9ab4b9
8 changed files with 460 additions and 0 deletions
60
main.py
Normal file
60
main.py
Normal file
|
@ -0,0 +1,60 @@
|
|||
import os
|
||||
import hashlib
|
||||
from flask import Flask, render_template, request, redirect, flash, url_for
|
||||
from werkzeug.middleware.proxy_fix import ProxyFix
|
||||
|
||||
app = Flask(__name__)
|
||||
app.secret_key = os.getenv("SECRET_KEY")
|
||||
BASE_URL = os.getenv("BASE_URL") # Adjust as appropriate for your deployment
|
||||
|
||||
app.wsgi_app = ProxyFix(
|
||||
app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1
|
||||
)
|
||||
|
||||
UPLOAD_FOLDER = os.getenv("UPLOAD_FOLDER")
|
||||
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
||||
|
||||
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
|
||||
|
||||
def allowed_file(filename):
|
||||
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
||||
|
||||
def get_file_hash(file_storage):
|
||||
file_storage.seek(0)
|
||||
sha = hashlib.sha256()
|
||||
while True:
|
||||
chunk = file_storage.read(4096)
|
||||
if not chunk:
|
||||
break
|
||||
sha.update(chunk)
|
||||
hash8 = sha.hexdigest()[:8]
|
||||
file_storage.seek(0)
|
||||
return hash8
|
||||
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
def upload_file():
|
||||
uploaded_url = None
|
||||
if request.method == 'POST':
|
||||
if 'file' not in request.files:
|
||||
flash('No file part', 'error')
|
||||
return redirect(request.url)
|
||||
file = request.files['file']
|
||||
if file.filename == '':
|
||||
flash('No selected file', 'error')
|
||||
return redirect(request.url)
|
||||
if file and allowed_file(file.filename):
|
||||
hash8 = get_file_hash(file)
|
||||
ext = file.filename.rsplit('.', 1)[1].lower()
|
||||
filename = f"{hash8}.{ext}"
|
||||
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
||||
file.save(filepath)
|
||||
# Compose the URL (assuming files will eventually be hosted at /uploads/<filename>)
|
||||
uploaded_url = f"{BASE_URL}/{filename}"
|
||||
return render_template("index.html", uploaded_url=uploaded_url)
|
||||
else:
|
||||
flash('Invalid file extension', 'error')
|
||||
return redirect(request.url)
|
||||
return render_template("index.html", uploaded_url=uploaded_url)
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
Loading…
Add table
Add a link
Reference in a new issue