'use strict'; /** * 文件资源模型,对应 files 表 * 同一主体下按内容 hash 去重(uk_files_principal_hash),磁盘路径按「主体ID/年/月/日」分层 * @param {Egg.Application} app egg application * @return {object} file model */ module.exports = app => { const { BIGINT, STRING, INTEGER, DATE } = app.Sequelize; const File = app.model.define('file', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' }, principal_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID' }, hash: { type: STRING(64), allowNull: false, comment: '文件内容 SHA256,用于去重' }, name: { type: STRING(255), allowNull: false, comment: '原始文件名' }, ext: { type: STRING(20), allowNull: false, comment: '扩展名(小写,含点,如 .png)' }, mime: { type: STRING(100), comment: 'MIME 类型' }, size: { type: INTEGER.UNSIGNED, allowNull: false, defaultValue: 0, comment: '文件大小(字节)' }, path: { type: STRING(255), allowNull: false, comment: '相对存储路径:主体ID/年/月/日/hash.ext' }, url: { type: STRING(500), allowNull: false, comment: '访问地址' }, created_at: { type: DATE, comment: '创建时间' }, updated_at: { type: DATE, comment: '更新时间' }, deleted_at: { type: DATE, comment: '软删除时间' }, }, { paranoid: true, deletedAt: 'deleted_at', }); File.associate = () => { app.model.File.belongsTo(app.model.Principal, { foreignKey: 'principal_id', as: 'principal' }); }; return File; };