packServer/backend/database/migrations/20261123170000-create-file-records.js
liufumingren bde5dc7bad feat(backend): 图片上传接入腾讯云 COS,新增存储管理与未引用清理
- 头像 / 文档图片改由后端校验并转发到 COS,密钥只留在服务端
- 新增 file_records 登记表(迁移 20261123170000),记录对象键、大小、上传者等
- 引用关系实时计算(用户头像 / 文档正文),改删业务数据后状态自动同步
- 新增平台管理「存储管理」页:引用状态与明细、单删、两步清理未引用
- 新增接口 GET /api/web/admin/files、POST /api/web/admin/files/cleanup、
  DELETE /api/web/admin/files/:id(仍被引用默认 409,force 才删)
- COS 目录前缀(COS_DIR_AVATAR / COS_DIR_DOC)、大小上限、清理保护期改为可配置

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-23 16:39:47 +08:00

46 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 上传文件登记表(file_records)
*
* 每次上传成功都会登记一行:COS 对象键、访问地址、大小、类型、上传者。
* **引用关系不单独存表**:管理端查询时实时判断(用户头像 / 文档正文),
* 这样业务侧改动(改头像、删文档)不需要同步维护引用行,也不会出现脏引用。
*
* 扩展新的引用来源时,只需在 `service/fileStorage` 的 REFERENCE_SOURCES 里加一条 EXISTS 片段。
*/
module.exports = {
async up(queryInterface, Sequelize) {
const { BIGINT, STRING, DATE } = Sequelize;
await queryInterface.createTable('file_records', {
id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false },
object_key: { type: STRING(512), allowNull: false, comment: 'COS 对象键(相对路径,唯一)' },
url: { type: STRING(512), allowNull: false, comment: '对外访问地址(CDN / COS 域名 + 对象键)' },
file_name: { type: STRING(255), allowNull: false, defaultValue: '', comment: '上传时的原始文件名' },
size: { type: BIGINT.UNSIGNED, allowNull: false, defaultValue: 0, comment: '文件大小(字节)' },
mime_type: { type: STRING(100), allowNull: false, defaultValue: '', comment: 'MIME 类型' },
hash: { type: STRING(64), allowNull: false, defaultValue: '', comment: '内容 SHA256(校验 / 排查重复)' },
category: { type: STRING(32), allowNull: false, defaultValue: '', comment: '业务分类:avatar 头像 / doc 文档图片' },
uploader_id: { type: BIGINT.UNSIGNED, allowNull: true, comment: '上传者 users.id' },
uploader_name: { type: STRING(64), allowNull: false, defaultValue: '', comment: '上传者快照(昵称/用户名)' },
created_at: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') },
updated_at: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') },
deleted_at: { type: DATE, allowNull: true },
}, { comment: '上传文件登记表(COS 对象与引用追踪)' });
await queryInterface.addIndex('file_records', [ 'object_key' ], {
name: 'uk_file_records_key',
unique: true,
});
await queryInterface.addIndex('file_records', [ 'category', 'created_at' ], {
name: 'idx_file_records_category_created',
});
await queryInterface.addIndex('file_records', [ 'uploader_id' ], { name: 'idx_file_records_uploader' });
},
async down(queryInterface) {
await queryInterface.dropTable('file_records');
},
};