得到图片的过程优化, 已有的图片验证sha256后可复用

This commit is contained in:
weiye.wang 2024-05-12 18:51:46 +08:00
parent 5f60529a0e
commit b17f89c466
2 changed files with 35 additions and 14 deletions

View File

@ -4,7 +4,7 @@
<title>输入备注</title> <title>输入备注</title>
<style> <style>
img { img {
width: 30%; /* Set the width of the image */ width: 50%; /* Set the width of the image */
height: auto; /* Maintain aspect ratio */ height: auto; /* Maintain aspect ratio */
} }
</style> </style>

View File

@ -1,5 +1,5 @@
from flask import Flask, render_template, request, redirect, url_for from flask import Flask, render_template, request, redirect, url_for
import os,shutil import os,shutil,hashlib
from database_tools_2 import * from database_tools_2 import *
app = Flask(__name__) app = Flask(__name__)
@ -19,25 +19,46 @@ def show_image():
mycursor.execute(sql,val) mycursor.execute(sql,val)
content = mycursor.fetchall()[0][0] content = mycursor.fetchall()[0][0]
mydb.close() mydb.close()
latex_raw = ReadTextFile("模板文件/独立文件模板.txt") content_hash = hashlib.sha256(content.encode("utf8")).hexdigest()
latexdata = StringSubstitute(r"<<待替换\d*>>",latex_raw,[content]) # print(content_hash,type(content_hash))
makedir("临时文件/pics") mydb = connect(hostname = db_host, port = db_port, username=db_user, pwd=db_pwd, db = "tikupics")
currentdate = GetDate() mycursor = mydb.cursor()
currenttime = GetTime() mycursor.execute("SELECT hash,pic FROM pics WHERE ID = %s;",(id,))
SaveTextFile(latexdata,f"临时文件/pics/ID{id}-{currentdate}-{currenttime}.tex") ret_list = mycursor.fetchall()
os.system(f"xelatex -interaction=batchmode -output-directory=临时文件/pics ID{id}-{currentdate}-{currenttime}.tex") if len(ret_list) > 0 and ret_list[0][0] == content_hash:
os.chdir("临时文件/pics") with open(f"static/{id}.png","wb") as f:
os.system(f"pdftocairo -png -r 600 ID{id}-{currentdate}-{currenttime}.pdf") f.write(ret_list[0][1])
os.chdir("../..") else:
shutil.copy(f"临时文件/pics/ID{id}-{currentdate}-{currenttime}-1.png",f"static/{id}.png") latex_raw = ReadTextFile("模板文件/独立文件模板.txt")
latexdata = StringSubstitute(r"<<待替换\d*>>",latex_raw,[content])
makedir("临时文件/pics")
currentdate = GetDate()
currenttime = GetTime()
SaveTextFile(latexdata,f"临时文件/pics/ID{id}-{currentdate}-{currenttime}.tex")
os.system(f"xelatex -interaction=batchmode -output-directory=临时文件/pics ID{id}-{currentdate}-{currenttime}.tex")
os.chdir("临时文件/pics")
os.system(f"pdftocairo -png -r 600 ID{id}-{currentdate}-{currenttime}.pdf")
os.chdir("../..")
shutil.copy(f"临时文件/pics/ID{id}-{currentdate}-{currenttime}-1.png",f"static/{id}.png")
with open(f"static/{id}.png","rb") as f:
image_data = f.read()
mycursor.execute("INSERT INTO pics (ID,pic,date,hash) VALUES (%s,%s,%s,%s)",(id,image_data,currentdate,content_hash))
mydb.commit()
mydb.close()
return render_template('show_image.html', image_path=f'/static/{id}.png') return render_template('show_image.html', image_path=f'/static/{id}.png')
@app.route('/submit_remarks', methods=['POST']) @app.route('/submit_remarks', methods=['POST'])
def submit_remarks(): def submit_remarks():
category = request.form['category'] category = request.form['category']
remarks = request.form['remarks'] remarks = request.form['remarks']
# date = GetDate()
# mydb = connect(hostname = db_host, port = db_port, username=db_user, pwd=db_pwd, db = "tiku")
# mycursor = mydb.cursor()
# mydb.close()
return render_template('show_remarks.html', category=category, remarks=remarks) return render_template('show_remarks.html', category=category, remarks=remarks)
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=True) app.run(debug=True)