@kne/fastify-file-manager 2.0.0 → 2.0.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.
@@ -1,8 +1,9 @@
1
1
  const fp = require('fastify-plugin');
2
2
  const fs = require('fs-extra');
3
- const crypto = require('crypto');
4
- const path = require('path');
3
+ const crypto = require('node:crypto');
4
+ const path = require('node:path');
5
5
  const { NotFound } = require('http-errors');
6
+ const {Readable} = require('node:stream');
6
7
 
7
8
  module.exports = fp(async (fastify, options) => {
8
9
  const { models, services } = fastify.fileManager;
@@ -12,9 +13,15 @@ module.exports = fp(async (fastify, options) => {
12
13
  const hash = crypto.createHash('md5');
13
14
  const extension = path.extname(filename);
14
15
  let buffer = Buffer.alloc(0);
15
-
16
- // 使用流处理文件数据
17
- const stream = file.createReadStream();
16
+ // 处理文件流或Buffer数据
17
+ let stream;
18
+ if (file.createReadStream) {
19
+ stream = file.createReadStream();
20
+ } else if (file.file) {
21
+ stream = file.file;
22
+ } else {
23
+ throw new Error('无效的文件格式');
24
+ }
18
25
  for await (const chunk of stream) {
19
26
  hash.update(chunk);
20
27
  buffer = Buffer.concat([buffer, chunk]);
@@ -25,14 +32,14 @@ module.exports = fp(async (fastify, options) => {
25
32
  const ossServices = options.ossAdapter();
26
33
  if (typeof ossServices.uploadFile === 'function') {
27
34
  // 使用流上传到OSS
28
- const uploadStream = file.createReadStream();
35
+ const uploadStream = file.createReadStream ? file.createReadStream() : Readable.from(buffer);
29
36
  await ossServices.uploadFileStream({ stream: uploadStream, filename: `${digest}${extension}` });
30
37
  storageType = 'oss';
31
38
  } else {
32
39
  // 使用流写入本地文件
33
40
  const filepath = path.resolve(options.root, `${digest}${extension}`);
34
41
  const writeStream = fs.createWriteStream(filepath);
35
- const readStream = file.createReadStream();
42
+ const readStream = file.createReadStream ? file.createReadStream() : Readable.from(buffer);
36
43
  await new Promise((resolve, reject) => {
37
44
  readStream.pipe(writeStream)
38
45
  .on('finish', resolve)
@@ -57,17 +64,15 @@ module.exports = fp(async (fastify, options) => {
57
64
  file.storageType = storageType;
58
65
  await file.save();
59
66
  return file;
60
- })(() =>
61
- models.fileRecord.create({
62
- filename,
63
- namespace: namespace || options.namespace,
64
- encoding,
65
- mimetype,
66
- hash: digest,
67
- size: buffer.byteLength,
68
- storageType
69
- })
70
- );
67
+ })(() => models.fileRecord.create({
68
+ filename,
69
+ namespace: namespace || options.namespace,
70
+ encoding,
71
+ mimetype,
72
+ hash: digest,
73
+ size: buffer.byteLength,
74
+ storageType
75
+ }));
71
76
  return Object.assign({}, outputFile.get({ plain: true }), { id: outputFile.uuid });
72
77
  };
73
78
 
@@ -135,9 +140,7 @@ module.exports = fp(async (fastify, options) => {
135
140
  targetFile = await ossServices.downloadFile({ filename: targetFileName });
136
141
  }
137
142
  return Object.assign({}, file.get({ pain: true }), {
138
- id: file.uuid,
139
- filePath: targetFileName,
140
- targetFile
143
+ id: file.uuid, filePath: targetFileName, targetFile
141
144
  });
142
145
  };
143
146
 
@@ -166,13 +169,10 @@ module.exports = fp(async (fastify, options) => {
166
169
  }
167
170
 
168
171
  const { count, rows } = await models.fileRecord.findAndCountAll({
169
- where: queryFilter,
170
- offset: perPage * (currentPage - 1),
171
- limit: perPage
172
+ where: queryFilter, offset: perPage * (currentPage - 1), limit: perPage
172
173
  });
173
174
  return {
174
- pageData: rows.map(item => Object.assign({}, item.get({ plain: true }), { id: item.uuid })),
175
- totalCount: count
175
+ pageData: rows.map(item => Object.assign({}, item.get({ plain: true }), { id: item.uuid })), totalCount: count
176
176
  };
177
177
  };
178
178
 
@@ -198,8 +198,7 @@ module.exports = fp(async (fastify, options) => {
198
198
  };
199
199
 
200
200
  Object.assign(services, {
201
- uploadToFileSystem, uploadFromUrl, getFileUrl, getFileInfo, getFileList, deleteFiles, renameFile,
202
- // 兼容之前api,后面可能会删掉
201
+ uploadToFileSystem, uploadFromUrl, getFileUrl, getFileInfo, getFileList, deleteFiles, renameFile, // 兼容之前api,后面可能会删掉
203
202
  fileRecord: { uploadToFileSystem, uploadFromUrl, getFileUrl, getFileInfo, getFileList, deleteFiles, renameFile }
204
203
  });
205
204
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-file-manager",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "用于管理静态文件上传查看等",
5
5
  "main": "index.js",
6
6
  "scripts": {