@kne/fastify-file-manager 2.0.1 → 2.0.2

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.
@@ -3,7 +3,7 @@ const fs = require('fs-extra');
3
3
  const crypto = require('node:crypto');
4
4
  const path = require('node:path');
5
5
  const { NotFound } = require('http-errors');
6
- const {Readable} = require('node:stream');
6
+ const os = require('node:os');
7
7
 
8
8
  module.exports = fp(async (fastify, options) => {
9
9
  const { models, services } = fastify.fileManager;
@@ -12,34 +12,46 @@ module.exports = fp(async (fastify, options) => {
12
12
  const { filename, encoding, mimetype } = file;
13
13
  const hash = crypto.createHash('md5');
14
14
  const extension = path.extname(filename);
15
- let buffer = Buffer.alloc(0);
16
- // 处理文件流或Buffer数据
17
- let stream;
18
- if (file.createReadStream) {
19
- stream = file.createReadStream();
20
- } else if (file.file) {
21
- stream = file.file;
15
+ const tmpPath = path.resolve(os.tmpdir(), `temp_${filename}_${crypto.randomBytes(6).toString('hex')}`);
16
+ const writeStream = fs.createWriteStream(tmpPath);
17
+ let fileSize = 0;
18
+ if (file.file) {
19
+ file.file.on('data', (chunk) => {
20
+ hash.update(chunk); // 更新哈希
21
+ writeStream.write(chunk); // 写入文件
22
+ fileSize += chunk.length; // 更新文件大小
23
+ });
24
+
25
+ await new Promise((resolve, reject) => {
26
+ file.file.on('end', () => {
27
+ writeStream.end(); // 关闭写入流
28
+ resolve();
29
+ });
30
+ file.file.on('error', reject);
31
+ });
32
+ } else if (file.toBuffer) {
33
+ const buffer = await file.toBuffer();
34
+ hash.update(buffer);
35
+ writeStream.write(buffer);
36
+ fileSize = buffer.byteLength;
22
37
  } else {
23
- throw new Error('无效的文件格式');
24
- }
25
- for await (const chunk of stream) {
26
- hash.update(chunk);
27
- buffer = Buffer.concat([buffer, chunk]);
38
+ throw new Error('文件类型不支持');
28
39
  }
40
+
29
41
  const digest = hash.digest('hex');
30
42
 
31
43
  let storageType;
32
44
  const ossServices = options.ossAdapter();
33
45
  if (typeof ossServices.uploadFile === 'function') {
34
46
  // 使用流上传到OSS
35
- const uploadStream = file.createReadStream ? file.createReadStream() : Readable.from(buffer);
36
- await ossServices.uploadFileStream({ stream: uploadStream, filename: `${digest}${extension}` });
47
+ const readStream = fs.createReadStream(tmpPath);
48
+ await ossServices.uploadFileStream({ stream: readStream, filename: `${digest}${extension}` });
37
49
  storageType = 'oss';
38
50
  } else {
39
51
  // 使用流写入本地文件
40
52
  const filepath = path.resolve(options.root, `${digest}${extension}`);
41
53
  const writeStream = fs.createWriteStream(filepath);
42
- const readStream = file.createReadStream ? file.createReadStream() : Readable.from(buffer);
54
+ const readStream = fs.createReadStream(tmpPath);
43
55
  await new Promise((resolve, reject) => {
44
56
  readStream.pipe(writeStream)
45
57
  .on('finish', resolve)
@@ -48,6 +60,9 @@ module.exports = fp(async (fastify, options) => {
48
60
  storageType = 'local';
49
61
  }
50
62
 
63
+ //清楚临时文件
64
+ await fs.remove(tmpPath);
65
+
51
66
  const outputFile = await (async create => {
52
67
  if (!id) {
53
68
  return await create();
@@ -60,7 +75,7 @@ module.exports = fp(async (fastify, options) => {
60
75
  file.encoding = encoding;
61
76
  file.mimetype = mimetype;
62
77
  file.hash = digest;
63
- file.size = buffer.byteLength;
78
+ file.size = fileSize;
64
79
  file.storageType = storageType;
65
80
  await file.save();
66
81
  return file;
@@ -70,7 +85,7 @@ module.exports = fp(async (fastify, options) => {
70
85
  encoding,
71
86
  mimetype,
72
87
  hash: digest,
73
- size: buffer.byteLength,
88
+ size: fileSize,
74
89
  storageType
75
90
  }));
76
91
  return Object.assign({}, outputFile.get({ plain: true }), { id: outputFile.uuid });
@@ -81,21 +96,11 @@ module.exports = fp(async (fastify, options) => {
81
96
  if (!response.ok) {
82
97
  throw new Error('下载文件失败');
83
98
  }
84
- const chunks = [];
85
- for await (const chunk of response.body) {
86
- chunks.push(chunk);
87
- }
88
- const buffer = Buffer.concat(chunks);
89
99
  const tempFile = {
90
100
  filename: path.basename(url).split('?')[0],
91
101
  mimetype: response.headers.get('content-type'),
92
102
  encoding: 'binary',
93
- createReadStream: () => {
94
- const readable = new require('stream').Readable();
95
- readable.push(buffer);
96
- readable.push(null);
97
- return readable;
98
- }
103
+ file: response.body
99
104
  };
100
105
  return await uploadToFileSystem({ id, file: tempFile, namespace });
101
106
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-file-manager",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "用于管理静态文件上传查看等",
5
5
  "main": "index.js",
6
6
  "scripts": {