@kne/fastify-file-manager 2.0.3 → 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.
@@ -219,8 +219,83 @@ module.exports = fp(async (fastify, options) => {
219
219
  await file.save();
220
220
  };
221
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
+
222
279
  Object.assign(services, {
223
- uploadToFileSystem, uploadFromUrl, getFileUrl, getFileInfo, getFileList, deleteFiles, renameFile, // 兼容之前api,后面可能会删掉
224
- 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
+ }
225
300
  });
226
301
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-file-manager",
3
- "version": "2.0.3",
3
+ "version": "2.0.4",
4
4
  "description": "用于管理静态文件上传查看等",
5
5
  "main": "index.js",
6
6
  "scripts": {