packServer/backend/app/model/file_record.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

32 lines
1.5 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)
*
* 只登记「上传成功」的对象;引用关系不落库,由 `service/fileStorage` 实时判断
* (用户头像 / 文档正文),所以删除业务数据后不会留下脏引用。
*/
module.exports = (app) => {
const { BIGINT, STRING } = app.Sequelize;
const FileRecord = app.model.define('file_record', {
id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' },
// `key` 是 MySQL 保留字,落库列名用 object_key
key: { type: STRING(512), allowNull: false, unique: true, field: 'object_key', comment: 'COS 对象键' },
url: { type: STRING(512), allowNull: false, comment: '对外访问地址' },
fileName: { type: STRING(255), allowNull: false, defaultValue: '', comment: '原始文件名' },
size: { type: BIGINT.UNSIGNED, allowNull: false, defaultValue: 0, comment: '字节数' },
mimeType: { 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' },
uploaderId: { type: BIGINT.UNSIGNED, allowNull: true, comment: '上传者 users.id' },
uploaderName: { type: STRING(64), allowNull: false, defaultValue: '', comment: '上传者快照' },
}, {
tableName: 'file_records',
paranoid: true,
comment: '上传文件登记表',
});
return FileRecord;
};