@kne/fastify-file-manager 3.0.1-alpha.0 → 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 +46 -25
- package/package.json +1 -1
|
@@ -29,39 +29,64 @@ module.exports = fp(async (fastify, fastifyOptions) => {
|
|
|
29
29
|
const hash = crypto.createHash('md5');
|
|
30
30
|
const extension = path.extname(filename);
|
|
31
31
|
const tmpPath = path.resolve(os.tmpdir(), `temp_${filename}_${crypto.randomBytes(6).toString('hex')}`);
|
|
32
|
-
const writeStream = fs.createWriteStream(tmpPath);
|
|
33
32
|
let fileSize = 0;
|
|
34
33
|
if (file.file) {
|
|
35
|
-
|
|
36
|
-
hash.update(chunk); // 更新哈希
|
|
37
|
-
writeStream.write(chunk); // 写入文件
|
|
38
|
-
fileSize += chunk.length; // 更新文件大小
|
|
39
|
-
});
|
|
40
|
-
|
|
34
|
+
const writeStream = fs.createWriteStream(tmpPath);
|
|
41
35
|
await new Promise((resolve, reject) => {
|
|
36
|
+
const cleanup = () => {
|
|
37
|
+
file.file.removeAllListeners();
|
|
38
|
+
writeStream.removeAllListeners();
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const handleError = err => {
|
|
42
|
+
cleanup();
|
|
43
|
+
reject(err);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
writeStream.on('error', handleError);
|
|
47
|
+
writeStream.on('drain', () => {
|
|
48
|
+
file.file.resume();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
file.file.on('data', chunk => {
|
|
52
|
+
hash.update(chunk);
|
|
53
|
+
const canWrite = writeStream.write(chunk);
|
|
54
|
+
if (!canWrite) {
|
|
55
|
+
file.file.pause();
|
|
56
|
+
}
|
|
57
|
+
fileSize += chunk.length;
|
|
58
|
+
});
|
|
59
|
+
|
|
42
60
|
file.file.on('end', () => {
|
|
43
|
-
writeStream.end();
|
|
61
|
+
writeStream.end();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
file.file.on('error', handleError);
|
|
65
|
+
writeStream.on('finish', () => {
|
|
66
|
+
cleanup();
|
|
44
67
|
resolve();
|
|
45
68
|
});
|
|
46
|
-
file.file.on('error', reject);
|
|
47
69
|
});
|
|
48
|
-
} else if (file.toBuffer) {
|
|
49
|
-
const buffer = await file.toBuffer();
|
|
70
|
+
} else if (file.toBuffer || file.buffer) {
|
|
71
|
+
const buffer = file.toBuffer ? await file.toBuffer() : file.buffer;
|
|
50
72
|
hash.update(buffer);
|
|
51
|
-
|
|
73
|
+
await fs.writeFile(tmpPath, buffer);
|
|
52
74
|
fileSize = buffer.byteLength;
|
|
53
|
-
|
|
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
|
+
});
|
|
82
|
+
await fs.copy(file.filepath, tmpPath);
|
|
83
|
+
const stat = await fs.stat(tmpPath);
|
|
84
|
+
|
|
85
|
+
fileSize = stat.size;
|
|
54
86
|
} else {
|
|
55
87
|
throw new Error('文件类型不支持');
|
|
56
88
|
}
|
|
57
89
|
|
|
58
|
-
await new Promise((resolve, reject) => {
|
|
59
|
-
writeStream.on('finish', () => {
|
|
60
|
-
resolve();
|
|
61
|
-
});
|
|
62
|
-
writeStream.on('error', reject);
|
|
63
|
-
});
|
|
64
|
-
|
|
65
90
|
const digest = hash.digest('hex');
|
|
66
91
|
|
|
67
92
|
let storageType;
|
|
@@ -392,26 +417,22 @@ module.exports = fp(async (fastify, fastifyOptions) => {
|
|
|
392
417
|
cwd: tmpPath,
|
|
393
418
|
nodir: true
|
|
394
419
|
});
|
|
395
|
-
console.log('---->1:', files);
|
|
396
420
|
//将文件上传到文件系统
|
|
397
421
|
const fileList = [];
|
|
398
422
|
for (let dir of files) {
|
|
399
423
|
const filepath = path.resolve(tmpPath, dir);
|
|
400
424
|
const filename = path.basename(dir);
|
|
401
|
-
const fileStream = fs.createReadStream(filepath);
|
|
402
425
|
const mimetype = MimeTypes.lookup(filepath) || 'application/octet-stream';
|
|
403
|
-
console.log(filename, mimetype);
|
|
404
426
|
const file = await uploadToFileSystem({
|
|
405
427
|
file: {
|
|
406
428
|
filename,
|
|
407
429
|
mimetype,
|
|
408
430
|
encoding: 'binary',
|
|
409
|
-
|
|
431
|
+
filepath
|
|
410
432
|
},
|
|
411
433
|
filename,
|
|
412
434
|
namespace
|
|
413
435
|
});
|
|
414
|
-
console.log('---->2:', dir, file);
|
|
415
436
|
fileList.push({
|
|
416
437
|
dir,
|
|
417
438
|
file
|