diff --git a/db.py b/db.py index cdef37c..10d3e40 100644 --- a/db.py +++ b/db.py @@ -35,8 +35,9 @@ def searchByid(id: str): conn.close() return out + # 查找文件信息 -def searchByid(filename: str): +def searchByFilename(filename: str): conn = getConn() c = conn.cursor() cursor = c.execute("SELECT * FROM Metadata WHERE filename = ?", (filename,)) @@ -65,13 +66,17 @@ def newFile(filename: str): conn.close() return suuid + # 获取文件元数据 -def getMetadata(form:int,num:int): +def getMetadata(form: int, num: int): conn = getConn() 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 = [] for row in cursor: out.append(list(row)) conn.close() - return out \ No newline at end of file + return out + diff --git a/file.py b/file.py index a7a9091..c51fe91 100644 --- a/file.py +++ b/file.py @@ -1,9 +1,10 @@ -import shutil, os ,configparser +import shutil, os, configparser, zipfile import db config = configparser.ConfigParser() config.read("./conf/app.ini") + def init(): try: os.makedirs(config.get("file", "inputdir")) @@ -12,9 +13,48 @@ def init(): except: pass + def auotLoadFile(): fileList = os.listdir(config.get("file", "inputdir")) for item in fileList: - db.newFile(item) - shutil.move(config.get("file", "inputdir")+"/"+item, config.get("file", "storedir")+"/"+item) - print("已添加 "+item) + if zipfile.is_zipfile( + config.get("file", "inputdir") + "/" + 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), "" diff --git a/main.py b/main.py index 79e860d..184194d 100644 --- a/main.py +++ b/main.py @@ -52,11 +52,21 @@ def api(): # 接口 return abort(400) -@app.route("/api/img") -def img(): # 图片接口 +@app.route("/api/img//") +def img(bookid, index): # 图片接口 if request.cookies.get("islogin") is None: 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/")