From 79beba473e0cd89a32d99bf4767a95c447ee7f7a Mon Sep 17 00:00:00 2001 From: Kakune55 Date: Mon, 15 Apr 2024 15:19:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db.py => db/file.py | 44 ++++++++++---------------------------------- db/user.py | 36 ++++++++++++++++++++++++++++++++++++ db/util.py | 33 +++++++++++++++++++++++++++++++++ file.py | 6 +++--- main.py | 5 +++-- web/api_Img.py | 4 ++-- web/page.py | 6 +++--- 7 files changed, 90 insertions(+), 44 deletions(-) rename db.py => db/file.py (57%) create mode 100644 db/user.py create mode 100644 db/util.py diff --git a/db.py b/db/file.py similarity index 57% rename from db.py rename to db/file.py index 7486c31..549a10e 100644 --- a/db.py +++ b/db/file.py @@ -1,33 +1,10 @@ -import sqlite3, configparser, shortuuid - -config = configparser.ConfigParser() -config.read("./conf/app.ini") - - -def getConn(): - return sqlite3.connect(config.get("database", "path")) - - -def init(): - conn = getConn() - c = conn.cursor() - c.execute( - """ - CREATE TABLE IF NOT EXISTS Metadata ( - num INTEGER PRIMARY KEY AUTOINCREMENT, - id TEXT NOT NULL, - filename TEXT NOT NULL, - pagenumber INT NOT NULL - ); - """ - ) - conn.commit() - conn.close() +import shortuuid +import db.util as util # 查找文件信息 def searchByid(id: str): - conn = getConn() + conn = util.getConn() c = conn.cursor() cursor = c.execute("SELECT * FROM Metadata WHERE id = ?", (id,)) out = [] @@ -38,8 +15,8 @@ def searchByid(id: str): # 查找文件信息 -def searchByFilename(filename: str): - conn = getConn() +def searchByname(filename: str): + conn = util.getConn() c = conn.cursor() cursor = c.execute("SELECT * FROM Metadata WHERE filename = ?", (filename,)) out = [] @@ -50,18 +27,18 @@ def searchByFilename(filename: str): # 在数据库中添加一个新的文件记录 -def newFile(filename: str, pagenumber:int): +def new(filename: str): suuid = shortuuid.random(8) - conn = getConn() + conn = util.getConn() c = conn.cursor() c.execute( """ INSERT INTO Metadata - (id, filename, pagenumber) + (id, filename) VALUES (?, ?, ?); """, - (suuid, filename, pagenumber), + (suuid, filename), ) conn.commit() conn.close() @@ -70,7 +47,7 @@ def newFile(filename: str, pagenumber:int): # 获取文件元数据 def getMetadata(form: int, num: int): - conn = getConn() + conn = util.getConn() c = conn.cursor() cursor = c.execute( "SELECT * FROM Metadata ORDER BY num desc LIMIT ?, ?", (form, num) @@ -80,4 +57,3 @@ def getMetadata(form: int, num: int): out.append(list(row)) conn.close() return out - diff --git a/db/user.py b/db/user.py new file mode 100644 index 0000000..fbaee9e --- /dev/null +++ b/db/user.py @@ -0,0 +1,36 @@ +import sqlite3, configparser +import db.util as util + +config = configparser.ConfigParser() +config.read("./conf/app.ini") + + +def getConn(): + return sqlite3.connect(config.get("database", "path")) + + +def new(username: str, password: int): + "新建用户" + conn = util.getConn() + c = conn.cursor() + c.execute( + """ + INSERT INTO User + (username, password) + VALUES + (?, ?); + """, + (username, password), + ) + conn.commit() + conn.close() + + +def check(username: str, password: int): + "判断用户信息是否正确" + conn = util.getConn() + c = conn.cursor() + cursor = c.execute("SELECT * FROM User WHERE username = ? AND password = ?", (username, password)) + if cursor.fetchone() is None: + return False + return True \ No newline at end of file diff --git a/db/util.py b/db/util.py new file mode 100644 index 0000000..d47471c --- /dev/null +++ b/db/util.py @@ -0,0 +1,33 @@ +import sqlite3, configparser + +config = configparser.ConfigParser() +config.read("./conf/app.ini") + + +def getConn(): + return sqlite3.connect(config.get("database", "path")) + + +def init(): + conn = getConn() + c = conn.cursor() + c.execute( + """ + CREATE TABLE IF NOT EXISTS Metadata ( + num INTEGER PRIMARY KEY AUTOINCREMENT, + id TEXT NOT NULL, + filename TEXT NOT NULL + ); + """ + ) + c.execute( + """ + CREATE TABLE IF NOT EXISTS User ( + uid INTEGER PRIMARY KEY AUTOINCREMENT, + username TEXT NOT NULL, + password TEXT NOT NULL + ); + """ + ) + conn.commit() + conn.close() diff --git a/file.py b/file.py index 03cbaeb..976dc34 100644 --- a/file.py +++ b/file.py @@ -1,5 +1,5 @@ import shutil, os, configparser, zipfile, io -import db +import db.file from PIL import Image config = configparser.ConfigParser() @@ -24,7 +24,7 @@ def auotLoadFile(): with zipfile.ZipFile( config.get("file", "inputdir") + "/" + item, "r" ) as zip_ref: - db.newFile(item, len(zip_ref.namelist())) # 添加数据库记录 移动到存储 + db.file.new(item) # 添加数据库记录 移动到存储 shutil.move( config.get("file", "inputdir") + "/" + item, config.get("file", "storedir") + "/" + item, @@ -35,7 +35,7 @@ def auotLoadFile(): def raedZip(bookid: str, index: int): - bookinfo = db.searchByid(bookid) + bookinfo = db.file.searchByid(bookid) zippath = config.get("file", "storedir") + "/" + bookinfo[0][2] try: diff --git a/main.py b/main.py index 478474b..c73c362 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,6 @@ import configparser -import db, file +import db.util +import db.file, file from flask import * from web.api_Img import api_Img_bp @@ -13,7 +14,7 @@ config.read("./conf/app.ini") def appinit(): file.init() - db.init() + db.util.init() file.auotLoadFile() diff --git a/web/api_Img.py b/web/api_Img.py index d7f8910..27bc48d 100644 --- a/web/api_Img.py +++ b/web/api_Img.py @@ -1,6 +1,6 @@ from flask import * from flask import Blueprint -import db, file +import db.file , file api_Img_bp = Blueprint("api_Img_bp", __name__) @@ -9,7 +9,7 @@ api_Img_bp = Blueprint("api_Img_bp", __name__) def img(bookid, index): # 图片接口 if request.cookies.get("islogin") is None: return abort(403) - if db.searchByid(bookid) == "": + if db.file.searchByid(bookid) == "": return abort(404) # 设置响应类型为图片 data, filename = file.raedZip(bookid, index) diff --git a/web/page.py b/web/page.py index 7aaf5d3..3f84324 100644 --- a/web/page.py +++ b/web/page.py @@ -1,7 +1,7 @@ from flask import * from flask import Blueprint import configparser -import db, file +import db.file , file page_bp = Blueprint("page_bp", __name__) @@ -14,7 +14,7 @@ def overview(page): # 概览 page = int(page) if request.cookies.get("islogin") is None: return redirect("/") - metaDataList = db.getMetadata((page - 1) * 20, page * 20) + metaDataList = db.file.getMetadata((page - 1) * 20, page * 20) if page <= 3: lastPageList = range(1, page) else: @@ -33,7 +33,7 @@ def overview(page): # 概览 def book(bookid): # 接口 if request.cookies.get("islogin") is None: return abort(403) - data = db.searchByid(bookid) + data = db.file.searchByid(bookid) if data == "": return abort(404) return render_template("view.html", id=bookid, index=range(1, data[0][3]))