feat:New database migration, new adaptation root path configuration
This commit is contained in:
@@ -0,0 +1,52 @@
|
|||||||
|
from tortoise import connections
|
||||||
|
|
||||||
|
|
||||||
|
async def create_file_codes_table():
|
||||||
|
conn = connections.get("default")
|
||||||
|
await conn.execute_script(
|
||||||
|
"""
|
||||||
|
CREATE TABLE IF NOT EXISTS filecodes
|
||||||
|
(
|
||||||
|
id INTEGER not null
|
||||||
|
primary key autoincrement,
|
||||||
|
code VARCHAR(255) not null
|
||||||
|
unique,
|
||||||
|
prefix VARCHAR(255) default '' not null,
|
||||||
|
suffix VARCHAR(255) default '' not null,
|
||||||
|
uuid_file_name VARCHAR(255),
|
||||||
|
file_path VARCHAR(255),
|
||||||
|
size INT default 0 not null,
|
||||||
|
text TEXT,
|
||||||
|
expired_at TIMESTAMP,
|
||||||
|
expired_count INT default 0 not null,
|
||||||
|
used_count INT default 0 not null,
|
||||||
|
created_at TIMESTAMP default CURRENT_TIMESTAMP not null
|
||||||
|
);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_filecodes_code_1c7ee7
|
||||||
|
on filecodes (code);
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def create_key_value_table():
|
||||||
|
conn = connections.get("default")
|
||||||
|
await conn.execute_script(
|
||||||
|
"""
|
||||||
|
CREATE TABLE IF NOT EXISTS keyvalue
|
||||||
|
(
|
||||||
|
id INTEGER not null
|
||||||
|
primary key autoincrement,
|
||||||
|
key VARCHAR(255) not null
|
||||||
|
unique,
|
||||||
|
value JSON,
|
||||||
|
created_at TIMESTAMP default CURRENT_TIMESTAMP not null
|
||||||
|
);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_keyvalue_key_eab890
|
||||||
|
on keyvalue (key);
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def migrate():
|
||||||
|
await create_file_codes_table()
|
||||||
|
await create_key_value_table()
|
||||||
@@ -40,6 +40,44 @@ class FileCodes(Model):
|
|||||||
auto_now_add=True, description="创建时间"
|
auto_now_add=True, description="创建时间"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
file_hash = fields.CharField(
|
||||||
|
max_length=128, # SHA-256需要64字符,这里预留扩展空间
|
||||||
|
description="文件哈希值",
|
||||||
|
unique=True,
|
||||||
|
null=True # 允许旧数据为空
|
||||||
|
)
|
||||||
|
hash_algorithm = fields.CharField(
|
||||||
|
max_length=20,
|
||||||
|
description="哈希算法类型",
|
||||||
|
null=True,
|
||||||
|
default="sha256"
|
||||||
|
)
|
||||||
|
|
||||||
|
class FileCodes(Model):
|
||||||
|
# 新增分片字段
|
||||||
|
chunk_size = fields.IntField(
|
||||||
|
description="分片大小(字节)",
|
||||||
|
default=0
|
||||||
|
)
|
||||||
|
total_chunks = fields.IntField(
|
||||||
|
description="总分片数",
|
||||||
|
default=0
|
||||||
|
)
|
||||||
|
uploaded_chunks = fields.IntField(
|
||||||
|
description="已上传分片数",
|
||||||
|
default=0
|
||||||
|
)
|
||||||
|
upload_status = fields.CharField(
|
||||||
|
max_length=20,
|
||||||
|
description="上传状态",
|
||||||
|
default="pending", # pending/in_progress/completed
|
||||||
|
choices=["pending", "in_progress", "completed"]
|
||||||
|
)
|
||||||
|
is_chunked = fields.BooleanField(
|
||||||
|
description="是否分片上传",
|
||||||
|
default=False
|
||||||
|
)
|
||||||
|
|
||||||
async def is_expired(self):
|
async def is_expired(self):
|
||||||
# 按时间
|
# 按时间
|
||||||
if self.expired_at is None:
|
if self.expired_at is None:
|
||||||
@@ -54,6 +92,31 @@ class FileCodes(Model):
|
|||||||
return f"{self.file_path}/{self.uuid_file_name}"
|
return f"{self.file_path}/{self.uuid_file_name}"
|
||||||
|
|
||||||
|
|
||||||
|
class FileChunks(Model):
|
||||||
|
id = fields.IntField(pk=True)
|
||||||
|
file_code = fields.ForeignKeyField(
|
||||||
|
"models.FileCodes",
|
||||||
|
related_name="chunks",
|
||||||
|
on_delete=fields.CASCADE
|
||||||
|
)
|
||||||
|
chunk_number = fields.IntField(description="分片序号")
|
||||||
|
chunk_hash = fields.CharField(
|
||||||
|
max_length=128,
|
||||||
|
description="分片哈希校验值"
|
||||||
|
)
|
||||||
|
chunk_path = fields.CharField(
|
||||||
|
max_length=255,
|
||||||
|
description="分片存储路径"
|
||||||
|
)
|
||||||
|
created_at = fields.DatetimeField(
|
||||||
|
auto_now_add=True,
|
||||||
|
description="上传时间"
|
||||||
|
)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
unique_together = [("file_code", "chunk_number")]
|
||||||
|
|
||||||
|
|
||||||
class KeyValue(Model):
|
class KeyValue(Model):
|
||||||
id: Optional[int] = fields.IntField(pk=True)
|
id: Optional[int] = fields.IntField(pk=True)
|
||||||
key: Optional[str] = fields.CharField(
|
key: Optional[str] = fields.CharField(
|
||||||
|
|||||||
@@ -0,0 +1,80 @@
|
|||||||
|
import glob
|
||||||
|
import importlib
|
||||||
|
import os
|
||||||
|
|
||||||
|
from tortoise import Tortoise
|
||||||
|
|
||||||
|
from core.settings import data_root
|
||||||
|
|
||||||
|
|
||||||
|
async def init_db():
|
||||||
|
try:
|
||||||
|
# 使用正确的Tortoise初始化配置格式
|
||||||
|
db_config = {
|
||||||
|
"db_url": f"sqlite://{data_root}/filecodebox.db",
|
||||||
|
"modules": {"models": ["apps.base.models"]},
|
||||||
|
"use_tz": False,
|
||||||
|
"timezone": "Asia/Shanghai"
|
||||||
|
}
|
||||||
|
|
||||||
|
await Tortoise.init(**db_config)
|
||||||
|
|
||||||
|
# 创建migrations表
|
||||||
|
await Tortoise.get_connection("default").execute_script("""
|
||||||
|
CREATE TABLE IF NOT EXISTS migrates (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
migration_file VARCHAR(255) NOT NULL UNIQUE,
|
||||||
|
executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
)
|
||||||
|
""")
|
||||||
|
|
||||||
|
# 执行迁移
|
||||||
|
await execute_migrations()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"数据库初始化失败: {str(e)}")
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
async def execute_migrations():
|
||||||
|
"""执行数据库迁移"""
|
||||||
|
try:
|
||||||
|
# 收集迁移文件
|
||||||
|
migration_files = []
|
||||||
|
for root, dirs, files in os.walk("apps"):
|
||||||
|
if "migrations" in dirs:
|
||||||
|
migration_path = os.path.join(root, "migrations")
|
||||||
|
migration_files.extend(glob.glob(os.path.join(migration_path, "migrations_*.py")))
|
||||||
|
|
||||||
|
# 按文件名排序
|
||||||
|
migration_files.sort()
|
||||||
|
|
||||||
|
for migration_file in migration_files:
|
||||||
|
file_name = os.path.basename(migration_file)
|
||||||
|
|
||||||
|
# 检查是否已执行
|
||||||
|
executed = await Tortoise.get_connection("default").execute_query(
|
||||||
|
"SELECT id FROM migrates WHERE migration_file = ?", [file_name]
|
||||||
|
)
|
||||||
|
|
||||||
|
if not executed[1]:
|
||||||
|
print(f"执行迁移: {file_name}")
|
||||||
|
# 导入并执行migration
|
||||||
|
module_path = migration_file.replace("/", ".").replace("\\", ".").replace(".py", "")
|
||||||
|
try:
|
||||||
|
migration_module = importlib.import_module(module_path)
|
||||||
|
if hasattr(migration_module, "migrate"):
|
||||||
|
await migration_module.migrate()
|
||||||
|
# 记录执行
|
||||||
|
await Tortoise.get_connection("default").execute_query(
|
||||||
|
"INSERT INTO migrates (migration_file) VALUES (?)",
|
||||||
|
[file_name]
|
||||||
|
)
|
||||||
|
print(f"迁移完成: {file_name}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"迁移 {file_name} 执行失败: {str(e)}")
|
||||||
|
raise
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"迁移过程发生错误: {str(e)}")
|
||||||
|
raise
|
||||||
@@ -16,6 +16,7 @@ from apps.base.models import KeyValue
|
|||||||
from apps.base.utils import ip_limit
|
from apps.base.utils import ip_limit
|
||||||
from apps.base.views import share_api
|
from apps.base.views import share_api
|
||||||
from apps.admin.views import admin_api
|
from apps.admin.views import admin_api
|
||||||
|
from core.database import init_db
|
||||||
from core.response import APIResponse
|
from core.response import APIResponse
|
||||||
from core.settings import data_root, settings, BASE_DIR, DEFAULT_CONFIG
|
from core.settings import data_root, settings, BASE_DIR, DEFAULT_CONFIG
|
||||||
from core.tasks import delete_expire_files
|
from core.tasks import delete_expire_files
|
||||||
@@ -24,16 +25,6 @@ from contextlib import asynccontextmanager
|
|||||||
from tortoise import Tortoise
|
from tortoise import Tortoise
|
||||||
|
|
||||||
|
|
||||||
async def init_db():
|
|
||||||
await Tortoise.init(
|
|
||||||
db_url=f"sqlite://{data_root}/filecodebox.db",
|
|
||||||
modules={"models": ["apps.base.models"]},
|
|
||||||
use_tz=False,
|
|
||||||
timezone="Asia/Shanghai",
|
|
||||||
)
|
|
||||||
await Tortoise.generate_schemas()
|
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
# 初始化数据库
|
# 初始化数据库
|
||||||
@@ -115,6 +106,7 @@ async def index():
|
|||||||
.replace("{{description}}", str(settings.description))
|
.replace("{{description}}", str(settings.description))
|
||||||
.replace("{{keywords}}", str(settings.keywords))
|
.replace("{{keywords}}", str(settings.keywords))
|
||||||
.replace("{{opacity}}", str(settings.opacity))
|
.replace("{{opacity}}", str(settings.opacity))
|
||||||
|
.replace('"/assets/', '"assets/')
|
||||||
.replace("{{background}}", str(settings.background)),
|
.replace("{{background}}", str(settings.background)),
|
||||||
media_type="text/html",
|
media_type="text/html",
|
||||||
headers={"Cache-Control": "no-cache"},
|
headers={"Cache-Control": "no-cache"},
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
## 🚀 更新计划
|
## 🚀 更新计划
|
||||||
- [ ] 切片上传,同文件秒传,断点续传
|
- [ ] 切片上传,同文件秒传,断点续传
|
||||||
|
- [x] 适配子目录
|
||||||
- [x] 用户登录重构
|
- [x] 用户登录重构
|
||||||
- [x] webdav存储
|
- [x] webdav存储
|
||||||
- [x] 存储支持自定义路径
|
- [x] 存储支持自定义路径
|
||||||
|
|||||||
+6
-3
@@ -28,11 +28,14 @@ FileCodeBox is a lightweight file sharing tool developed with FastAPI + Vue3. It
|
|||||||
<div align="center">
|
<div align="center">
|
||||||
<h3>
|
<h3>
|
||||||
<a href="https://github.com/vastsa/FileCodeBoxFronted" target="_blank">
|
<a href="https://github.com/vastsa/FileCodeBoxFronted" target="_blank">
|
||||||
<img src="https://img.shields.io/badge/Frontend-FileCodeBoxFronted-blue?style=for-the-badge&logo=github" alt="Frontend Repository">
|
<img src="https://img.shields.io/badge/Frontend-FileCodeBoxFronted2024-blue?style=for-the-badge&logo=github" alt="前端仓库2024">
|
||||||
</a>
|
</a>
|
||||||
|
<a href="https://github.com/vastsa/FileCodeBoxFronted2023" target="_blank">
|
||||||
|
<img src="https://img.shields.io/badge/Frontend-FileCodeBoxFronted2023-blue?style=for-the-badge&logo=github" alt="前端仓库2023">
|
||||||
|
</a>
|
||||||
|
|
||||||
<a href="https://share.lanol.cn" target="_blank">
|
<a href="https://share.lanol.cn" target="_blank">
|
||||||
<img src="https://img.shields.io/badge/Demo-share.lanol.cn-green?style=for-the-badge&logo=internet-explorer" alt="Demo Site">
|
<img src="https://img.shields.io/badge/Demo-share.lanol.cn-green?style=for-the-badge&logo=internet-explorer" alt="演示站点">
|
||||||
</a>
|
</a>
|
||||||
</h3>
|
</h3>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
+4
-45
@@ -1,49 +1,8 @@
|
|||||||
aioboto3==11.2.0
|
aioboto3==11.2.0
|
||||||
aiobotocore==2.5.0
|
aiohttp==3.10.11
|
||||||
aiohttp==3.9.2
|
|
||||||
aioitertools==0.11.0
|
|
||||||
aiosignal==1.3.1
|
|
||||||
aiosqlite==0.17.0
|
|
||||||
annotated-types==0.5.0
|
|
||||||
anyio==3.7.1
|
|
||||||
async-timeout==4.0.3
|
|
||||||
atlastk==0.13.2
|
|
||||||
attrs==23.1.0
|
|
||||||
boto3==1.26.76
|
|
||||||
botocore==1.29.76
|
botocore==1.29.76
|
||||||
certifi==2024.8.30
|
|
||||||
cffi==1.17.1
|
|
||||||
charset-normalizer==3.2.0
|
|
||||||
click==8.1.6
|
|
||||||
cryptography==43.0.1
|
|
||||||
exceptiongroup==1.1.2
|
|
||||||
fastapi==0.115.0
|
fastapi==0.115.0
|
||||||
frozenlist==1.4.0
|
pydantic==2.4.0
|
||||||
greenlet==2.0.2
|
|
||||||
h11==0.14.0
|
|
||||||
idna==3.7
|
|
||||||
iso8601==1.1.0
|
|
||||||
jmespath==1.0.1
|
|
||||||
msal==1.31.0
|
|
||||||
multidict==6.0.4
|
|
||||||
Office365-REST-Python-Client==2.5.2
|
|
||||||
pycparser==2.22
|
|
||||||
pydantic==2.1.1
|
|
||||||
pydantic_core==2.4.0
|
|
||||||
PyJWT==2.9.0
|
|
||||||
pypika-tortoise==0.1.6
|
|
||||||
python-dateutil==2.8.2
|
|
||||||
python-multipart==0.0.7
|
|
||||||
pytz==2023.3
|
|
||||||
requests==2.32.3
|
|
||||||
s3transfer==0.6.1
|
|
||||||
six==1.16.0
|
|
||||||
sniffio==1.3.0
|
|
||||||
SQLAlchemy==2.0.19
|
|
||||||
starlette==0.38.6
|
|
||||||
tortoise-orm==0.20.0
|
|
||||||
typing_extensions==4.8.0
|
|
||||||
urllib3==1.26.20
|
|
||||||
uvicorn==0.23.2
|
uvicorn==0.23.2
|
||||||
wrapt==1.15.0
|
tortoise-orm==0.20.0
|
||||||
yarl==1.9.2
|
python-multipart==0.0.20
|
||||||
Reference in New Issue
Block a user