Skip to content

Commit

Permalink
Merge pull request #14 from CrazyMrYan/feat/full/1.2.0
Browse files Browse the repository at this point in the history
feat: #8 Soft deletion of files
  • Loading branch information
CrazyMrYan authored Jun 25, 2024
2 parents 9286116 + 9d9df1f commit fe74c08
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 32 deletions.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// app.js
const Koa = require('koa');
const { koaBody } = require('koa-body');
const {koaBody} = require('koa-body');
const path = require('path');
const fs = require('fs');
const sequelize = require('./utils/dbInstance'); // 确保路径正确
Expand All @@ -10,6 +10,7 @@ require('dotenv').config({ path: '.env.local' });

const app = new Koa();


app.use(require('koa-static')(path.join(__dirname, 'public')));

const createDirectories = () => {
Expand All @@ -29,13 +30,16 @@ createDirectories();
app.use(
koaBody({
multipart: true,
// 解决 DELETE 没法获取ids的问题
parsedMethods: ['POST', 'PUT', 'PATCH', 'DELETE'],
formidable: {
uploadDir: path.join(__dirname, 'provisional'), // 临时上传目录
keepExtensions: true // 保留文件扩展名
}
})
);


// 挂载文件路由
app.use(filesRouter.routes()).use(filesRouter.allowedMethods());

Expand Down
90 changes: 85 additions & 5 deletions routers/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ router.post("/files", async (ctx) => {
await fsp.unlink(file.filepath);
}
}
ctx.status(201);
ctx.status = 201;
ctx.body = fileList.length > 1 ? responses : responses[0];
} catch (error) {
ctx.status = 500;
Expand Down Expand Up @@ -309,6 +309,86 @@ router.put('/files/:id', async (ctx) => {
}
});

// 文件删除接口
router.delete('/files/:id', async (ctx) => {
const { id } = ctx.params;

try {
// 查找文件
const file = await Files.findOne({
where: {
id,
is_delete: false,
}
});

if (!file) {
ctx.status = 404;
ctx.body = { message: 'File not found' };
return;
}

// 执行软删除,将 is_delete 字段设置为 true
await file.update({
is_delete: true,
updated_at: new Date(), // 更新更新时间
updated_by: ctx.query.updated_by || 'anonymous' // 可以通过查询参数传递更新者
});

// 返回删除成功的信息
ctx.status = 204;
} catch (error) {
ctx.status = 500;
ctx.body = { message: 'Error deleting file', error: error.message };
console.error('Delete file error:', error);
}
});

// 文件批量删除接口
router.delete('/files', async (ctx) => {
const { ids } = ctx.request.body; // 获取要删除的文件 ID 列表
const updated_by = ctx.query.updated_by || 'anonymous'; // 获取更新者,默认为匿名
console.log(ctx.request.body);
console.log(JSON.stringify(ctx.request.body));

if (!ids || !Array.isArray(ids) || ids.length === 0) {
ctx.status = 400;
ctx.body = { message: 'No file ids provided for deletion' };
return;
}

try {
// 查找并更新指定的文件
const [numberOfAffectedRows] = await Files.update(
{
is_delete: true,
updated_by: updated_by,
updated_at: new Date(),
},
{
where: {
id: {
[Op.in]: ids,
},
is_delete: false,
},
}
);

if (numberOfAffectedRows === 0) {
ctx.status = 404;
ctx.body = { message: 'No files found to delete' };
return;
}

// 返回删除成功的信息
ctx.status = 204;
} catch (error) {
ctx.status = 500;
ctx.body = { message: 'Error deleting files', error: error.message };
console.error('Delete files error:', error);
}
});

