@kne/fastify-file-manager 2.0.5 → 2.0.7

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,11 +4,23 @@ 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
+ const { Readable } = require('node:stream');
8
8
 
9
9
  module.exports = fp(async (fastify, options) => {
10
10
  const { models, services } = fastify.fileManager;
11
11
  const { Op } = fastify.sequelize.Sequelize;
12
+
13
+ const detail = async ({ id, uuid }) => {
14
+ const file = await models.fileRecord.findOne({
15
+ where: { uuid: String(id || uuid).split('?')[0] }
16
+ });
17
+
18
+ if (!file) {
19
+ throw new Error('文件不存在');
20
+ }
21
+
22
+ return file;
23
+ };
12
24
  const uploadToFileSystem = async ({ id, file, namespace }) => {
13
25
  const { filename, encoding, mimetype } = file;
14
26
  const hash = crypto.createHash('md5');
@@ -39,6 +51,13 @@ module.exports = fp(async (fastify, options) => {
39
51
  throw new Error('文件类型不支持');
40
52
  }
41
53
 
54
+ await new Promise((resolve, reject) => {
55
+ writeStream.on('finish', () => {
56
+ resolve();
57
+ });
58
+ writeStream.on('error', reject);
59
+ });
60
+
42
61
  const digest = hash.digest('hex');
43
62
 
44
63
  let storageType;
@@ -68,10 +87,7 @@ module.exports = fp(async (fastify, options) => {
68
87
  if (!id) {
69
88
  return await create();
70
89
  }
71
- const file = await models.fileRecord.findOne({ where: { uuid: id } });
72
- if (!file) {
73
- throw new Error('原文件不存在');
74
- }
90
+ const file = await detail({ id });
75
91
  file.filename = filename;
76
92
  file.encoding = encoding;
77
93
  file.mimetype = mimetype;
@@ -123,12 +139,7 @@ module.exports = fp(async (fastify, options) => {
123
139
  };
124
140
 
125
141
  const getFileUrl = async ({ id, namespace }) => {
126
- const file = await models.fileRecord.findOne({
127
- where: { uuid: id }
128
- });
129
- if (!file) {
130
- throw new Error('文件不存在');
131
- }
142
+ const file = await detail({ id });
132
143
  const extension = path.extname(file.filename);
133
144
  const ossServices = options.ossAdapter();
134
145
  if (file.storageType === 'oss' && typeof ossServices.getFileLink !== 'function') {
@@ -145,12 +156,7 @@ module.exports = fp(async (fastify, options) => {
145
156
  };
146
157
 
147
158
  const getFileInfo = async ({ id }) => {
148
- const file = await models.fileRecord.findOne({
149
- where: { uuid: id }
150
- });
151
- if (!file) {
152
- throw new Error('文件不存在');
153
- }
159
+ const file = await detail({ id });
154
160
  const extension = path.extname(file.filename);
155
161
  const targetFileName = `${file.hash}${extension}`;
156
162
  const ossServices = options.ossAdapter();
@@ -202,27 +208,20 @@ module.exports = fp(async (fastify, options) => {
202
208
  await models.fileRecord.destroy({
203
209
  where: {
204
210
  uuid: {
205
- [Op.in]: ids
211
+ [Op.in]: ids.map((str) => str.split('?')[0])
206
212
  }
207
213
  }
208
214
  });
209
215
  };
210
216
 
211
217
  const renameFile = async ({ id, filename }) => {
212
- const file = await models.fileRecord.findOne({
213
- where: { uuid: id }
214
- });
215
- if (!file) {
216
- throw new Error('文件不存在');
217
- }
218
+ const file = await detail({ id });
218
219
  file.filename = filename;
219
220
  await file.save();
220
221
  };
221
222
 
222
223
  const getFileBlob = async ({ id }) => {
223
- const file = await models.fileRecord.findOne({
224
- where: { uuid: id }
225
- });
224
+ const file = await detail({ id });
226
225
  if (!file) {
227
226
  throw new Error('文件不存在');
228
227
  }
@@ -251,12 +250,7 @@ module.exports = fp(async (fastify, options) => {
251
250
  };
252
251
 
253
252
  const getFileStream = async ({ id }) => {
254
- const file = await models.fileRecord.findOne({
255
- where: { uuid: id }
256
- });
257
- if (!file) {
258
- throw new Error('文件不存在');
259
- }
253
+ const file = await detail({ id });
260
254
 
261
255
  const extension = path.extname(file.filename);
262
256
  const targetFileName = `${file.hash}${extension}`;
@@ -277,9 +271,7 @@ module.exports = fp(async (fastify, options) => {
277
271
  };
278
272
 
279
273
  const getFileInstance = async ({ id, uuid }) => {
280
- return await models.fileRecord.findOne({
281
- where: { uuid: uuid || id }
282
- });
274
+ return detail({ id, uuid });
283
275
  };
284
276
 
285
277
  Object.assign(services, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-file-manager",
3
- "version": "2.0.5",
3
+ "version": "2.0.7",
4
4
  "description": "用于管理静态文件上传查看等",
5
5
  "main": "index.js",
6
6
  "scripts": {