mirror of
https://github.com/Kakune55/ComiPy.git
synced 2025-05-06 18:29:26 +08:00
数据库接口重构
This commit is contained in:
parent
372435d224
commit
79beba473e
@ -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
|
||||
|
36
db/user.py
Normal file
36
db/user.py
Normal file
@ -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
|
33
db/util.py
Normal file
33
db/util.py
Normal file
@ -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()
|
6
file.py
6
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:
|
||||
|
5
main.py
5
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()
|
||||
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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]))
|
||||
|
Loading…
x
Reference in New Issue
Block a user