Skip to content

Commit 4d5baca

Browse files
committed
fix: Fix the issue of incorrect thumbnail after uploading 🐛
1 parent 1b50ae2 commit 4d5baca

4 files changed

Lines changed: 37 additions & 29 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ jspm_packages/
4646

4747
#
4848
public/files/*
49+
provisional
4950
provisional/*
5051
privacy/*
52+
resource
53+
resource/*
5154

5255
# Snowpack dependency directory (https://snowpack.dev/)
5356
web_modules/

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@ nodejs 实现文件上传功能
1717

1818

1919
### 环境变量
20-
可以创建一个 `.env.local` 文件,在里面配置对应的环境变量
20+
创建一个 `.env.local` 文件,在里面配置对应的环境变量
2121

22-
- TINIFY_KEY=
23-
- INTERNAL_NETWORK_DOMAIN=
24-
- PUBLIC_NETWORK_DOMAIN=
25-
- SERVER_PORT=
26-
- MYSQL_DATABASE=
27-
- MYSQL_HOST=
28-
- MYSQL_USER=
29-
- MYSQL_PASSWORD=
22+
TINIFY_KEY=
23+
INTERNAL_NETWORK_DOMAIN=http://localhost:3000
24+
PUBLIC_NETWORK_DOMAIN=http://localhost:3000
25+
SERVER_PORT=3000
26+
DIALECT=mysql
27+
MYSQL_DATABASE=
28+
MYSQL_HOST=
29+
MYSQL_USER=root
30+
MYSQL_PASSWORD=
31+
MYSQL_PORT=3306
3032

3133
### 创建表的字段说明
3234

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ app.use(require('koa-static')(path.join(__dirname, 'public')));
1515
const createDirectories = () => {
1616
const dirs = [
1717
path.join(__dirname, 'provisional'),
18-
path.join(__dirname, 'public', 'files')
18+
path.join(__dirname, 'public', 'resource')
1919
];
2020
dirs.forEach((dir) => {
2121
if (!fs.existsSync(dir)) {

routers/files.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ const { imageMimeTypes, tinifySupportedMimeTypes } = require('../constants/file'
1313
tinify.key = process.env.TINIFY_KEY;
1414

1515
const router = new Router();
16-
const uploadDirectory = path.join(__dirname, '..', 'public', 'files');
16+
const uploadDirectory = path.join(__dirname, '..', 'resource'); // 修改后的上传目录
1717
const iconsDirectory = path.join(__dirname, '..', 'public', 'icons');
1818

19-
// 获取缩略图路径
20-
const getFileThumbPath = (fileId) => path.join(uploadDirectory, `${fileId}_thumb`);
19+
// 获取实际文件路径
20+
const getRealFilePath = (fileId, ext) => path.join(uploadDirectory, `${fileId}${ext}`);
21+
22+
// 获取实际缩略图路径
23+
const getRealThumbPath = (fileId) => path.join(uploadDirectory, `${fileId}_thumb.png`);
2124

2225
// 获取默认缩略图路径
2326
const getDefaultThumbPath = (mime) => {
@@ -50,52 +53,52 @@ router.post('/upload', async (ctx) => {
5053
for (const file of fileList) {
5154
const fileId = uuidv4();
5255
const ext = path.extname(file.filepath);
53-
const outputFilePath = path.join(uploadDirectory, `${fileId}${ext}`);
56+
const realFilePath = getRealFilePath(fileId, ext);
5457

5558
const { mime, ext: fileExt } = await detectFileType(file.filepath, file);
56-
let outputFileThumbPath = null;
59+
let realThumbPath = null;
5760

5861
if (shouldGenerateThumb && imageMimeTypes.includes(mime)) {
59-
outputFileThumbPath = getFileThumbPath(fileId);
62+
realThumbPath = getRealThumbPath(fileId);
6063
await sharp(file.filepath)
6164
.resize(200, 200)
62-
.toFile(outputFileThumbPath);
65+
.toFile(realThumbPath);
6366
} else if (shouldGenerateThumb) {
64-
outputFileThumbPath = getDefaultThumbPath(mime);
67+
realThumbPath = getDefaultThumbPath(mime);
6568
}
6669

6770
if (shouldCompress && tinifySupportedMimeTypes.includes(mime)) {
68-
await tinify.fromFile(file.filepath).toFile(outputFilePath);
71+
await tinify.fromFile(file.filepath).toFile(realFilePath);
6972
} else {
7073
if (shouldKeepTemp) {
71-
await fsp.copyFile(file.filepath, outputFilePath);
74+
await fsp.copyFile(file.filepath, realFilePath);
7275
} else {
73-
await fsp.rename(file.filepath, outputFilePath);
76+
await fsp.rename(file.filepath, realFilePath);
7477
}
7578
}
7679

77-
const fileUrl = `${process.env.PUBLIC_NETWORK_DOMAIN}/files/${fileId}${ext}`;
78-
const thumbUrl = shouldGenerateThumb ? `${process.env.PUBLIC_NETWORK_DOMAIN}/files/${fileId}?type=thumb` : null;
80+
const fileUrl = `${process.env.PUBLIC_NETWORK_DOMAIN}/files/${fileId}`;
81+
const thumbUrl = shouldGenerateThumb ? `${fileUrl}?type=thumb` : null;
7982

8083
await File.create({
8184
id: fileId,
82-
filename: path.basename(outputFilePath),
83-
filesize: (await fsp.stat(outputFilePath)).size,
85+
filename: path.basename(realFilePath),
86+
filesize: (await fsp.stat(realFilePath)).size,
8487
filelocation: fileUrl,
85-
real_file_location: outputFilePath,
88+
real_file_location: realFilePath,
8689
created_by: ctx.query.createdBy || 'anonymous',
8790
is_public: isFilePublic,
8891
thumb_location: thumbUrl,
8992
is_thumb: shouldGenerateThumb,
9093
is_delete: false,
91-
real_file_thumb_location: outputFileThumbPath,
94+
real_file_thumb_location: realThumbPath,
9295
mime,
9396
ext: fileExt
9497
});
9598

9699
const response = { filepath: fileUrl };
97100
if (responseType === 'md' && imageMimeTypes.includes(mime)) {
98-
response.filepath = `![${path.basename(outputFilePath)}](${fileUrl})`;
101+
response.filepath = `![${path.basename(realFilePath)}](${fileUrl})`;
99102
}
100103
responses.push(response);
101104

@@ -177,7 +180,7 @@ router.get('/files', async (ctx) => {
177180
}
178181
});
179182

180-
// 获取单个文件
183+
// 获取单个文件信息
181184
router.get('/files/:id', async (ctx) => {
182185
const { id } = ctx.params;
183186
const { type } = ctx.query; // 获取查询参数 'type',可以是 'thumb' 或 'original'

0 commit comments

Comments
 (0)