@kne/fastify-file-manager 3.0.0-alpha.0 → 3.0.0-alpha.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.
@@ -7,8 +7,9 @@ const os = require('node:os');
7
7
  const { Readable } = require('node:stream');
8
8
  const compressing = require('compressing');
9
9
  const { glob } = require('glob');
10
+ const MimeTypes = require('mime-types');
10
11
 
11
- module.exports = fp(async (fastify, options) => {
12
+ module.exports = fp(async (fastify, fastifyOptions) => {
12
13
  const { models, services } = fastify.fileManager;
13
14
  const { Op } = fastify.sequelize.Sequelize;
14
15
 
@@ -64,7 +65,7 @@ module.exports = fp(async (fastify, options) => {
64
65
  const digest = hash.digest('hex');
65
66
 
66
67
  let storageType;
67
- const ossServices = options.ossAdapter();
68
+ const ossServices = fastifyOptions.ossAdapter();
68
69
  if (typeof ossServices.uploadFile === 'function') {
69
70
  // 使用流上传到OSS
70
71
  const readStream = fs.createReadStream(tmpPath);
@@ -72,7 +73,7 @@ module.exports = fp(async (fastify, options) => {
72
73
  storageType = 'oss';
73
74
  } else {
74
75
  // 使用流写入本地文件
75
- const filepath = path.resolve(options.root, `${digest}${extension}`);
76
+ const filepath = path.resolve(fastifyOptions.root, `${digest}${extension}`);
76
77
  const writeStream = fs.createWriteStream(filepath);
77
78
  const readStream = fs.createReadStream(tmpPath);
78
79
  await new Promise((resolve, reject) => {
@@ -101,7 +102,7 @@ module.exports = fp(async (fastify, options) => {
101
102
  })(() =>
102
103
  models.fileRecord.create({
103
104
  filename,
104
- namespace: namespace || options.namespace,
105
+ namespace: namespace || fastifyOptions.namespace,
105
106
  encoding,
106
107
  mimetype,
107
108
  hash: digest,
@@ -175,7 +176,7 @@ module.exports = fp(async (fastify, options) => {
175
176
  const getFileUrl = async ({ id, namespace }) => {
176
177
  const file = await detail({ id, namespace });
177
178
  const extension = path.extname(file.filename);
178
- const ossServices = options.ossAdapter();
179
+ const ossServices = fastifyOptions.ossAdapter();
179
180
  if (file.storageType === 'oss' && typeof ossServices.getFileLink !== 'function') {
180
181
  throw new Error('ossAdapter未正确配置无法读取oss类型存储文件');
181
182
  }
@@ -183,17 +184,17 @@ module.exports = fp(async (fastify, options) => {
183
184
  return await ossServices.getFileLink({ filename: `${file.hash}${extension}` });
184
185
  }
185
186
 
186
- if (!(await fs.exists(`${options.root}/${file.hash}${extension}`))) {
187
+ if (!(await fs.exists(`${fastifyOptions.root}/${file.hash}${extension}`))) {
187
188
  throw new NotFound();
188
189
  }
189
- return `${options.prefix}/file/${file.hash}${extension}?filename=${file.filename}`;
190
+ return `${fastifyOptions.prefix}/file/${file.hash}${extension}?filename=${file.filename}`;
190
191
  };
191
192
 
192
193
  const getFileInfo = async ({ id, namespace }) => {
193
194
  const file = await detail({ id, namespace });
194
195
  const extension = path.extname(file.filename);
195
196
  const targetFileName = `${file.hash}${extension}`;
196
- const ossServices = options.ossAdapter();
197
+ const ossServices = fastifyOptions.ossAdapter();
197
198
  if (file.storageType === 'oss' && typeof ossServices.downloadFile !== 'function') {
198
199
  throw new Error('ossAdapter未正确配置无法读取oss类型存储文件');
199
200
  }
@@ -294,7 +295,7 @@ module.exports = fp(async (fastify, options) => {
294
295
 
295
296
  const extension = path.extname(file.filename);
296
297
  const targetFileName = `${file.hash}${extension}`;
297
- const ossServices = options.ossAdapter();
298
+ const ossServices = fastifyOptions.ossAdapter();
298
299
 
299
300
  let buffer;
300
301
  if (file.storageType === 'oss') {
@@ -303,7 +304,7 @@ module.exports = fp(async (fastify, options) => {
303
304
  }
304
305
  buffer = await ossServices.downloadFile({ filename: targetFileName });
305
306
  } else {
306
- const filePath = path.resolve(options.root, targetFileName);
307
+ const filePath = path.resolve(fastifyOptions.root, targetFileName);
307
308
  if (!(await fs.exists(filePath))) {
308
309
  throw new NotFound();
309
310
  }
@@ -319,14 +320,14 @@ module.exports = fp(async (fastify, options) => {
319
320
  const getFileReadStream = file => {
320
321
  const extension = path.extname(file.filename);
321
322
  const targetFileName = `${file.hash}${extension}`;
322
- const ossServices = options.ossAdapter();
323
+ const ossServices = fastifyOptions.ossAdapter();
323
324
  if (file.storageType === 'oss') {
324
325
  if (typeof ossServices.getFileStream !== 'function') {
325
326
  throw new Error('ossAdapter未正确配置无法读取oss类型存储文件');
326
327
  }
327
328
  return ossServices.getFileStream({ filename: targetFileName });
328
329
  } else {
329
- const filePath = path.resolve(options.root, targetFileName);
330
+ const filePath = path.resolve(fastifyOptions.root, targetFileName);
330
331
  return fs.createReadStream(filePath);
331
332
  }
332
333
  };
@@ -388,7 +389,8 @@ module.exports = fp(async (fastify, options) => {
388
389
  const tmpPath = path.resolve(os.tmpdir(), `temp_${id}_${crypto.randomBytes(6).toString('hex')}`);
389
390
  await compressing[type].uncompress(fileStream, tmpPath);
390
391
  const files = await glob(globOptions, {
391
- cwd: tmpPath
392
+ cwd: tmpPath,
393
+ nodir: true
392
394
  });
393
395
  //将文件上传到文件系统
394
396
  const fileList = await Promise.all(
@@ -396,8 +398,15 @@ module.exports = fp(async (fastify, options) => {
396
398
  const filepath = path.resolve(tmpPath, dir);
397
399
  const filename = path.basename(dir);
398
400
  const fileStream = fs.createReadStream(filepath);
401
+
402
+ const mimetype = MimeTypes.lookup(filepath) || 'application/octet-stream';
399
403
  const file = await uploadToFileSystem({
400
- file: fileStream,
404
+ file: {
405
+ filename,
406
+ mimetype,
407
+ encoding: 'binary',
408
+ file: fileStream
409
+ },
401
410
  filename,
402
411
  namespace
403
412
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-file-manager",
3
- "version": "3.0.0-alpha.0",
3
+ "version": "3.0.0-alpha.1",
4
4
  "description": "用于管理静态文件上传查看等",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -52,6 +52,7 @@
52
52
  "compressing": "^2.0.0",
53
53
  "fs-extra": "^11.2.0",
54
54
  "glob": "^13.0.0",
55
- "http-errors": "^2.0.0"
55
+ "http-errors": "^2.0.0",
56
+ "mime-types": "^3.0.2"
56
57
  }
57
58
  }