@kne/fastify-file-manager 2.0.5 → 2.0.6

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');
@@ -68,10 +80,7 @@ module.exports = fp(async (fastify, options) => {
68
80
  if (!id) {
69
81
  return await create();
70
82
  }
71
- const file = await models.fileRecord.findOne({ where: { uuid: id } });
72
- if (!file) {
73
- throw new Error('原文件不存在');
74
- }
83
+ const file = await detail({ id });
75
84
  file.filename = filename;
76
85
  file.encoding = encoding;
77
86
  file.mimetype = mimetype;
@@ -123,12 +132,7 @@ module.exports = fp(async (fastify, options) => {
123
132
  };
124
133
 
125
134
  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
- }
135
+ const file = await detail({ id });
132
136
  const extension = path.extname(file.filename);
133
137
  const ossServices = options.ossAdapter();
134
138
  if (file.storageType === 'oss' && typeof ossServices.getFileLink !== 'function') {
@@ -145,12 +149,7 @@ module.exports = fp(async (fastify, options) => {
145
149
  };
146
150
 
147
151
  const getFileInfo = async ({ id }) => {
148
- const file = await models.fileRecord.findOne({
149
- where: { uuid: id }
150
- });
151
- if (!file) {
152
- throw new Error('文件不存在');
153
- }
152
+ const file = await detail({ id });
154
153
  const extension = path.extname(file.filename);
155
154
  const targetFileName = `${file.hash}${extension}`;
156
155
  const ossServices = options.ossAdapter();
@@ -202,27 +201,20 @@ module.exports = fp(async (fastify, options) => {
202
201
  await models.fileRecord.destroy({
203
202
  where: {
204
203
  uuid: {
205
- [Op.in]: ids
204
+ [Op.in]: ids.map((str) => str.split('?')[0])
206
205
  }
207
206
  }
208
207
  });
209
208
  };
210
209
 
211
210
  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
- }
211
+ const file = await detail({ id });
218
212
  file.filename = filename;
219
213
  await file.save();
220
214
  };
221
215
 
222
216
  const getFileBlob = async ({ id }) => {
223
- const file = await models.fileRecord.findOne({
224
- where: { uuid: id }
225
- });
217
+ const file = await detail({ id });
226
218
  if (!file) {
227
219
  throw new Error('文件不存在');
228
220
  }
@@ -251,12 +243,7 @@ module.exports = fp(async (fastify, options) => {
251
243
  };
252
244
 
253
245
  const getFileStream = async ({ id }) => {
254
- const file = await models.fileRecord.findOne({
255
- where: { uuid: id }
256
- });
257
- if (!file) {
258
- throw new Error('文件不存在');
259
- }
246
+ const file = await detail({ id });
260
247
 
261
248
  const extension = path.extname(file.filename);
262
249
  const targetFileName = `${file.hash}${extension}`;
@@ -277,9 +264,7 @@ module.exports = fp(async (fastify, options) => {
277
264
  };
278
265
 
279
266
  const getFileInstance = async ({ id, uuid }) => {
280
- return await models.fileRecord.findOne({
281
- where: { uuid: uuid || id }
282
- });
267
+ return detail({ id, uuid });
283
268
  };
284
269
 
285
270
  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.6",
4
4
  "description": "用于管理静态文件上传查看等",
5
5
  "main": "index.js",
6
6
  "scripts": {