// 文件预览
router.get("/files/:id/preview", async (ctx) => {
Expand Down Expand Up @@ -426,10 +506,10 @@ router.get("/files/:id/export", async (ctx) => {
});

// 批量下载
router.get("/files/export", async (ctx) => {
const fileIds = ctx.query.ids ? ctx.query.ids.split(",") : [];
router.get("/files/export/batch", async (ctx) => {
const ids = ctx.query.ids ? ctx.query.ids.split(",") : [];

if (fileIds.length === 0) {
if (ids.length === 0) {
ctx.status = 400;
ctx.body = { message: "No file ids provided for download" };
return;
Expand All @@ -438,7 +518,7 @@ router.get("/files/export", async (ctx) => {
try {
const files = await Files.findAll({
where: {
id: { [Op.in]: fileIds },
id: { [Op.in]: ids },
is_delete: false,
[Op.or]: [
{ public_expiration: null },
Expand Down
52 changes: 26 additions & 26 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

"@hapi/bourne@^3.0.0":
version "3.0.0"
resolved "https://registry.npmmirror.com/@hapi/bourne/-/bourne-3.0.0.tgz"
resolved "https://registry.yarnpkg.com/@hapi/bourne/-/bourne-3.0.0.tgz#f11fdf7dda62fe8e336fa7c6642d9041f30356d7"
integrity sha512-Waj1cwPXJDucOib4a3bAISsKJVb15MKi9IvmTI/7ssVEm6sywXGjVJDhl6/umt1pK1ZS7PacXU3A1PmFKHEZ2w==

"@koa/cors@^5.0.0":
Expand Down Expand Up @@ -77,42 +77,42 @@

"@types/accepts@*":
version "1.3.7"
resolved "https://registry.npmmirror.com/@types/accepts/-/accepts-1.3.7.tgz"
resolved "https://registry.yarnpkg.com/@types/accepts/-/accepts-1.3.7.tgz#3b98b1889d2b2386604c2bbbe62e4fb51e95b265"
integrity sha512-Pay9fq2lM2wXPWbteBsRAGiWH2hig4ZE2asK+mm7kUzlxRTfL961rj89I6zV/E3PcIkDqyuBEcMxFT7rccugeQ==
dependencies:
"@types/node" "*"

"@types/body-parser@*":
version "1.19.5"
resolved "https://registry.npmmirror.com/@types/body-parser/-/body-parser-1.19.5.tgz"
resolved "https://registry.yarnpkg.com/@types/body-parser/-/body-parser-1.19.5.tgz#04ce9a3b677dc8bd681a17da1ab9835dc9d3ede4"
integrity sha512-fB3Zu92ucau0iQ0JMCFQE7b/dv8Ot07NI3KaZIkIUNXq82k4eBAqUaneXfleGY9JWskeS9y+u0nXMyspcuQrCg==
dependencies:
"@types/connect" "*"
"@types/node" "*"

"@types/co-body@^6.1.0":
version "6.1.3"
resolved "https://registry.npmmirror.com/@types/co-body/-/co-body-6.1.3.tgz"
resolved "https://registry.yarnpkg.com/@types/co-body/-/co-body-6.1.3.tgz#201796c6389066b400cfcb4e1ec5c3db798265a2"
integrity sha512-UhuhrQ5hclX6UJctv5m4Rfp52AfG9o9+d9/HwjxhVB5NjXxr5t9oKgJxN8xRHgr35oo8meUEHUPFWiKg6y71aA==
dependencies:
"@types/node" "*"
"@types/qs" "*"

"@types/connect@*":
version "3.4.38"
resolved "https://registry.npmmirror.com/@types/connect/-/connect-3.4.38.tgz"
resolved "https://registry.yarnpkg.com/@types/connect/-/connect-3.4.38.tgz#5ba7f3bc4fbbdeaff8dded952e5ff2cc53f8d858"
integrity sha512-K6uROf1LD88uDQqJCktA4yzL1YYAK6NgfsI0v/mTgyPKWsX1CnJ0XPSDhViejru1GcRkLWb8RlzFYJRqGUbaug==
dependencies:
"@types/node" "*"

"@types/content-disposition@*":
version "0.5.8"
resolved "https://registry.npmmirror.com/@types/content-disposition/-/content-disposition-0.5.8.tgz"
resolved "https://registry.yarnpkg.com/@types/content-disposition/-/content-disposition-0.5.8.tgz#6742a5971f490dc41e59d277eee71361fea0b537"
integrity sha512-QVSSvno3dE0MgO76pJhmv4Qyi/j0Yk9pBp0Y7TJ2Tlj+KCgJWY6qX7nnxCOLkZ3VYRSIk1WTxCvwUSdx6CCLdg==

"@types/cookies@*":
version "0.9.0"
resolved "https://registry.npmmirror.com/@types/cookies/-/cookies-0.9.0.tgz"
resolved "https://registry.yarnpkg.com/@types/cookies/-/cookies-0.9.0.tgz#a2290cfb325f75f0f28720939bee854d4142aee2"
integrity sha512-40Zk8qR147RABiQ7NQnBzWzDcjKzNrntB5BAmeGCb2p/MIyOE+4BVvc17wumsUqUw00bJYqoXFHYygQnEFh4/Q==
dependencies:
"@types/connect" "*"
Expand All @@ -128,9 +128,9 @@
"@types/ms" "*"

"@types/express-serve-static-core@^4.17.33":
version "4.19.3"
resolved "https://registry.npmmirror.com/@types/express-serve-static-core/-/express-serve-static-core-4.19.3.tgz"
integrity sha512-KOzM7MhcBFlmnlr/fzISFF5vGWVSvN6fTd4T+ExOt08bA/dA5kpSzY52nMsI1KDFmUREpJelPYyuslLRSjjgCg==
version "4.19.5"
resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.19.5.tgz#218064e321126fcf9048d1ca25dd2465da55d9c6"
integrity sha512-y6W03tvrACO72aijJ5uF02FRq5cgDR9lUxddQ8vyF+GvmjJQqbzDcJngEjURc+ZsG31VI3hODNZJ2URj86pzmg==
dependencies:
"@types/node" "*"
"@types/qs" "*"
Expand All @@ -139,7 +139,7 @@

"@types/express@*":
version "4.17.21"
resolved "https://registry.npmmirror.com/@types/express/-/express-4.17.21.tgz"
resolved "https://registry.yarnpkg.com/@types/express/-/express-4.17.21.tgz#c26d4a151e60efe0084b23dc3369ebc631ed192d"
integrity sha512-ejlPM315qwLpaQlQDTjPdsUFSc6ZsP4AN6AlWnogPjQ7CVi7PYF3YVz+CY3jE2pwYf7E/7HlDAN0rV2GxTG0HQ==
dependencies:
"@types/body-parser" "*"
Expand All @@ -149,36 +149,36 @@

"@types/formidable@^2.0.5":
version "2.0.6"
resolved "https://registry.npmmirror.com/@types/formidable/-/formidable-2.0.6.tgz"
resolved "https://registry.yarnpkg.com/@types/formidable/-/formidable-2.0.6.tgz#811ed3cd8a8a7675e02420b3f861c317e055376a"
integrity sha512-L4HcrA05IgQyNYJj6kItuIkXrInJvsXTPC5B1i64FggWKKqSL+4hgt7asiSNva75AoLQjq29oPxFfU4GAQ6Z2w==
dependencies:
"@types/node" "*"

"@types/http-assert@*":
version "1.5.5"
resolved "https://registry.npmmirror.com/@types/http-assert/-/http-assert-1.5.5.tgz"
resolved "https://registry.yarnpkg.com/@types/http-assert/-/http-assert-1.5.5.tgz#dfb1063eb7c240ee3d3fe213dac5671cfb6a8dbf"
integrity sha512-4+tE/lwdAahgZT1g30Jkdm9PzFRde0xwxBNUyRsCitRvCQB90iuA2uJYdUnhnANRcqGXaWOGY4FEoxeElNAK2g==

"@types/http-errors@*":
version "2.0.4"
resolved "https://registry.npmmirror.com/@types/http-errors/-/http-errors-2.0.4.tgz"
resolved "https://registry.yarnpkg.com/@types/http-errors/-/http-errors-2.0.4.tgz#7eb47726c391b7345a6ec35ad7f4de469cf5ba4f"
integrity sha512-D0CFMMtydbJAegzOyHjtiKPLlvnm3iTZyZRSZoLq2mRhDdmLfIWOCYPfQJ4cu2erKghU++QvjcUjp/5h7hESpA==

"@types/keygrip@*":
version "1.0.6"
resolved "https://registry.npmmirror.com/@types/keygrip/-/keygrip-1.0.6.tgz"
resolved "https://registry.yarnpkg.com/@types/keygrip/-/keygrip-1.0.6.tgz#1749535181a2a9b02ac04a797550a8787345b740"
integrity sha512-lZuNAY9xeJt7Bx4t4dx0rYCDqGPW8RXhQZK1td7d4H6E9zYbLoOtjBvfwdTKpsyxQI/2jv+armjX/RW+ZNpXOQ==

"@types/koa-compose@*":
version "3.2.8"
resolved "https://registry.npmmirror.com/@types/koa-compose/-/koa-compose-3.2.8.tgz"
resolved "https://registry.yarnpkg.com/@types/koa-compose/-/koa-compose-3.2.8.tgz#dec48de1f6b3d87f87320097686a915f1e954b57"
integrity sha512-4Olc63RY+MKvxMwVknCUDhRQX1pFQoBZ/lXcRLP69PQkEpze/0cr8LNqJQe5NFb/b19DWi2a5bTi2VAlQzhJuA==
dependencies:
"@types/koa" "*"

"@types/koa@*", "@types/koa@^2.13.5":
version "2.15.0"
resolved "https://registry.npmmirror.com/@types/koa/-/koa-2.15.0.tgz"
resolved "https://registry.yarnpkg.com/@types/koa/-/koa-2.15.0.tgz#eca43d76f527c803b491731f95df575636e7b6f2"
integrity sha512-7QFsywoE5URbuVnG3loe03QXuGajrnotr3gQkXcEBShORai23MePfFYdhz90FEtBBpkyIYQbVD+evKtloCgX3g==
dependencies:
"@types/accepts" "*"
Expand All @@ -192,7 +192,7 @@

"@types/mime@^1":
version "1.3.5"
resolved "https://registry.npmmirror.com/@types/mime/-/mime-1.3.5.tgz"
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-1.3.5.tgz#1ef302e01cf7d2b5a0fa526790c9123bf1d06690"
integrity sha512-/pyBZWSLD2n0dcHE3hq8s8ZvcETHtEuF+3E7XVt0Ig2nvsVQXdghHVcEkIWjy9A0wKfTn97a/PSDYohKIlnP/w==

"@types/ms@*":
Expand All @@ -209,25 +209,25 @@

"@types/qs@*":
version "6.9.15"
resolved "https://registry.npmmirror.com/@types/qs/-/qs-6.9.15.tgz"
resolved "https://registry.yarnpkg.com/@types/qs/-/qs-6.9.15.tgz#adde8a060ec9c305a82de1babc1056e73bd64dce"
integrity sha512-uXHQKES6DQKKCLh441Xv/dwxOq1TVS3JPUMlEqoEglvlhR6Mxnlew/Xq/LRVHpLyk7iK3zODe1qYHIMltO7XGg==

"@types/range-parser@*":
version "1.2.7"
resolved "https://registry.npmmirror.com/@types/range-parser/-/range-parser-1.2.7.tgz"
resolved "https://registry.yarnpkg.com/@types/range-parser/-/range-parser-1.2.7.tgz#50ae4353eaaddc04044279812f52c8c65857dbcb"
integrity sha512-hKormJbkJqzQGhziax5PItDUTMAM9uE2XXQmM37dyd4hVM+5aVl7oVxMVUiVQn2oCQFN/LKCZdvSM0pFRqbSmQ==

"@types/send@*":
version "0.17.4"
resolved "https://registry.npmmirror.com/@types/send/-/send-0.17.4.tgz"
resolved "https://registry.yarnpkg.com/@types/send/-/send-0.17.4.tgz#6619cd24e7270793702e4e6a4b958a9010cfc57a"
integrity sha512-x2EM6TJOybec7c52BX0ZspPodMsQUd5L6PRwOunVyVUhXiBSKf3AezDL8Dgvgt5o0UfKNfuA0eMLr2wLT4AiBA==
dependencies:
"@types/mime" "^1"
"@types/node" "*"

"@types/serve-static@*":
version "1.15.7"
resolved "https://registry.npmmirror.com/@types/serve-static/-/serve-static-1.15.7.tgz"
resolved "https://registry.yarnpkg.com/@types/serve-static/-/serve-static-1.15.7.tgz#22174bbd74fb97fe303109738e9b5c2f3064f714"
integrity sha512-W8Ym+h8nhuRwaKPaDw34QUkwsGi6Rc4yYqvKFo5rm2FUEhCFbzVWrxXUxuKK8TASjWsysJY0nsmNCGhCOIsrOw==
dependencies:
"@types/http-errors" "*"
Expand Down Expand Up @@ -462,7 +462,7 @@ cli-tableau@^2.0.0:

co-body@^6.1.0:
version "6.2.0"
resolved "https://registry.npmmirror.com/co-body/-/co-body-6.2.0.tgz"
resolved "https://registry.yarnpkg.com/co-body/-/co-body-6.2.0.tgz#afd776d60e5659f4eee862df83499698eb1aea1b"
integrity sha512-Kbpv2Yd1NdL1V/V4cwLVxraHDV6K8ayohr2rmH0J87Er8+zJjcTa6dAn9QMPC9CRgU8+aNajKbSf1TzDB1yKPA==
dependencies:
"@hapi/bourne" "^3.0.0"
Expand Down Expand Up @@ -1185,7 +1185,7 @@ keygrip@~1.1.0:

koa-body@^6.0.1:
version "6.0.1"
resolved "https://registry.npmmirror.com/koa-body/-/koa-body-6.0.1.tgz"
resolved "https://registry.yarnpkg.com/koa-body/-/koa-body-6.0.1.tgz#46c490033cceebb2874c53cfbb04c45562cf3c84"
integrity sha512-M8ZvMD8r+kPHy28aWP9VxL7kY8oPWA+C7ZgCljrCMeaU7uX6wsIQgDHskyrAr9sw+jqnIXyv4Mlxri5R4InIJg==
dependencies:
"@types/co-body" "^6.1.0"
Expand Down Expand Up @@ -1742,7 +1742,7 @@ qs@^6.11.0, qs@^6.5.2:

raw-body@^2.3.3:
version "2.5.2"
resolved "https://registry.npmmirror.com/raw-body/-/raw-body-2.5.2.tgz"
resolved "https://registry.yarnpkg.com/raw-body/-/raw-body-2.5.2.tgz#99febd83b90e08975087e8f1f9419a149366b68a"
integrity sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==
dependencies:
bytes "3.1.2"
Expand Down Expand Up @@ -2293,5 +2293,5 @@ ylru@^1.2.0:

zod@^3.19.1:
version "3.23.8"
resolved "https://registry.npmmirror.com/zod/-/zod-3.23.8.tgz"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.23.8.tgz#e37b957b5d52079769fb8097099b592f0ef4067d"
integrity sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==

0 comments on commit fe74c08

Please sign in to comment.