-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
73 lines (55 loc) · 2.41 KB
/
app.py
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
from flask import Flask, request, render_template, flash, redirect, url_for, jsonify
import os
import random
import string
app = Flask(__name__)
app.secret_key = ''
# 指定存储目录
IMAGE_UPLOAD_FOLDER = '/mnt/Images'
SCHEM_UPLOAD_FOLDER = '/mnt/schematics'
ALLOWED_IMAGE_EXTENSIONS = {'jpg', 'jpeg', 'png'}
ALLOWED_SCHEM_EXTENSIONS = {'schem', 'schematic'}
# 创建上传目录
os.makedirs(IMAGE_UPLOAD_FOLDER, exist_ok=True)
os.makedirs(SCHEM_UPLOAD_FOLDER, exist_ok=True)
def allowed_file(filename, allowed_extensions):
"""检查文件扩展名是否被允许"""
return '.' in filename and filename.rsplit('.', 1)[1].lower() in allowed_extensions
def generate_random_filename(extension, upload_folder):
"""生成唯一的随机文件名"""
while True:
random_code = ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
filename = f"{random_code}.{extension}"
# 检查文件是否已存在
if not os.path.exists(os.path.join(upload_folder, filename)):
return filename
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# 检查是否有文件上传
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# 检查文件名
if file.filename == '':
flash('No selected file')
return redirect(request.url)
file_extension = file.filename.rsplit('.', 1)[1].lower()
# 判断文件类型并保存
if allowed_file(file.filename, ALLOWED_IMAGE_EXTENSIONS):
random_filename = generate_random_filename(file_extension, IMAGE_UPLOAD_FOLDER)
file.save(os.path.join(IMAGE_UPLOAD_FOLDER, random_filename))
flash('Image uploaded successfully!')
elif allowed_file(file.filename, ALLOWED_SCHEM_EXTENSIONS):
random_filename = generate_random_filename(file_extension, SCHEM_UPLOAD_FOLDER)
file.save(os.path.join(SCHEM_UPLOAD_FOLDER, random_filename))
flash('Schematic file uploaded successfully!')
else:
flash('Invalid file type')
return redirect(request.url)
# 返回生成的文件名
return jsonify({"filename": random_filename})
return render_template('upload.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=45000, debug=True)