mirror of
https://github.com/Kakune55/ComiPy.git
synced 2025-05-06 18:29:26 +08:00
图片接口基本功能完成
This commit is contained in:
parent
e5a7a53bc9
commit
8d24548e7a
13
db.py
13
db.py
@ -35,8 +35,9 @@ def searchByid(id: str):
|
|||||||
conn.close()
|
conn.close()
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
# 查找文件信息
|
# 查找文件信息
|
||||||
def searchByid(filename: str):
|
def searchByFilename(filename: str):
|
||||||
conn = getConn()
|
conn = getConn()
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
cursor = c.execute("SELECT * FROM Metadata WHERE filename = ?", (filename,))
|
cursor = c.execute("SELECT * FROM Metadata WHERE filename = ?", (filename,))
|
||||||
@ -65,13 +66,17 @@ def newFile(filename: str):
|
|||||||
conn.close()
|
conn.close()
|
||||||
return suuid
|
return suuid
|
||||||
|
|
||||||
|
|
||||||
# 获取文件元数据
|
# 获取文件元数据
|
||||||
def getMetadata(form:int,num:int):
|
def getMetadata(form: int, num: int):
|
||||||
conn = getConn()
|
conn = getConn()
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
cursor = c.execute("SELECT * FROM Metadata ORDER BY num desc LIMIT ?, ?",(form, num))
|
cursor = c.execute(
|
||||||
|
"SELECT * FROM Metadata ORDER BY num desc LIMIT ?, ?", (form, num)
|
||||||
|
)
|
||||||
out = []
|
out = []
|
||||||
for row in cursor:
|
for row in cursor:
|
||||||
out.append(list(row))
|
out.append(list(row))
|
||||||
conn.close()
|
conn.close()
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
48
file.py
48
file.py
@ -1,9 +1,10 @@
|
|||||||
import shutil, os ,configparser
|
import shutil, os, configparser, zipfile
|
||||||
import db
|
import db
|
||||||
|
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config.read("./conf/app.ini")
|
config.read("./conf/app.ini")
|
||||||
|
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
try:
|
try:
|
||||||
os.makedirs(config.get("file", "inputdir"))
|
os.makedirs(config.get("file", "inputdir"))
|
||||||
@ -12,9 +13,48 @@ def init():
|
|||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def auotLoadFile():
|
def auotLoadFile():
|
||||||
fileList = os.listdir(config.get("file", "inputdir"))
|
fileList = os.listdir(config.get("file", "inputdir"))
|
||||||
for item in fileList:
|
for item in fileList:
|
||||||
db.newFile(item)
|
if zipfile.is_zipfile(
|
||||||
shutil.move(config.get("file", "inputdir")+"/"+item, config.get("file", "storedir")+"/"+item)
|
config.get("file", "inputdir") + "/" + item
|
||||||
print("已添加 "+item)
|
): # 判断是否为压缩包
|
||||||
|
db.newFile(item) # 添加数据库记录 移动到存储
|
||||||
|
shutil.move(
|
||||||
|
config.get("file", "inputdir") + "/" + item,
|
||||||
|
config.get("file", "storedir") + "/" + item,
|
||||||
|
)
|
||||||
|
print("已添加 " + item)
|
||||||
|
else:
|
||||||
|
print("不符合条件 " + item)
|
||||||
|
|
||||||
|
|
||||||
|
def raedZip(bookid: str, index: int):
|
||||||
|
bookinfo = db.searchByid(bookid)
|
||||||
|
zippath = config.get("file", "storedir") + "/" + bookinfo[0][2]
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 创建一个ZipFile对象
|
||||||
|
with zipfile.ZipFile(zippath, "r") as zip_ref:
|
||||||
|
# 获取图片文件列表
|
||||||
|
image_files = [
|
||||||
|
file
|
||||||
|
for file in zip_ref.namelist()
|
||||||
|
if file.lower().endswith((".png", ".jpg", ".jpeg"))
|
||||||
|
]
|
||||||
|
|
||||||
|
if not image_files:
|
||||||
|
return "not imgfile in zip", ""
|
||||||
|
|
||||||
|
# 假设我们只提取图片文件
|
||||||
|
image_filename = image_files[int(index)]
|
||||||
|
|
||||||
|
# 读取图片数据
|
||||||
|
image_data = zip_ref.read(image_filename)
|
||||||
|
return image_data, image_filename
|
||||||
|
|
||||||
|
except zipfile.BadZipFile: # 异常处理
|
||||||
|
return "Bad ZipFile", ""
|
||||||
|
except Exception as e:
|
||||||
|
return str(e), ""
|
||||||
|
16
main.py
16
main.py
@ -52,11 +52,21 @@ def api(): # 接口
|
|||||||
return abort(400)
|
return abort(400)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/img")
|
@app.route("/api/img/<bookid>/<index>")
|
||||||
def img(): # 图片接口
|
def img(bookid, index): # 图片接口
|
||||||
if request.cookies.get("islogin") is None:
|
if request.cookies.get("islogin") is None:
|
||||||
return abort(403)
|
return abort(403)
|
||||||
return "Hello World"
|
if db.searchByid(bookid) == "":
|
||||||
|
return jsonify({'message': 'No ZIP file part'}), 400
|
||||||
|
# 设置响应类型为图片
|
||||||
|
data, filename = file.raedZip(bookid,index)
|
||||||
|
if data is str:
|
||||||
|
return data
|
||||||
|
response = make_response(data) #读取文件
|
||||||
|
response.headers.set('Content-Type', 'image/{}'.format(filename.split('.')[-1]))
|
||||||
|
response.headers.set('Content-Disposition', 'inline', filename=filename)
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
@app.route("/book/<bookid>")
|
@app.route("/book/<bookid>")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user