@kne/fastify-file-manager 2.0.2 → 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.
@@ -4,6 +4,7 @@ const crypto = require('node:crypto');
4
4
  const path = require('node:path');
5
5
  const { NotFound } = require('http-errors');
6
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;
@@ -80,13 +81,7 @@ module.exports = fp(async (fastify, options) => {
80
81
  await file.save();
81
82
  return file;
82
83
  })(() => models.fileRecord.create({
83
- filename,
84
- namespace: namespace || options.namespace,
85
- encoding,
86
- mimetype,
87
- hash: digest,
88
- size: fileSize,
89
- storageType
84
+ filename, namespace: namespace || options.namespace, encoding, mimetype, hash: digest, size: fileSize, storageType
90
85
  }));
91
86
  return Object.assign({}, outputFile.get({ plain: true }), { id: outputFile.uuid });
92
87
  };
@@ -96,11 +91,33 @@ module.exports = fp(async (fastify, options) => {
96
91
  if (!response.ok) {
97
92
  throw new Error('下载文件失败');
98
93
  }
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();
99
116
  const tempFile = {
100
117
  filename: path.basename(url).split('?')[0],
101
118
  mimetype: response.headers.get('content-type'),
102
119
  encoding: 'binary',
103
- file: response.body
120
+ file: nodeStream
104
121
  };
105
122
  return await uploadToFileSystem({ id, file: tempFile, namespace });
106
123
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-file-manager",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "description": "用于管理静态文件上传查看等",
5
5
  "main": "index.js",
6
6
  "scripts": {