-
Notifications
You must be signed in to change notification settings - Fork 0
/
ueditor.py
98 lines (87 loc) · 4.1 KB
/
ueditor.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
93
94
95
96
97
98
"""
File description.
This file is mainly an implementation of ueditor upload
such as sending images, getting content, etc.
encoding: utf-8
@author: Zhang Jiajun
@contact: [email protected]
@software: Pycharm
@time: 2022/1/12
@gituhb: sanxiadaba/pythonBlog
"""
import os
import time
from flask import Blueprint, request, jsonify, session
from common.myLog import rootDir, dirInDir, logDanger, listLogger
from common.ueditorConf import returnUeConf
from common.utility import compress_image
from constant import blogLanguage
from database.logs import Log
from database.upload import Upload
instanceUpload = Upload()
instanceLog = Log()
ueditor = Blueprint("ueditor", __name__)
# Path to the uploaded file
imgSavePath = rootDir + "\\static\\img\\upload"
# Interface for specific access
@ueditor.route("/uedit", methods=["GET", "POST"])
@logDanger
def uedit():
param = request.args.get("action")
if request.method == "GET" and param == "config":
# return render_template("uecConfig.json")
return returnUeConf(session.get("userid"))
else:
userid = session.get("userid")
# Each user has a folder of their own and pictures are uploaded here
myPictureName = "myPic_" + str(userid)
myPicturePath = imgSavePath + "\\" + myPictureName
# Detection of requests sent by front-end ueditor Different requests respond to different states
if request.method == "POST" and request.args.get("action") == "uploadimage":
if instanceUpload.checkLimitUpload() is True:
result = {}
# There is a daily upload limit, and the front-end will prompt if the number of uploads is used up.
if blogLanguage == "Chinese":
result["state"] = "今日上传次数已用完"
else:
result["state"] = "Exceeding the number of times limit"
info = f"The user with userid {userid} failed to upload because he ran out of uploads today."
instanceLog.insertDetail(credit=0, target=0, type="Failed to upload image", info=info)
listLogger(userid, info, 8)
return jsonify(result)
else:
f = request.files["upfile"] # Get data from front-end image files
filename = f.filename
suffix = filename.split(".")[-1] # Get the article's suffix name (not including .)
newname = time.strftime("%Y%m%d_%H%M%S." + suffix)
dirInDir(myPictureName, imgSavePath)
f.save(myPicturePath + "\\" + newname) # Save image
# Compress the image and overwrite the original file
source = dest = myPicturePath + "\\" + newname
# Compressed images
compress_image(source, dest, 1200)
instanceUpload.insertDetail(imgname=source)
info = f"The user with userid {userid} has uploaded the image with path " + source + ""
instanceLog.insertDetail(credit=0, target=0, type="Upload images", info=info)
listLogger(userid, info, [8])
result = {}
# Returns a success status code
result["state"] = "SUCCESS"
result["url"] = f"static/img/upload/{myPictureName}/{newname}"
result['title'] = filename
result["original"] = filename
return jsonify(result)
elif request.method == "GET" and param == "listimage":
list = []
filelist = os.listdir(myPicturePath)
for filename in filelist:
if filename.lower().endswith(".png") or filename.lower().endswith(".jpg") \
or filename.lower().endswith(".jpeg") or filename.lower().endswith(".gif") \
or filename.lower().endswith(".bmp"):
list.append({r"url": r"static/img/upload/%s/%s" % (myPictureName, filename)})
result = {}
result["state"] = "SUCCESS"
result["list"] = list
result["start"] = 0
result["total"] = 50
return jsonify(result)