File size: 2,410 Bytes
a987248
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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

@app.route('/upload', methods=['GET', 'POST'])
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')

@app.route('/clearcache', methods=['POST'])
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)