Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const usersRouter = require("./routers/users");
const redisClient = require("./redis");
const authenticateToken = require("./middleware/authenticateToken");
const cors = require("@koa/cors");

require("./models");
require("dotenv").config({ path: ".env.local" });

const app = new Koa();
Expand Down
2 changes: 1 addition & 1 deletion middleware/authenticateToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const isWhitelisted = (url, method) => {
const authenticateToken = async (ctx, next) => {
// token 不存在并且在白名单类,免除校验
const token = ctx.headers["authorization"]?.replace("Bearer ", "");
if (isWhitelisted(ctx.path, ctx.method) && !token) {
if (isWhitelisted(ctx.path, ctx.method)) {
await next();
return;
}
Expand Down
199 changes: 102 additions & 97 deletions models/files.js
Original file line number Diff line number Diff line change
@@ -1,99 +1,104 @@
const { DataTypes } = require('sequelize');
const sequelize = require('../utils/dbInstance'); // 修改为实际的sequelize实例路径
const Files = sequelize.define('Files', {
id: {
type: DataTypes.STRING(50),
allowNull: false, // 必须为 NOT NULL
primaryKey: true,
},
filename: {
type: DataTypes.STRING(255),
allowNull: false,
},
file_size: {
type: DataTypes.BIGINT,
allowNull: false,
},
file_location: {
type: DataTypes.STRING(255),
allowNull: false,
},
created_by: {
type: DataTypes.STRING(255),
allowNull: false,
},
created_at: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: DataTypes.NOW,
},
updated_by: {
type: DataTypes.STRING(255),
allowNull: true,
defaultValue: null,
},
updated_at: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: DataTypes.NOW,
onUpdate: DataTypes.NOW,
},
is_public: {
type: DataTypes.BOOLEAN,
allowNull: true,
defaultValue: false,
},
public_expiration: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: null,
},
public_by: {
type: DataTypes.STRING(255),
allowNull: true,
defaultValue: null,
},
is_thumb: {
type: DataTypes.BOOLEAN,
allowNull: true,
defaultValue: null,
},
thumb_location: {
type: DataTypes.STRING(255),
allowNull: true,
defaultValue: null,
},
is_delete: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
real_file_location: {
type: DataTypes.STRING(255),
allowNull: true,
defaultValue: null,
},
real_file_thumb_location: {
type: DataTypes.STRING(255),
allowNull: true,
defaultValue: null,
},
mime: {
type: DataTypes.STRING(255),
allowNull: true,
defaultValue: null,
},
ext: {
type: DataTypes.STRING(50),
allowNull: true,
defaultValue: null,
},
}, {
tableName: 'files',
timestamps: false,
underscored: true,
charset: 'utf8mb4',
collate: 'utf8mb4_general_ci',
});
const { DataTypes } = require("sequelize");
const sequelize = require("../utils/dbInstance"); // 修改为实际的sequelize实例路径

const Files = sequelize.define(
"Files",
{
id: {
type: DataTypes.STRING(50),
allowNull: false, // 必须为 NOT NULL
primaryKey: true,
},
filename: {
type: DataTypes.STRING(255),
allowNull: false,
},
file_size: {
type: DataTypes.BIGINT,
allowNull: false,
},
file_location: {
type: DataTypes.STRING(255),
allowNull: false,
},
created_by: {
type: DataTypes.INTEGER,
allowNull: false,
},
created_at: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: DataTypes.NOW,
},
updated_by: {
type: DataTypes.INTEGER,
allowNull: true,
defaultValue: null,
},
updated_at: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: DataTypes.NOW,
onUpdate: DataTypes.NOW,
},
is_public: {
type: DataTypes.BOOLEAN,
allowNull: true,
defaultValue: false,
},
public_expiration: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: null,
},
public_by: {
type: DataTypes.INTEGER,
allowNull: true,
defaultValue: null,
},
is_thumb: {
type: DataTypes.BOOLEAN,
allowNull: true,
defaultValue: null,
},
thumb_location: {
type: DataTypes.STRING(255),
allowNull: true,
defaultValue: null,
},
is_delete: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
real_file_location: {
type: DataTypes.STRING(255),
allowNull: true,
defaultValue: null,
},
real_file_thumb_location: {
type: DataTypes.STRING(255),
allowNull: true,
defaultValue: null,
},
mime: {
type: DataTypes.STRING(255),
allowNull: true,
defaultValue: null,
},
ext: {
type: DataTypes.STRING(50),
allowNull: true,
defaultValue: null,
},
},
{
tableName: "files",
timestamps: false,
underscored: true,
charset: "utf8mb4",
collate: "utf8mb4_general_ci",
}
);

module.exports = Files;
11 changes: 11 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const Users = require("./users");
const Files = require("./files");

// 定义关联关系
Users.hasMany(Files, { as: "createdFiles", foreignKey: "created_by" });
Users.hasMany(Files, { as: "updatedFiles", foreignKey: "updated_by" });
Users.hasMany(Files, { as: "publicFiles", foreignKey: "public_by" });

Files.belongsTo(Users, { as: "creator", foreignKey: "created_by" });
Files.belongsTo(Users, { as: "updater", foreignKey: "updated_by" });
Files.belongsTo(Users, { as: "publisher", foreignKey: "public_by" });
2 changes: 1 addition & 1 deletion models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const { DataTypes } = require("sequelize");
const sequelize = require("../utils/dbInstance");
const { USER_STATUS } = require("../constants/users");

// 定义 User 模型
const Users = sequelize.define(
"User",
{
Expand Down Expand Up @@ -80,4 +79,5 @@ const Users = sequelize.define(
collate: "utf8mb4_unicode_ci",
}
);

module.exports = Users;
9 changes: 6 additions & 3 deletions routers/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { filesize } = require("filesize");

const { detectFileType } = require("../utils/detectFileType");
const Files = require("../models/files");
const Users = require("../models/users");
const {
imageMimeTypes,
tinifySupportedMimeTypes,
Expand Down Expand Up @@ -209,14 +210,16 @@ router.get("/files", validateQuery(FILES_LIST_GET_QUERY), async (ctx) => {
},
limit,
offset,
include: [
{ model: Users, as: "creator", attributes: ["id", "username"] },
{ model: Users, as: "updater", attributes: ["id", "username"] },
{ model: Users, as: "publisher", attributes: ["id", "username"] },
],
attributes: [
"id",
"created_by",
"created_at",
"public_by",
"public_expiration",
"updated_at",
"updated_by",
"file_size",
"filename",
"file_location",
Expand Down