@kne/fastify-file-manager 3.0.1-alpha.2 → 3.0.1-alpha.3

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.
@@ -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 { filename, encoding, mimetype } = file;
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
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-file-manager",
3
- "version": "3.0.1-alpha.2",
3
+ "version": "3.0.1-alpha.3",
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"