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