Spaces:
Runtime error
Runtime error
from flask import Flask, render_template, request, redirect, url_for | |
import os | |
# cors | |
from flask_cors import CORS, cross_origin | |
app = Flask(__name__) | |
cors = CORS(app) | |
# Set upload folder | |
UPLOAD_FOLDER = 'uploads' | |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | |
# Set allowed file extensions | |
ALLOWED_EXTENSIONS = {'xlsx', 'csv'} | |
def allowed_file(filename): | |
"""Helper function to check if a file has an allowed extension""" | |
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS | |
def upload_file(): | |
if request.method == 'POST': | |
# Check if a file was uploaded | |
if 'file' not in request.files: | |
return redirect(request.url) | |
if 'uuid' not in request.form: | |
return "no uuid" | |
file = request.files['file'] | |
# Check if file has an allowed extension | |
if not allowed_file(file.filename): | |
return 'Invalid file type' | |
# get uuid from form | |
uuid = request.form['uuid'] | |
# Save file to upload folder | |
# if folder with name: uuid does not exist, create it | |
if not os.path.exists(os.path.join(app.config['UPLOAD_FOLDER'], uuid)): | |
os.makedirs(os.path.join(app.config['UPLOAD_FOLDER'], uuid)) | |
file.save(os.path.join(app.config['UPLOAD_FOLDER'], uuid, file.filename)) | |
return {"status":"success","filename": file.filename, "uuid": uuid} | |
return "did nothing..." | |
# return render_template('upload.html') | |
def clear_cache(): | |
print(request.json) | |
if request.method == 'POST': | |
if 'uuid' not in request.json: | |
return "no uuid" | |
if 'filename' not in request.json: | |
return "no filename" | |
uuid = request.json['uuid'] | |
folder = os.path.join(app.config['UPLOAD_FOLDER'],uuid) | |
filename = request.json['filename'] | |
if filename in os.listdir(folder): | |
# os.remove(os.path.join(folder,filename)) | |
# rename this file to filename + _old | |
os.rename(os.path.join(folder,filename),os.path.join(folder,filename+"_old")) | |
return {"status":"success"} | |
return "did nothing..." | |
# return render_template('upload.html') | |
if __name__ == '__main__': | |
app.run(debug=True, port=5100) | |