Refactor code structure for improved readability and maintainability

This commit is contained in:
titor-z 2025-03-25 14:08:59 +00:00
commit ea98b368e9
8 changed files with 1262 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.ini
.DS_Store
node_modules/

7
index.ts Normal file
View File

@ -0,0 +1,7 @@
import { app } from "./src/app";
// app.listen(4312, ()=> console.log(`The Server is running.`))
// 启动服务器
const port = 8000;
app.listen(port, () => console.log('running...'));

26
package.json Normal file
View File

@ -0,0 +1,26 @@
{
"name": "file-serve",
"version": "0.0.1",
"description": "文件上传服务器",
"main": "index.js",
"author": "Titor<titor@h2ostudio.cn>",
"license": "MIT",
"scripts": {
"dev": "yarn tsx index.ts"
},
"devDependencies": {
"@types/ejs": "^3.1.5",
"@types/koa": "^2.15.0",
"@types/koa-router": "^7.4.8",
"@types/koa-static": "^4.0.4",
"@types/node": "^22.13.1",
"tsx": "^4.19.2"
},
"dependencies": {
"ejs": "^3.1.10",
"koa": "^2.15.3",
"koa-body": "^6.0.1",
"koa-router": "^13.0.1",
"koa-static": "^5.0.0"
}
}

77
src/app.ts Normal file
View File

@ -0,0 +1,77 @@
import Koa from 'koa';
import Router from 'koa-router';
import koaBody from 'koa-body';
import path from 'path';
import fs from 'fs';
import serve from 'koa-static';
import { File } from 'formidable';
import { render} from "ejs"
import { readdir, readFile } from 'fs/promises';
export const app = new Koa();
const router = new Router();
const uploadDir = '/mnt/d/cloud/'
// 设置上传文件存储目录
// const uploadDir = path.join(__dirname, '../uploads');
// 确保上传目录存在
// if (!fs.existsSync(uploadDir)) {
// fs.mkdirSync(uploadDir);
// }
// 使用 koa-body 中间件处理文件上传
app.use(
koaBody({
multipart: true,
formidable: {
uploadDir: uploadDir, // 上传文件存储目录
keepExtensions: true, // 保留文件扩展名
maxFileSize: 2048 * 1024 * 1024, // 设置最大文件大小为600MB
filename(name, ext, part, form) {
return part.originalFilename || `${name}${ext}`
}
},
})
)
// 静态文件服务,用于访问上传的文件
app.use(serve(uploadDir));
// 首页路由,显示文件列表和上传按钮
router.get('/', async (ctx) => {
const files = await readdir(uploadDir);
let file = await readFile(path.join(__dirname, 'template/index.ejs'));
ctx.body = render(file.toString(), {files})
});
// 文件上传路由
router.post('/upload', async (ctx) => {
// 检查是否有文件上传
if (!ctx.request.files || !ctx.request.files.file) {
ctx.status = 400;
ctx.body = '未上传文件';
return;
}
// 获取上传的文件对象
const file = ctx.request.files.file;
// 处理单个文件或多个文件
let fileName: string;
if (Array.isArray(file)) {
// 多个文件
fileName = file.map((f: File) => f.originalFilename || f.newFilename).join(', ');
} else {
// 单个文件
fileName = file.originalFilename || file.newFilename;
}
// 返回文件名
ctx.body = `文件上传成功,文件名:${fileName}`;
ctx.redirect('/');
});
// 使用路由中间件
app.use(router.routes()).use(router.allowedMethods());

27
src/template/index.ejs Normal file
View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件上传</title>
</head>
<body>
<h1>文件上传</h1>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<button type="submit">上传</button>
</form>
<h2>已上传文件列表:</h2>
<ul>
<% files.map((file)=> { %>
<li>
<a href="<%=file%>"><%=file%></a>
</li>
<% }).join('') %>
<!-- ${files.map((file) => `<li><a href="/${file}">${file}</a></li>`).join('')} -->
</ul>
</body>
</html>

11
src/types/koa.d.ts vendored Normal file
View File

@ -0,0 +1,11 @@
import 'koa';
import { Files } from 'formidable';
declare module 'koa' {
interface Request {
files?: Files; // 使用 formidable.Files 类型
}
interface ctx {
render?: Function
}
}

13
tsconfig.json Normal file
View File

@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*"]
}

1097
yarn.lock Normal file

File diff suppressed because it is too large Load Diff