From a03b7c03b89a482a0263253fff7a14b50c199253 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=A5=E5=AE=B6=E8=BE=89?= Date: Tue, 2 Jul 2024 22:54:51 +0800 Subject: [PATCH] feat: #18 Feature - Add processing filesize module --- middleware/authenticateToken.js | 1 - package.json | 1 + routers/files.js | 12 ++++++++---- routers/users.js | 4 ++-- yarn.lock | 5 +++++ 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/middleware/authenticateToken.js b/middleware/authenticateToken.js index 7d356fd..8552e36 100644 --- a/middleware/authenticateToken.js +++ b/middleware/authenticateToken.js @@ -25,7 +25,6 @@ const isWhitelisted = (url, method) => { const authenticateToken = async (ctx, next) => { // token 不存在并且在白名单类,免除校验 const token = ctx.headers["authorization"]?.replace("Bearer ", ""); - if (isWhitelisted(ctx.path, ctx.method) && !token) { await next(); return; diff --git a/package.json b/package.json index f32c71a..b57fabd 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "bcrypt": "^5.1.1", "dotenv": "^16.4.5", "file-type": "^19.0.0", + "filesize": "^10.1.2", "joi": "^17.13.3", "jsonwebtoken": "^9.0.2", "jszip": "^3.10.1", diff --git a/routers/files.js b/routers/files.js index 006d3d9..0c61be2 100644 --- a/routers/files.js +++ b/routers/files.js @@ -6,9 +6,11 @@ const sharp = require("sharp"); const tinify = require("tinify"); const { Op } = require("sequelize"); const { v4: uuidv4 } = require("uuid"); +const JSZip = require("jszip"); +const { filesize } = require("filesize"); + const { detectFileType } = require("../utils/detectFileType"); const Files = require("../models/files"); -const JSZip = require("jszip"); const { imageMimeTypes, tinifySupportedMimeTypes, @@ -224,7 +226,10 @@ router.get("/files", validateQuery(FILES_LIST_GET_QUERY), async (ctx) => { }); ctx.body = { - items: rows, + items: rows.map((file) => ({ + ...file.dataValues, + file_size: filesize(file.file_size), + })), total: count, }; } catch (error) { @@ -432,7 +437,6 @@ router.delete("/files", validateBody(FILES_BODY_BATCH_IDS), async (ctx) => { router.get("/files/:id/preview", validateParams(FILES_REST_ID), async (ctx) => { const { id } = ctx.params; const { type } = ctx.query; // 获取查询参数 'type',可以是 'thumb' 或 'original' - try { const file = await Files.findOne({ where: { @@ -441,7 +445,7 @@ router.get("/files/:id/preview", validateParams(FILES_REST_ID), async (ctx) => { [Op.or]: [ { public_expiration: null, is_public: true }, { public_expiration: { [Op.gt]: new Date() }, is_public: true }, - { created_by: ctx.state.user.id }, + { created_by: ctx.state?.user?.id || null }, ], }, attributes: [ diff --git a/routers/users.js b/routers/users.js index 44bfc2f..db87cf3 100644 --- a/routers/users.js +++ b/routers/users.js @@ -109,8 +109,8 @@ router.get("/users/info", async (ctx) => { router.post("/logout", async (ctx) => { const { id } = ctx.state.user; if (!ctx.state.token) { - ctx.status = 400; - ctx.body = { message: "Token is required" }; + ctx.status = 200; + ctx.body = { message: "Invalid Token" }; return; } diff --git a/yarn.lock b/yarn.lock index 17404d2..73b283b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -987,6 +987,11 @@ file-type@^19.0.0: strtok3 "^7.0.0" token-types "^5.0.1" +filesize@^10.1.2: + version "10.1.2" + resolved "https://registry.yarnpkg.com/filesize/-/filesize-10.1.2.tgz#33bb71c5c134102499f1bc36e6f2863137f6cb0c" + integrity sha512-Dx770ai81ohflojxhU+oG+Z2QGvKdYxgEr9OSA8UVrqhwNHjfH9A8f5NKfg83fEH8ZFA5N5llJo5T3PIoZ4CRA== + fill-range@^7.1.1: version "7.1.1" resolved "https://registry.npmmirror.com/fill-range/-/fill-range-7.1.1.tgz"