Skip to content

Commit 96e2233

Browse files
authored
Merge pull request #19 from diskcloud/feat/plus
feat: #18 Feature - Add processing filesize module
2 parents 2a73bac + a03b7c0 commit 96e2233

5 files changed

Lines changed: 16 additions & 7 deletions

File tree

middleware/authenticateToken.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const isWhitelisted = (url, method) => {
2525
const authenticateToken = async (ctx, next) => {
2626
// token 不存在并且在白名单类,免除校验
2727
const token = ctx.headers["authorization"]?.replace("Bearer ", "");
28-
2928
if (isWhitelisted(ctx.path, ctx.method) && !token) {
3029
await next();
3130
return;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"bcrypt": "^5.1.1",
1919
"dotenv": "^16.4.5",
2020
"file-type": "^19.0.0",
21+
"filesize": "^10.1.2",
2122
"joi": "^17.13.3",
2223
"jsonwebtoken": "^9.0.2",
2324
"jszip": "^3.10.1",

routers/files.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ const sharp = require("sharp");
66
const tinify = require("tinify");
77
const { Op } = require("sequelize");
88
const { v4: uuidv4 } = require("uuid");
9+
const JSZip = require("jszip");
10+
const { filesize } = require("filesize");
11+
912
const { detectFileType } = require("../utils/detectFileType");
1013
const Files = require("../models/files");
11-
const JSZip = require("jszip");
1214
const {
1315
imageMimeTypes,
1416
tinifySupportedMimeTypes,
@@ -224,7 +226,10 @@ router.get("/files", validateQuery(FILES_LIST_GET_QUERY), async (ctx) => {
224226
});
225227

226228
ctx.body = {
227-
items: rows,
229+
items: rows.map((file) => ({
230+
...file.dataValues,
231+
file_size: filesize(file.file_size),
232+
})),
228233
total: count,
229234
};
230235
} catch (error) {
@@ -432,7 +437,6 @@ router.delete("/files", validateBody(FILES_BODY_BATCH_IDS), async (ctx) => {
432437
router.get("/files/:id/preview", validateParams(FILES_REST_ID), async (ctx) => {
433438
const { id } = ctx.params;
434439
const { type } = ctx.query; // 获取查询参数 'type',可以是 'thumb' 或 'original'
435-
436440
try {
437441
const file = await Files.findOne({
438442
where: {
@@ -441,7 +445,7 @@ router.get("/files/:id/preview", validateParams(FILES_REST_ID), async (ctx) => {
441445
[Op.or]: [
442446
{ public_expiration: null, is_public: true },
443447
{ public_expiration: { [Op.gt]: new Date() }, is_public: true },
444-
{ created_by: ctx.state.user.id },
448+
{ created_by: ctx.state?.user?.id || null },
445449
],
446450
},
447451
attributes: [

routers/users.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ router.get("/users/info", async (ctx) => {
109109
router.post("/logout", async (ctx) => {
110110
const { id } = ctx.state.user;
111111
if (!ctx.state.token) {
112-
ctx.status = 400;
113-
ctx.body = { message: "Token is required" };
112+
ctx.status = 200;
113+
ctx.body = { message: "Invalid Token" };
114114
return;
115115
}
116116

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,11 @@ file-type@^19.0.0:
987987
strtok3 "^7.0.0"
988988
token-types "^5.0.1"
989989

990+
filesize@^10.1.2:
991+
version "10.1.2"
992+
resolved "https://registry.yarnpkg.com/filesize/-/filesize-10.1.2.tgz#33bb71c5c134102499f1bc36e6f2863137f6cb0c"
993+
integrity sha512-Dx770ai81ohflojxhU+oG+Z2QGvKdYxgEr9OSA8UVrqhwNHjfH9A8f5NKfg83fEH8ZFA5N5llJo5T3PIoZ4CRA==
994+
990995
fill-range@^7.1.1:
991996
version "7.1.1"
992997
resolved "https://registry.npmmirror.com/fill-range/-/fill-range-7.1.1.tgz"

0 commit comments

Comments
 (0)