feat(file-check): 新增文件资产管理与清理功能,支持上传、查询、删除及下载
This commit is contained in:
1 parent
143e054d86
commit
729546a7b7
52 files changed
+3420
-34
No files matched your search
@@ -0,0 +1,116 @@
|
||||
"""
|
||||
backend/migrations/versions/c9d0e1f2a3b5_seed_file_asset_cleanup_schedule.py
|
||||
|
||||
用途:为平台 Job file-asset-cleanup 写入默认 ScheduleTask,使 TTL 清理按小时自动执行。
|
||||
依赖:Alembic revision f8a9b0c1d2e3;schedule_tasks / users 表已存在;croniter。
|
||||
逻辑:有用户则插入 SCH-FILE-ASSET-CLEANUP;无用户时跳过,由应用启动/建用户钩子补建。
|
||||
next_run_at 按 Cron「0 * * * *」在 Asia/Shanghai 计算下次整点(与 ensure_* 对齐)。
|
||||
downgrade 仅按本迁移固定 schedule_no 删除。
|
||||
|
||||
Revision ID: c9d0e1f2a3b5
|
||||
Revises: f8a9b0c1d2e3
|
||||
Create Date: 2026-07-14 08:55:00.000000
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision = "c9d0e1f2a3b5"
|
||||
down_revision = "f8a9b0c1d2e3"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
_SCHEDULE_NO = "SCH-FILE-ASSET-CLEANUP"
|
||||
_CRON_EXPR = "0 * * * *"
|
||||
_TZ_NAME = "Asia/Shanghai"
|
||||
|
||||
|
||||
def _compute_next_run_at_utc_naive() -> datetime:
|
||||
"""
|
||||
按「0 * * * *」+ Asia/Shanghai 计算下次整点,返回 UTC naive。
|
||||
与 app.services.schedule.timezone_util.compute_next_run_at 语义对齐。
|
||||
"""
|
||||
try:
|
||||
from croniter import croniter
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
tz = ZoneInfo(_TZ_NAME)
|
||||
base = datetime.now(tz)
|
||||
next_local = croniter(_CRON_EXPR, base).get_next(datetime)
|
||||
if next_local.tzinfo is None:
|
||||
next_local = next_local.replace(tzinfo=tz)
|
||||
return next_local.astimezone(timezone.utc).replace(tzinfo=None)
|
||||
except Exception:
|
||||
# croniter/zoneinfo 不可用时退化:上海时区下一整点
|
||||
tz = timezone(timedelta(hours=8))
|
||||
now_local = datetime.now(tz)
|
||||
next_local = now_local.replace(minute=0, second=0, microsecond=0) + timedelta(hours=1)
|
||||
return next_local.astimezone(timezone.utc).replace(tzinfo=None)
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
conn = op.get_bind()
|
||||
exists = conn.execute(
|
||||
sa.text(
|
||||
"SELECT 1 FROM schedule_tasks "
|
||||
"WHERE schedule_no = :sno OR platform_api_path = 'file-asset-cleanup' "
|
||||
"LIMIT 1"
|
||||
),
|
||||
{"sno": _SCHEDULE_NO},
|
||||
).first()
|
||||
if exists:
|
||||
return
|
||||
|
||||
owner = conn.execute(
|
||||
sa.text("SELECT id FROM users WHERE role = 'admin' ORDER BY id ASC LIMIT 1")
|
||||
).first()
|
||||
if owner is None:
|
||||
owner = conn.execute(sa.text("SELECT id FROM users ORDER BY id ASC LIMIT 1")).first()
|
||||
if owner is None:
|
||||
# 空库无用户:留给 create_app / User after_commit 钩子补建
|
||||
return
|
||||
|
||||
now = datetime.utcnow()
|
||||
next_run = _compute_next_run_at_utc_naive()
|
||||
conn.execute(
|
||||
sa.text(
|
||||
"""
|
||||
INSERT INTO schedule_tasks (
|
||||
schedule_no, name, schedule_type, owner_user_id,
|
||||
cron_expr, schedule_timezone, wait_seconds,
|
||||
platform_api_path, job_params,
|
||||
kb_sync_enabled, notify_enabled,
|
||||
enabled, next_run_at, last_run_at,
|
||||
created_by, updated_by, created_at, updated_at
|
||||
) VALUES (
|
||||
:schedule_no, :name, 'platform_job', :owner_user_id,
|
||||
:cron_expr, 'Asia/Shanghai', 0,
|
||||
'file-asset-cleanup', NULL,
|
||||
false, false,
|
||||
true, :next_run_at, NULL,
|
||||
:owner_user_id, :owner_user_id, :now, :now
|
||||
)
|
||||
"""
|
||||
),
|
||||
{
|
||||
"schedule_no": _SCHEDULE_NO,
|
||||
"name": "临时文件资产 TTL 清理",
|
||||
"owner_user_id": int(owner[0]),
|
||||
"cron_expr": _CRON_EXPR,
|
||||
"next_run_at": next_run,
|
||||
"now": now,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""仅删除本迁移约定的 schedule_no,避免误删管理员手工创建的同路径排程。"""
|
||||
conn = op.get_bind()
|
||||
conn.execute(
|
||||
sa.text("DELETE FROM schedule_tasks WHERE schedule_no = :sno"),
|
||||
{"sno": _SCHEDULE_NO},
|
||||
)
|
||||
+157
@@ -0,0 +1,157 @@
|
||||
"""
|
||||
backend/migrations/versions/f8a9b0c1d2e3_add_file_assets_and_shipping_notice_intakes.py
|
||||
|
||||
用途:第二阶段通用文件资产、发货通知接入记录及任务溯源字段。
|
||||
依赖:Alembic revision e7f8a9b0c1d2。
|
||||
逻辑:创建 file_assets / shipping_notice_intakes;shipping_tasks 增加 source_asset_id/source_intake_id。
|
||||
|
||||
Revision ID: f8a9b0c1d2e3
|
||||
Revises: e7f8a9b0c1d2
|
||||
Create Date: 2026-07-14 00:30:00.000000
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision = "f8a9b0c1d2e3"
|
||||
down_revision = "e7f8a9b0c1d2"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"file_assets",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("asset_no", sa.String(length=64), nullable=False),
|
||||
sa.Column("owner_user_id", sa.Integer(), nullable=False),
|
||||
sa.Column("tenant_id", sa.String(length=64), nullable=True),
|
||||
sa.Column("original_filename", sa.String(length=255), nullable=False),
|
||||
sa.Column("storage_provider", sa.String(length=32), server_default="minio", nullable=False),
|
||||
sa.Column("storage_bucket", sa.String(length=128), nullable=False),
|
||||
sa.Column("storage_key", sa.String(length=512), nullable=False),
|
||||
sa.Column("content_hash", sa.String(length=64), nullable=False),
|
||||
sa.Column("file_size", sa.Integer(), nullable=False),
|
||||
sa.Column("mime_type", sa.String(length=128), nullable=True),
|
||||
sa.Column("extension", sa.String(length=32), nullable=True),
|
||||
sa.Column("upload_status", sa.String(length=32), server_default="uploading", nullable=False),
|
||||
sa.Column("lifecycle_status", sa.String(length=32), server_default="temporary", nullable=False),
|
||||
sa.Column("reference_count", sa.Integer(), server_default="0", nullable=False),
|
||||
sa.Column("expires_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("deleted_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=True),
|
||||
sa.ForeignKeyConstraint(["owner_user_id"], ["users.id"]),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("asset_no", name="uq_file_assets_asset_no"),
|
||||
)
|
||||
op.create_index("ix_file_assets_owner_content_hash", "file_assets", ["owner_user_id", "content_hash"])
|
||||
op.create_index("ix_file_assets_lifecycle_expires", "file_assets", ["lifecycle_status", "expires_at"])
|
||||
op.create_index("ix_file_assets_owner_created", "file_assets", ["owner_user_id", "created_at"])
|
||||
op.create_index(op.f("ix_file_assets_owner_user_id"), "file_assets", ["owner_user_id"], unique=False)
|
||||
|
||||
op.create_table(
|
||||
"shipping_notice_intakes",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("intake_no", sa.String(length=64), nullable=False),
|
||||
sa.Column("asset_id", sa.Integer(), nullable=False),
|
||||
sa.Column("user_id", sa.Integer(), nullable=False),
|
||||
sa.Column("conversation_id", sa.String(length=100), nullable=True),
|
||||
sa.Column("purpose", sa.String(length=32), server_default="shipping_notice", nullable=False),
|
||||
sa.Column("parse_status", sa.String(length=32), server_default="created", nullable=False),
|
||||
sa.Column("extracted_text", sa.Text(), nullable=True),
|
||||
sa.Column("structured_result_json", sa.JSON(), nullable=True),
|
||||
sa.Column("parse_warnings_json", sa.JSON(), nullable=True),
|
||||
sa.Column("parser_version", sa.String(length=32), nullable=False),
|
||||
sa.Column("parse_job_id", sa.Integer(), nullable=True),
|
||||
sa.Column("error_code", sa.String(length=64), nullable=True),
|
||||
sa.Column("error_message", sa.Text(), nullable=True),
|
||||
sa.Column("consumed_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("consumed_batch_id", sa.Integer(), nullable=True),
|
||||
sa.Column("consumed_task_id", sa.Integer(), nullable=True),
|
||||
sa.Column("expires_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=True),
|
||||
sa.ForeignKeyConstraint(["asset_id"], ["file_assets.id"]),
|
||||
sa.ForeignKeyConstraint(["consumed_batch_id"], ["shipping_batches.id"]),
|
||||
sa.ForeignKeyConstraint(["consumed_task_id"], ["shipping_tasks.id"]),
|
||||
sa.ForeignKeyConstraint(["user_id"], ["users.id"]),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("intake_no", name="uq_shipping_notice_intakes_intake_no"),
|
||||
sa.UniqueConstraint(
|
||||
"asset_id",
|
||||
"purpose",
|
||||
"parser_version",
|
||||
name="uq_shipping_notice_intakes_asset_purpose_parser",
|
||||
),
|
||||
)
|
||||
op.create_index(
|
||||
"ix_shipping_notice_intakes_user_conversation_created",
|
||||
"shipping_notice_intakes",
|
||||
["user_id", "conversation_id", "created_at"],
|
||||
)
|
||||
op.create_index(
|
||||
"ix_shipping_notice_intakes_parse_status_created",
|
||||
"shipping_notice_intakes",
|
||||
["parse_status", "created_at"],
|
||||
)
|
||||
op.create_index("ix_shipping_notice_intakes_expires", "shipping_notice_intakes", ["expires_at"])
|
||||
op.create_index(op.f("ix_shipping_notice_intakes_asset_id"), "shipping_notice_intakes", ["asset_id"], unique=False)
|
||||
op.create_index(op.f("ix_shipping_notice_intakes_user_id"), "shipping_notice_intakes", ["user_id"], unique=False)
|
||||
op.create_index(
|
||||
op.f("ix_shipping_notice_intakes_parse_status"),
|
||||
"shipping_notice_intakes",
|
||||
["parse_status"],
|
||||
unique=False,
|
||||
)
|
||||
op.create_index(
|
||||
op.f("ix_shipping_notice_intakes_parse_job_id"),
|
||||
"shipping_notice_intakes",
|
||||
["parse_job_id"],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
with op.batch_alter_table("shipping_tasks", schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column("source_asset_id", sa.Integer(), nullable=True))
|
||||
batch_op.add_column(sa.Column("source_intake_id", sa.Integer(), nullable=True))
|
||||
batch_op.create_foreign_key(
|
||||
"fk_shipping_tasks_source_asset_id_file_assets",
|
||||
"file_assets",
|
||||
["source_asset_id"],
|
||||
["id"],
|
||||
)
|
||||
batch_op.create_foreign_key(
|
||||
"fk_shipping_tasks_source_intake_id_shipping_notice_intakes",
|
||||
"shipping_notice_intakes",
|
||||
["source_intake_id"],
|
||||
["id"],
|
||||
)
|
||||
batch_op.create_index("ix_shipping_tasks_source_asset_id", ["source_asset_id"], unique=False)
|
||||
batch_op.create_index("ix_shipping_tasks_source_intake_id", ["source_intake_id"], unique=False)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
with op.batch_alter_table("shipping_tasks", schema=None) as batch_op:
|
||||
batch_op.drop_index("ix_shipping_tasks_source_intake_id")
|
||||
batch_op.drop_index("ix_shipping_tasks_source_asset_id")
|
||||
batch_op.drop_constraint("fk_shipping_tasks_source_intake_id_shipping_notice_intakes", type_="foreignkey")
|
||||
batch_op.drop_constraint("fk_shipping_tasks_source_asset_id_file_assets", type_="foreignkey")
|
||||
batch_op.drop_column("source_intake_id")
|
||||
batch_op.drop_column("source_asset_id")
|
||||
|
||||
op.drop_index(op.f("ix_shipping_notice_intakes_parse_job_id"), table_name="shipping_notice_intakes")
|
||||
op.drop_index(op.f("ix_shipping_notice_intakes_parse_status"), table_name="shipping_notice_intakes")
|
||||
op.drop_index(op.f("ix_shipping_notice_intakes_user_id"), table_name="shipping_notice_intakes")
|
||||
op.drop_index(op.f("ix_shipping_notice_intakes_asset_id"), table_name="shipping_notice_intakes")
|
||||
op.drop_index("ix_shipping_notice_intakes_expires", table_name="shipping_notice_intakes")
|
||||
op.drop_index("ix_shipping_notice_intakes_parse_status_created", table_name="shipping_notice_intakes")
|
||||
op.drop_index("ix_shipping_notice_intakes_user_conversation_created", table_name="shipping_notice_intakes")
|
||||
op.drop_table("shipping_notice_intakes")
|
||||
|
||||
op.drop_index(op.f("ix_file_assets_owner_user_id"), table_name="file_assets")
|
||||
op.drop_index("ix_file_assets_owner_created", table_name="file_assets")
|
||||
op.drop_index("ix_file_assets_lifecycle_expires", table_name="file_assets")
|
||||
op.drop_index("ix_file_assets_owner_content_hash", table_name="file_assets")
|
||||
op.drop_table("file_assets")
|
||||
Reference in new issue
Block a user