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
|
import shortuuid
|
||||||
|
import db.util as util
|
||||||
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()
|
|
||||||
|
|
||||||
|
|
||||||
# 查找文件信息
|
# 查找文件信息
|
||||||
def searchByid(id: str):
|
def searchByid(id: str):
|
||||||
conn = getConn()
|
conn = util.getConn()
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
cursor = c.execute("SELECT * FROM Metadata WHERE id = ?", (id,))
|
cursor = c.execute("SELECT * FROM Metadata WHERE id = ?", (id,))
|
||||||
out = []
|
out = []
|
||||||
@ -38,8 +15,8 @@ def searchByid(id: str):
|
|||||||
|
|
||||||
|
|
||||||
# 查找文件信息
|
# 查找文件信息
|
||||||
def searchByFilename(filename: str):
|
def searchByname(filename: str):
|
||||||
conn = getConn()
|
conn = util.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,))
|
||||||
out = []
|
out = []
|
||||||
@ -50,18 +27,18 @@ def searchByFilename(filename: str):
|
|||||||
|
|
||||||
|
|
||||||
# 在数据库中添加一个新的文件记录
|
# 在数据库中添加一个新的文件记录
|
||||||
def newFile(filename: str, pagenumber:int):
|
def new(filename: str):
|
||||||
suuid = shortuuid.random(8)
|
suuid = shortuuid.random(8)
|
||||||
conn = getConn()
|
conn = util.getConn()
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
c.execute(
|
c.execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO Metadata
|
INSERT INTO Metadata
|
||||||
(id, filename, pagenumber)
|
(id, filename)
|
||||||
VALUES
|
VALUES
|
||||||
(?, ?, ?);
|
(?, ?, ?);
|
||||||
""",
|
""",
|
||||||
(suuid, filename, pagenumber),
|
(suuid, filename),
|
||||||
)
|
)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
conn.close()
|
||||||
@ -70,7 +47,7 @@ def newFile(filename: str, pagenumber:int):
|
|||||||
|
|
||||||
# 获取文件元数据
|
# 获取文件元数据
|
||||||
def getMetadata(form: int, num: int):
|
def getMetadata(form: int, num: int):
|
||||||
conn = getConn()
|
conn = util.getConn()
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
cursor = c.execute(
|
cursor = c.execute(
|
||||||
"SELECT * FROM Metadata ORDER BY num desc LIMIT ?, ?", (form, num)
|
"SELECT * FROM Metadata ORDER BY num desc LIMIT ?, ?", (form, num)
|
||||||
@ -80,4 +57,3 @@ def getMetadata(form: int, num: int):
|
|||||||
out.append(list(row))
|
out.append(list(row))
|
||||||
conn.close()
|
conn.close()
|
||||||
return out
|
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 shutil, os, configparser, zipfile, io
|
||||||
import db
|
import db.file
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
@ -24,7 +24,7 @@ def auotLoadFile():
|
|||||||
with zipfile.ZipFile(
|
with zipfile.ZipFile(
|
||||||
config.get("file", "inputdir") + "/" + item, "r"
|
config.get("file", "inputdir") + "/" + item, "r"
|
||||||
) as zip_ref:
|
) as zip_ref:
|
||||||
db.newFile(item, len(zip_ref.namelist())) # 添加数据库记录 移动到存储
|
db.file.new(item) # 添加数据库记录 移动到存储
|
||||||
shutil.move(
|
shutil.move(
|
||||||
config.get("file", "inputdir") + "/" + item,
|
config.get("file", "inputdir") + "/" + item,
|
||||||
config.get("file", "storedir") + "/" + item,
|
config.get("file", "storedir") + "/" + item,
|
||||||
@ -35,7 +35,7 @@ def auotLoadFile():
|
|||||||
|
|
||||||
|
|
||||||
def raedZip(bookid: str, index: int):
|
def raedZip(bookid: str, index: int):
|
||||||
bookinfo = db.searchByid(bookid)
|
bookinfo = db.file.searchByid(bookid)
|
||||||
zippath = config.get("file", "storedir") + "/" + bookinfo[0][2]
|
zippath = config.get("file", "storedir") + "/" + bookinfo[0][2]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
5
main.py
5
main.py
@ -1,5 +1,6 @@
|
|||||||
import configparser
|
import configparser
|
||||||
import db, file
|
import db.util
|
||||||
|
import db.file, file
|
||||||
from flask import *
|
from flask import *
|
||||||
|
|
||||||
from web.api_Img import api_Img_bp
|
from web.api_Img import api_Img_bp
|
||||||
@ -13,7 +14,7 @@ config.read("./conf/app.ini")
|
|||||||
|
|
||||||
def appinit():
|
def appinit():
|
||||||
file.init()
|
file.init()
|
||||||
db.init()
|
db.util.init()
|
||||||
file.auotLoadFile()
|
file.auotLoadFile()
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
from flask import *
|
from flask import *
|
||||||
from flask import Blueprint
|
from flask import Blueprint
|
||||||
import db, file
|
import db.file , file
|
||||||
|
|
||||||
api_Img_bp = Blueprint("api_Img_bp", __name__)
|
api_Img_bp = Blueprint("api_Img_bp", __name__)
|
||||||
|
|
||||||
@ -9,7 +9,7 @@ api_Img_bp = Blueprint("api_Img_bp", __name__)
|
|||||||
def img(bookid, index): # 图片接口
|
def img(bookid, index): # 图片接口
|
||||||
if request.cookies.get("islogin") is None:
|
if request.cookies.get("islogin") is None:
|
||||||
return abort(403)
|
return abort(403)
|
||||||
if db.searchByid(bookid) == "":
|
if db.file.searchByid(bookid) == "":
|
||||||
return abort(404)
|
return abort(404)
|
||||||
# 设置响应类型为图片
|
# 设置响应类型为图片
|
||||||
data, filename = file.raedZip(bookid, index)
|
data, filename = file.raedZip(bookid, index)
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
from flask import *
|
from flask import *
|
||||||
from flask import Blueprint
|
from flask import Blueprint
|
||||||
import configparser
|
import configparser
|
||||||
import db, file
|
import db.file , file
|
||||||
|
|
||||||
page_bp = Blueprint("page_bp", __name__)
|
page_bp = Blueprint("page_bp", __name__)
|
||||||
|
|
||||||
@ -14,7 +14,7 @@ def overview(page): # 概览
|
|||||||
page = int(page)
|
page = int(page)
|
||||||
if request.cookies.get("islogin") is None:
|
if request.cookies.get("islogin") is None:
|
||||||
return redirect("/")
|
return redirect("/")
|
||||||
metaDataList = db.getMetadata((page - 1) * 20, page * 20)
|
metaDataList = db.file.getMetadata((page - 1) * 20, page * 20)
|
||||||
if page <= 3:
|
if page <= 3:
|
||||||
lastPageList = range(1, page)
|
lastPageList = range(1, page)
|
||||||
else:
|
else:
|
||||||
@ -33,7 +33,7 @@ def overview(page): # 概览
|
|||||||
def book(bookid): # 接口
|
def book(bookid): # 接口
|
||||||
if request.cookies.get("islogin") is None:
|
if request.cookies.get("islogin") is None:
|
||||||
return abort(403)
|
return abort(403)
|
||||||
data = db.searchByid(bookid)
|
data = db.file.searchByid(bookid)
|
||||||
if data == "":
|
if data == "":
|
||||||
return abort(404)
|
return abort(404)
|
||||||
return render_template("view.html", id=bookid, index=range(1, data[0][3]))
|
return render_template("view.html", id=bookid, index=range(1, data[0][3]))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user