@kne/fastify-file-manager 2.0.3 → 2.0.5
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.
- package/libs/services/file-record.js +85 -2
- package/package.json +1 -1
|
@@ -219,8 +219,91 @@ 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
|
+
|
|
279
|
+
const getFileInstance = async ({ id, uuid }) => {
|
|
280
|
+
return await models.fileRecord.findOne({
|
|
281
|
+
where: { uuid: uuid || id }
|
|
282
|
+
});
|
|
283
|
+
};
|
|
284
|
+
|
|
222
285
|
Object.assign(services, {
|
|
223
|
-
uploadToFileSystem,
|
|
224
|
-
|
|
286
|
+
uploadToFileSystem,
|
|
287
|
+
uploadFromUrl,
|
|
288
|
+
getFileUrl,
|
|
289
|
+
getFileInfo,
|
|
290
|
+
getFileList,
|
|
291
|
+
deleteFiles,
|
|
292
|
+
renameFile,
|
|
293
|
+
getFileBlob,
|
|
294
|
+
getFileStream, // 兼容之前api,后面可能会删掉
|
|
295
|
+
getFileInstance,
|
|
296
|
+
fileRecord: {
|
|
297
|
+
uploadToFileSystem,
|
|
298
|
+
uploadFromUrl,
|
|
299
|
+
getFileUrl,
|
|
300
|
+
getFileInfo,
|
|
301
|
+
getFileList,
|
|
302
|
+
deleteFiles,
|
|
303
|
+
renameFile,
|
|
304
|
+
getFileBlob,
|
|
305
|
+
getFileStream,
|
|
306
|
+
getFileInstance
|
|
307
|
+
}
|
|
225
308
|
});
|
|
226
309
|
});
|