This repository was archived by the owner on Mar 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
92 lines (71 loc) · 2.32 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/python3
# Auther: Ayatale
import os
from datetime import timedelta
from uploader import image_upload
from werkzeug.utils import secure_filename
from flask import Flask, url_for, request, redirect, render_template
# 启动flask
app = Flask(__name__)
# 定义图片的保存路径
base_path = os.path.join(os.path.dirname(__file__), "static", "img")
# 设置静态文件缓存过期时间
app.send_file_max_age_default = timedelta(seconds=1)
# 设置允许的文件格式
ALLOWED = set(["jpg", "png", "jpeg", "gif", "JPG", "PNG", "JPEG", "GIF"])
ERROR = "请检查上传的图片类型,仅限于jpg、png、gif"
# 判断文件是否为图片
def check_file(filename):
return "." in filename and filename.rsplit(".", 1)[1] in ALLOWED
# 图片保存函数
def save_img():
img = request.files["file"]
# 检查图片格式
if not (img and check_file(img.filename)):
return
# 保存图片
img_name = secure_filename(img.filename)
save_path = os.path.join(base_path, img_name)
img.save(save_path)
return save_path
# 设置网页后端
@app.route("/upload", methods=["POST", "GET"])
def upload_web():
if request.method == "POST":
save_path = save_img()
if not save_path:
return render_template("index.html")
# 上传到B站
result_url = image_upload(save_path, 2)
# 显示结果
return {"code": "success", "data": {"url": result_url}}
return render_template("index.html")
# 设置api接口
@app.route("/", methods=["POST", "GET"])
def upload_full():
# 如果是浏览器访问则跳转至路由/upload
if request.method == "GET":
return redirect(url_for("upload_web"))
# 保存图片
save_path = save_img()
if not save_path:
return ERROR
# 调用上传函数,返回图片链接
return image_upload(save_path, 0)
@app.route("/long", methods=["POST"])
def upload_long():
save_path = save_img()
if not save_path:
return ERROR
return image_upload(save_path, 1)
@app.route("/short", methods=["POST"])
def upload_short():
save_path = save_img()
if not save_path:
return ERROR
return image_upload(save_path, 2)
@app.route("/favicon<ico>")
def return_logo():
return "Null"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=2000, debug=1, threaded=0)