@kne/fastify-file-manager 2.0.2 → 2.0.4

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
  };
@@ -202,8 +219,83 @@ module.exports = fp(async (fastify, options) => {
202
219
  await file.save();
203
220
  };
204
221
 
222
+ const getFileBlob = async ({ id }) => {
223
+ const file = await models.fileRecord.findOne({
224
+ where: { uuid: id }
225
+ });
226
+ if (!file) {
227
+ throw new Error('文件不存在');
228
+ }
229
+
230
+ const extension = path.extname(file.filename);
231
+ const targetFileName = `${file.hash}${extension}`;
232
+ const ossServices = options.ossAdapter();
233
+
234
+ let buffer;
235
+ if (file.storageType === 'oss') {
236
+ if (typeof ossServices.downloadFile !== 'function') {
237
+ throw new Error('ossAdapter未正确配置无法读取oss类型存储文件');
238
+ }
239
+ buffer = await ossServices.downloadFile({ filename: targetFileName });
240
+ } else {
241
+ const filePath = path.resolve(options.root, targetFileName);
242
+ if (!(await fs.exists(filePath))) {
243
+ throw new NotFound();
244
+ }
245
+ buffer = await fs.readFile(filePath);
246
+ }
247
+
248
+ return Object.assign({}, file.get({ plain: true }), {
249
+ id: file.uuid, buffer
250
+ });
251
+ };
252
+
253
+ const getFileStream = async ({ id }) => {
254
+ const file = await models.fileRecord.findOne({
255
+ where: { uuid: id }
256
+ });
257
+ if (!file) {
258
+ throw new Error('文件不存在');
259
+ }
260
+
261
+ const extension = path.extname(file.filename);
262
+ const targetFileName = `${file.hash}${extension}`;
263
+ const ossServices = options.ossAdapter();
264
+
265
+ if (file.storageType === 'oss') {
266
+ if (typeof ossServices.getFileStream !== 'function') {
267
+ throw new Error('ossAdapter未正确配置无法读取oss类型存储文件');
268
+ }
269
+ return await ossServices.getFileStream({ filename: targetFileName });
270
+ } else {
271
+ const filePath = path.resolve(options.root, targetFileName);
272
+ if (!(await fs.exists(filePath))) {
273
+ throw new NotFound();
274
+ }
275
+ return fs.createReadStream(filePath);
276
+ }
277
+ };
278
+
205
279
  Object.assign(services, {
206
- uploadToFileSystem, uploadFromUrl, getFileUrl, getFileInfo, getFileList, deleteFiles, renameFile, // 兼容之前api,后面可能会删掉
207
- fileRecord: { uploadToFileSystem, uploadFromUrl, getFileUrl, getFileInfo, getFileList, deleteFiles, renameFile }
280
+ uploadToFileSystem,
281
+ uploadFromUrl,
282
+ getFileUrl,
283
+ getFileInfo,
284
+ getFileList,
285
+ deleteFiles,
286
+ renameFile,
287
+ getFileBlob,
288
+ getFileStream, // 兼容之前api,后面可能会删掉
289
+ fileRecord: {
290
+ uploadToFileSystem,
291
+ uploadFromUrl,
292
+ getFileUrl,
293
+ getFileInfo,
294
+ getFileList,
295
+ deleteFiles,
296
+ renameFile,
297
+ getFileBlob,
298
+ getFileStream
299
+ }
208
300
  });
209
301
  });
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.4",
4
4
  "description": "用于管理静态文件上传查看等",
5
5
  "main": "index.js",
6
6
  "scripts": {