@kne/fastify-file-manager 3.0.1-alpha.1 → 3.0.1-alpha.2
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 +45 -20
- package/package.json +1 -1
|
@@ -32,7 +32,6 @@ module.exports = fp(async (fastify, fastifyOptions) => {
|
|
|
32
32
|
let fileSize = 0;
|
|
33
33
|
if (file.file) {
|
|
34
34
|
const writeStream = fs.createWriteStream(tmpPath);
|
|
35
|
-
|
|
36
35
|
await new Promise((resolve, reject) => {
|
|
37
36
|
const cleanup = () => {
|
|
38
37
|
file.file.removeAllListeners();
|
|
@@ -74,8 +73,15 @@ module.exports = fp(async (fastify, fastifyOptions) => {
|
|
|
74
73
|
await fs.writeFile(tmpPath, buffer);
|
|
75
74
|
fileSize = buffer.byteLength;
|
|
76
75
|
} else if (file.filepath) {
|
|
76
|
+
const readStream = fs.createReadStream(file.filepath);
|
|
77
|
+
await new Promise((resolve, reject) => {
|
|
78
|
+
readStream.on('data', chunk => hash.update(chunk));
|
|
79
|
+
readStream.on('end', resolve);
|
|
80
|
+
readStream.on('error', reject);
|
|
81
|
+
});
|
|
77
82
|
await fs.copy(file.filepath, tmpPath);
|
|
78
83
|
const stat = await fs.stat(tmpPath);
|
|
84
|
+
|
|
79
85
|
fileSize = stat.size;
|
|
80
86
|
} else {
|
|
81
87
|
throw new Error('文件类型不支持');
|
|
@@ -118,16 +124,18 @@ module.exports = fp(async (fastify, fastifyOptions) => {
|
|
|
118
124
|
file.options = options;
|
|
119
125
|
await file.save();
|
|
120
126
|
return file;
|
|
121
|
-
})(() =>
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
127
|
+
})(() =>
|
|
128
|
+
models.fileRecord.create({
|
|
129
|
+
filename,
|
|
130
|
+
namespace: namespace || fastifyOptions.namespace,
|
|
131
|
+
encoding,
|
|
132
|
+
mimetype,
|
|
133
|
+
hash: digest,
|
|
134
|
+
size: fileSize,
|
|
135
|
+
storageType,
|
|
136
|
+
options
|
|
137
|
+
})
|
|
138
|
+
);
|
|
131
139
|
return Object.assign({}, outputFile.get({ plain: true }), { id: outputFile.uuid });
|
|
132
140
|
};
|
|
133
141
|
|
|
@@ -182,7 +190,10 @@ module.exports = fp(async (fastify, fastifyOptions) => {
|
|
|
182
190
|
}
|
|
183
191
|
|
|
184
192
|
const tempFile = {
|
|
185
|
-
filename,
|
|
193
|
+
filename,
|
|
194
|
+
mimetype: response.headers.get('content-type'),
|
|
195
|
+
encoding: 'binary',
|
|
196
|
+
file: nodeStream
|
|
186
197
|
};
|
|
187
198
|
return await uploadToFileSystem({ id, file: tempFile, namespace, options });
|
|
188
199
|
};
|
|
@@ -217,7 +228,9 @@ module.exports = fp(async (fastify, fastifyOptions) => {
|
|
|
217
228
|
targetFile = await ossServices.downloadFile({ filename: targetFileName });
|
|
218
229
|
}
|
|
219
230
|
return Object.assign({}, file.get({ pain: true }), {
|
|
220
|
-
id: file.uuid,
|
|
231
|
+
id: file.uuid,
|
|
232
|
+
filePath: targetFileName,
|
|
233
|
+
targetFile
|
|
221
234
|
});
|
|
222
235
|
};
|
|
223
236
|
|
|
@@ -272,10 +285,14 @@ module.exports = fp(async (fastify, fastifyOptions) => {
|
|
|
272
285
|
});
|
|
273
286
|
|
|
274
287
|
const { count, rows } = await models.fileRecord.findAndCountAll({
|
|
275
|
-
where: queryFilter,
|
|
288
|
+
where: queryFilter,
|
|
289
|
+
offset: perPage * (currentPage - 1),
|
|
290
|
+
limit: perPage,
|
|
291
|
+
order: [['createdAt', 'desc']]
|
|
276
292
|
});
|
|
277
293
|
return {
|
|
278
|
-
pageData: rows.map(item => Object.assign({}, item.get({ plain: true }), { id: item.uuid })),
|
|
294
|
+
pageData: rows.map(item => Object.assign({}, item.get({ plain: true }), { id: item.uuid })),
|
|
295
|
+
totalCount: count
|
|
279
296
|
};
|
|
280
297
|
};
|
|
281
298
|
|
|
@@ -320,7 +337,8 @@ module.exports = fp(async (fastify, fastifyOptions) => {
|
|
|
320
337
|
}
|
|
321
338
|
|
|
322
339
|
return Object.assign({}, file.get({ plain: true }), {
|
|
323
|
-
id: file.uuid,
|
|
340
|
+
id: file.uuid,
|
|
341
|
+
buffer
|
|
324
342
|
});
|
|
325
343
|
};
|
|
326
344
|
|
|
@@ -396,7 +414,8 @@ module.exports = fp(async (fastify, fastifyOptions) => {
|
|
|
396
414
|
const tmpPath = path.resolve(os.tmpdir(), `temp_${id}_${crypto.randomBytes(6).toString('hex')}`);
|
|
397
415
|
await compressing[type].uncompress(fileStream, tmpPath);
|
|
398
416
|
const files = await glob(globOptions, {
|
|
399
|
-
cwd: tmpPath,
|
|
417
|
+
cwd: tmpPath,
|
|
418
|
+
nodir: true
|
|
400
419
|
});
|
|
401
420
|
//将文件上传到文件系统
|
|
402
421
|
const fileList = [];
|
|
@@ -406,11 +425,17 @@ module.exports = fp(async (fastify, fastifyOptions) => {
|
|
|
406
425
|
const mimetype = MimeTypes.lookup(filepath) || 'application/octet-stream';
|
|
407
426
|
const file = await uploadToFileSystem({
|
|
408
427
|
file: {
|
|
409
|
-
filename,
|
|
410
|
-
|
|
428
|
+
filename,
|
|
429
|
+
mimetype,
|
|
430
|
+
encoding: 'binary',
|
|
431
|
+
filepath
|
|
432
|
+
},
|
|
433
|
+
filename,
|
|
434
|
+
namespace
|
|
411
435
|
});
|
|
412
436
|
fileList.push({
|
|
413
|
-
dir,
|
|
437
|
+
dir,
|
|
438
|
+
file
|
|
414
439
|
});
|
|
415
440
|
}
|
|
416
441
|
fs.remove(tmpPath).catch(console.error);
|