@kne/fastify-file-manager 3.0.1-alpha.2 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/libs/services/file-record.js +25 -11
- package/package.json +4 -2
|
@@ -13,6 +13,8 @@ module.exports = fp(async (fastify, fastifyOptions) => {
|
|
|
13
13
|
const { models, services } = fastify.fileManager;
|
|
14
14
|
const { Op } = fastify.sequelize.Sequelize;
|
|
15
15
|
|
|
16
|
+
const sanitizeFilename = filename => filename.replace(/[\\/:*?"<>|\0]/g, '_');
|
|
17
|
+
|
|
16
18
|
const detail = async ({ id, uuid, namespace }) => {
|
|
17
19
|
const file = await models.fileRecord.findOne({
|
|
18
20
|
where: { uuid: String(id || uuid).split('?')[0] }
|
|
@@ -25,7 +27,8 @@ module.exports = fp(async (fastify, fastifyOptions) => {
|
|
|
25
27
|
return file;
|
|
26
28
|
};
|
|
27
29
|
const uploadToFileSystem = async ({ id, file, namespace, options }) => {
|
|
28
|
-
const {
|
|
30
|
+
const { encoding, mimetype } = file;
|
|
31
|
+
const filename = sanitizeFilename(file.filename);
|
|
29
32
|
const hash = crypto.createHash('md5');
|
|
30
33
|
const extension = path.extname(filename);
|
|
31
34
|
const tmpPath = path.resolve(os.tmpdir(), `temp_${filename}_${crypto.randomBytes(6).toString('hex')}`);
|
|
@@ -190,7 +193,7 @@ module.exports = fp(async (fastify, fastifyOptions) => {
|
|
|
190
193
|
}
|
|
191
194
|
|
|
192
195
|
const tempFile = {
|
|
193
|
-
filename,
|
|
196
|
+
filename: sanitizeFilename(filename),
|
|
194
197
|
mimetype: response.headers.get('content-type'),
|
|
195
198
|
encoding: 'binary',
|
|
196
199
|
file: nodeStream
|
|
@@ -308,6 +311,10 @@ module.exports = fp(async (fastify, fastifyOptions) => {
|
|
|
308
311
|
|
|
309
312
|
const renameFile = async ({ id, filename }) => {
|
|
310
313
|
const file = await detail({ id });
|
|
314
|
+
filename = sanitizeFilename(filename);
|
|
315
|
+
if (!path.extname(filename) && path.extname(file.filename)) {
|
|
316
|
+
filename += path.extname(file.filename);
|
|
317
|
+
}
|
|
311
318
|
file.filename = filename;
|
|
312
319
|
await file.save();
|
|
313
320
|
};
|
|
@@ -374,15 +381,22 @@ module.exports = fp(async (fastify, fastifyOptions) => {
|
|
|
374
381
|
await fs.mkdir(tmpPath);
|
|
375
382
|
const files = [];
|
|
376
383
|
for (const file of fileList) {
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
384
|
+
try {
|
|
385
|
+
const fileStream = await getFileReadStream(file);
|
|
386
|
+
const filepath = path.resolve(tmpPath, file.filename);
|
|
387
|
+
const writeStream = fs.createWriteStream(filepath);
|
|
388
|
+
fileStream.pipe(writeStream);
|
|
389
|
+
await new Promise((resolve, reject) => {
|
|
390
|
+
writeStream.on('finish', resolve);
|
|
391
|
+
writeStream.on('error', reject);
|
|
392
|
+
});
|
|
393
|
+
files.push(filepath);
|
|
394
|
+
} catch (error) {
|
|
395
|
+
console.warn(`skip invalid file: ${file.uuid}`, error.message);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
if (files.length === 0) {
|
|
399
|
+
throw new Error('No valid files to compress');
|
|
386
400
|
}
|
|
387
401
|
const compressStream = new compressing[type].Stream();
|
|
388
402
|
files.forEach(filepath => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kne/fastify-file-manager",
|
|
3
|
-
"version": "3.0.1
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"description": "用于管理静态文件上传查看等",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
"build:md": "npx @kne/md-doc",
|
|
9
9
|
"start:md": "npx @kne/md-doc --watch",
|
|
10
10
|
"prettier": "prettier --config .prettierrc --write '{libs/**/*,index}.{js,jsx,ts,tsx,json,css,scss}'",
|
|
11
|
-
"lint-staged": "npx lint-staged"
|
|
11
|
+
"lint-staged": "npx lint-staged",
|
|
12
|
+
"test": "mocha tests/**/*.test.js --timeout 30000"
|
|
12
13
|
},
|
|
13
14
|
"lint-staged": {
|
|
14
15
|
"{libs/**/*,index}.{js,jsx,ts,tsx,json,css,scss}": [
|
|
@@ -37,6 +38,7 @@
|
|
|
37
38
|
"@kne/fastify-sequelize": "^2.0.1",
|
|
38
39
|
"fastify": "^5.3.2",
|
|
39
40
|
"husky": "^9.0.11",
|
|
41
|
+
"mocha": "^11.7.5",
|
|
40
42
|
"prettier": "^3.2.5",
|
|
41
43
|
"qs": "^6.12.3",
|
|
42
44
|
"sqlite3": "^5.1.7"
|