From d736a7b03a610d12dab039dc35846d85f3ff7016 Mon Sep 17 00:00:00 2001 From: Lan Date: Sun, 16 Feb 2025 21:38:44 +0800 Subject: [PATCH] feat:New database migration, new adaptation root path configuration --- apps/base/migrations/migrations_001.py | 52 +++++++++++++++++ apps/base/models.py | 63 ++++++++++++++++++++ core/database.py | 80 ++++++++++++++++++++++++++ main.py | 12 +--- readme.md | 1 + readme_en.md | 9 ++- requirements.txt | 49 ++-------------- 7 files changed, 208 insertions(+), 58 deletions(-) create mode 100644 apps/base/migrations/migrations_001.py create mode 100644 core/database.py diff --git a/apps/base/migrations/migrations_001.py b/apps/base/migrations/migrations_001.py new file mode 100644 index 0000000..2a11960 --- /dev/null +++ b/apps/base/migrations/migrations_001.py @@ -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() diff --git a/apps/base/models.py b/apps/base/models.py index 3224b05..0159e26 100644 --- a/apps/base/models.py +++ b/apps/base/models.py @@ -40,6 +40,44 @@ class FileCodes(Model): 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): # 按时间 if self.expired_at is None: @@ -54,6 +92,31 @@ class FileCodes(Model): 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): id: Optional[int] = fields.IntField(pk=True) key: Optional[str] = fields.CharField( diff --git a/core/database.py b/core/database.py new file mode 100644 index 0000000..d574359 --- /dev/null +++ b/core/database.py @@ -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 diff --git a/main.py b/main.py index c5f3de4..3c24274 100644 --- a/main.py +++ b/main.py @@ -16,6 +16,7 @@ from apps.base.models import KeyValue from apps.base.utils import ip_limit from apps.base.views import share_api from apps.admin.views import admin_api +from core.database import init_db from core.response import APIResponse from core.settings import data_root, settings, BASE_DIR, DEFAULT_CONFIG from core.tasks import delete_expire_files @@ -24,16 +25,6 @@ from contextlib import asynccontextmanager 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 async def lifespan(app: FastAPI): # 初始化数据库 @@ -115,6 +106,7 @@ async def index(): .replace("{{description}}", str(settings.description)) .replace("{{keywords}}", str(settings.keywords)) .replace("{{opacity}}", str(settings.opacity)) + .replace('"/assets/', '"assets/') .replace("{{background}}", str(settings.background)), media_type="text/html", headers={"Cache-Control": "no-cache"}, diff --git a/readme.md b/readme.md index 91fd137..edf54a9 100644 --- a/readme.md +++ b/readme.md @@ -22,6 +22,7 @@ ## 🚀 更新计划 - [ ] 切片上传,同文件秒传,断点续传 +- [x] 适配子目录 - [x] 用户登录重构 - [x] webdav存储 - [x] 存储支持自定义路径 diff --git a/readme_en.md b/readme_en.md index eff700b..cde515d 100644 --- a/readme_en.md +++ b/readme_en.md @@ -28,11 +28,14 @@ FileCodeBox is a lightweight file sharing tool developed with FastAPI + Vue3. It

-Frontend Repository +前端仓库2024 -    + +前端仓库2023 + +     -Demo Site +演示站点

diff --git a/requirements.txt b/requirements.txt index 15da0b1..c397aca 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,49 +1,8 @@ aioboto3==11.2.0 -aiobotocore==2.5.0 -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 +aiohttp==3.10.11 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 -frozenlist==1.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 +pydantic==2.4.0 uvicorn==0.23.2 -wrapt==1.15.0 -yarl==1.9.2 +tortoise-orm==0.20.0 +python-multipart==0.0.20 \ No newline at end of file