mirror of
https://github.com/Kakune55/ComiPy.git
synced 2025-05-06 18:29:26 +08:00
基本项目结构完成
This commit is contained in:
parent
c4c0291455
commit
b36e50cbc5
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
__pycache__
|
||||||
|
data
|
||||||
|
input
|
||||||
|
test.py
|
16
conf/app.ini
Normal file
16
conf/app.ini
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
[server]
|
||||||
|
port=8080
|
||||||
|
debug=True
|
||||||
|
host=0.0.0.0
|
||||||
|
|
||||||
|
[user]
|
||||||
|
username=admin
|
||||||
|
password=admin
|
||||||
|
|
||||||
|
[database]
|
||||||
|
path=./data/metadata.db
|
||||||
|
|
||||||
|
[file]
|
||||||
|
inputdir=./input
|
||||||
|
storedir=./data/file
|
||||||
|
tmpdir=./data/tmp
|
77
db.py
Normal file
77
db.py
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
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
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
# 查找文件信息
|
||||||
|
def searchByid(id: str):
|
||||||
|
conn = getConn()
|
||||||
|
c = conn.cursor()
|
||||||
|
cursor = c.execute("SELECT * FROM Metadata WHERE id = ?", (id,))
|
||||||
|
out = []
|
||||||
|
for row in cursor:
|
||||||
|
out.append(row)
|
||||||
|
conn.close()
|
||||||
|
return out
|
||||||
|
|
||||||
|
# 查找文件信息
|
||||||
|
def searchByid(filename: str):
|
||||||
|
conn = getConn()
|
||||||
|
c = conn.cursor()
|
||||||
|
cursor = c.execute("SELECT * FROM Metadata WHERE filename = ?", (filename,))
|
||||||
|
out = []
|
||||||
|
for row in cursor:
|
||||||
|
out.append(row)
|
||||||
|
conn.close()
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
# 在数据库中添加一个新的文件记录
|
||||||
|
def newFile(filename: str):
|
||||||
|
suuid = shortuuid.random(8)
|
||||||
|
conn = getConn()
|
||||||
|
c = conn.cursor()
|
||||||
|
c.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO Metadata
|
||||||
|
(id, filename)
|
||||||
|
VALUES
|
||||||
|
(?, ?);
|
||||||
|
""",
|
||||||
|
(suuid, filename),
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
return suuid
|
||||||
|
|
||||||
|
# 获取文件元数据
|
||||||
|
def getMetadata(form:int,num:int):
|
||||||
|
conn = getConn()
|
||||||
|
c = conn.cursor()
|
||||||
|
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
|
20
file.py
Normal file
20
file.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import shutil, os ,configparser
|
||||||
|
import db
|
||||||
|
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read("./conf/app.ini")
|
||||||
|
|
||||||
|
def init():
|
||||||
|
try:
|
||||||
|
os.makedirs(config.get("file", "inputdir"))
|
||||||
|
os.makedirs(config.get("file", "storedir"))
|
||||||
|
os.makedirs(config.get("file", "tmpdir"))
|
||||||
|
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)
|
82
main.py
Normal file
82
main.py
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
import configparser, os
|
||||||
|
import db, file
|
||||||
|
from flask import *
|
||||||
|
|
||||||
|
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read("./conf/app.ini")
|
||||||
|
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def appinit():
|
||||||
|
file.init()
|
||||||
|
db.init()
|
||||||
|
file.auotLoadFile()
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/", methods=["GET", "POST"])
|
||||||
|
def login(): # 登录页面
|
||||||
|
if request.method == "GET":
|
||||||
|
if request.cookies.get("islogin") is not None:
|
||||||
|
return redirect("/overview")
|
||||||
|
return render_template("login.html")
|
||||||
|
elif request.method == "POST":
|
||||||
|
if request.form["username"] == config.get("user", "username") and request.form[
|
||||||
|
"password"
|
||||||
|
] == config.get("user", "password"):
|
||||||
|
resp = make_response(redirect("/overview"))
|
||||||
|
resp.set_cookie("islogin", "True")
|
||||||
|
return resp
|
||||||
|
else:
|
||||||
|
return redirect("/")
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/overview")
|
||||||
|
def overview(): # 概览
|
||||||
|
if request.cookies.get("islogin") is None:
|
||||||
|
return redirect("/")
|
||||||
|
return config.get("server", "port")
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/info")
|
||||||
|
def api(): # 接口
|
||||||
|
if request.cookies.get("islogin") is None:
|
||||||
|
return abort(403)
|
||||||
|
func = request.args.get("func")
|
||||||
|
if func == "bookname" and request.args.get("page") is not None:
|
||||||
|
page = int(request.args.get("page"))
|
||||||
|
return db.getMetadata((page - 1) * 20, page * 20)
|
||||||
|
|
||||||
|
return abort(400)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/img")
|
||||||
|
def img(): # 图片接口
|
||||||
|
if request.cookies.get("islogin") is None:
|
||||||
|
return abort(403)
|
||||||
|
return "Hello World"
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/book/<bookid>")
|
||||||
|
def book(bookid): # 接口
|
||||||
|
if request.cookies.get("islogin") is None:
|
||||||
|
return abort(403)
|
||||||
|
return bookid
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/view/<bookid>")
|
||||||
|
def view(bookid): # 接口
|
||||||
|
if request.cookies.get("islogin") is None:
|
||||||
|
return abort(403)
|
||||||
|
return bookid
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
appinit()
|
||||||
|
app.run(
|
||||||
|
debug=config.get("server", "debug"),
|
||||||
|
host=config.get("server", "host"),
|
||||||
|
port=config.get("server", "port"),
|
||||||
|
)
|
Loading…
x
Reference in New Issue
Block a user