@kne/fastify-file-manager 2.0.0 → 2.0.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 +48 -44
- package/package.json +1 -1
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
2
|
const fs = require('fs-extra');
|
|
3
|
-
const crypto = require('crypto');
|
|
4
|
-
const path = require('path');
|
|
3
|
+
const crypto = require('node:crypto');
|
|
4
|
+
const path = require('node:path');
|
|
5
5
|
const { NotFound } = require('http-errors');
|
|
6
|
+
const os = require('node:os');
|
|
6
7
|
|
|
7
8
|
module.exports = fp(async (fastify, options) => {
|
|
8
9
|
const { models, services } = fastify.fileManager;
|
|
@@ -11,28 +12,46 @@ module.exports = fp(async (fastify, options) => {
|
|
|
11
12
|
const { filename, encoding, mimetype } = file;
|
|
12
13
|
const hash = crypto.createHash('md5');
|
|
13
14
|
const extension = path.extname(filename);
|
|
14
|
-
|
|
15
|
+
const tmpPath = path.resolve(os.tmpdir(), `temp_${filename}_${crypto.randomBytes(6).toString('hex')}`);
|
|
16
|
+
const writeStream = fs.createWriteStream(tmpPath);
|
|
17
|
+
let fileSize = 0;
|
|
18
|
+
if (file.file) {
|
|
19
|
+
file.file.on('data', (chunk) => {
|
|
20
|
+
hash.update(chunk); // 更新哈希
|
|
21
|
+
writeStream.write(chunk); // 写入文件
|
|
22
|
+
fileSize += chunk.length; // 更新文件大小
|
|
23
|
+
});
|
|
15
24
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
25
|
+
await new Promise((resolve, reject) => {
|
|
26
|
+
file.file.on('end', () => {
|
|
27
|
+
writeStream.end(); // 关闭写入流
|
|
28
|
+
resolve();
|
|
29
|
+
});
|
|
30
|
+
file.file.on('error', reject);
|
|
31
|
+
});
|
|
32
|
+
} else if (file.toBuffer) {
|
|
33
|
+
const buffer = await file.toBuffer();
|
|
34
|
+
hash.update(buffer);
|
|
35
|
+
writeStream.write(buffer);
|
|
36
|
+
fileSize = buffer.byteLength;
|
|
37
|
+
} else {
|
|
38
|
+
throw new Error('文件类型不支持');
|
|
21
39
|
}
|
|
40
|
+
|
|
22
41
|
const digest = hash.digest('hex');
|
|
23
42
|
|
|
24
43
|
let storageType;
|
|
25
44
|
const ossServices = options.ossAdapter();
|
|
26
45
|
if (typeof ossServices.uploadFile === 'function') {
|
|
27
46
|
// 使用流上传到OSS
|
|
28
|
-
const
|
|
29
|
-
await ossServices.uploadFileStream({ stream:
|
|
47
|
+
const readStream = fs.createReadStream(tmpPath);
|
|
48
|
+
await ossServices.uploadFileStream({ stream: readStream, filename: `${digest}${extension}` });
|
|
30
49
|
storageType = 'oss';
|
|
31
50
|
} else {
|
|
32
51
|
// 使用流写入本地文件
|
|
33
52
|
const filepath = path.resolve(options.root, `${digest}${extension}`);
|
|
34
53
|
const writeStream = fs.createWriteStream(filepath);
|
|
35
|
-
const readStream =
|
|
54
|
+
const readStream = fs.createReadStream(tmpPath);
|
|
36
55
|
await new Promise((resolve, reject) => {
|
|
37
56
|
readStream.pipe(writeStream)
|
|
38
57
|
.on('finish', resolve)
|
|
@@ -41,6 +60,9 @@ module.exports = fp(async (fastify, options) => {
|
|
|
41
60
|
storageType = 'local';
|
|
42
61
|
}
|
|
43
62
|
|
|
63
|
+
//清楚临时文件
|
|
64
|
+
await fs.remove(tmpPath);
|
|
65
|
+
|
|
44
66
|
const outputFile = await (async create => {
|
|
45
67
|
if (!id) {
|
|
46
68
|
return await create();
|
|
@@ -53,21 +75,19 @@ module.exports = fp(async (fastify, options) => {
|
|
|
53
75
|
file.encoding = encoding;
|
|
54
76
|
file.mimetype = mimetype;
|
|
55
77
|
file.hash = digest;
|
|
56
|
-
file.size =
|
|
78
|
+
file.size = fileSize;
|
|
57
79
|
file.storageType = storageType;
|
|
58
80
|
await file.save();
|
|
59
81
|
return file;
|
|
60
|
-
})(() =>
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
})
|
|
70
|
-
);
|
|
82
|
+
})(() => models.fileRecord.create({
|
|
83
|
+
filename,
|
|
84
|
+
namespace: namespace || options.namespace,
|
|
85
|
+
encoding,
|
|
86
|
+
mimetype,
|
|
87
|
+
hash: digest,
|
|
88
|
+
size: fileSize,
|
|
89
|
+
storageType
|
|
90
|
+
}));
|
|
71
91
|
return Object.assign({}, outputFile.get({ plain: true }), { id: outputFile.uuid });
|
|
72
92
|
};
|
|
73
93
|
|
|
@@ -76,21 +96,11 @@ module.exports = fp(async (fastify, options) => {
|
|
|
76
96
|
if (!response.ok) {
|
|
77
97
|
throw new Error('下载文件失败');
|
|
78
98
|
}
|
|
79
|
-
const chunks = [];
|
|
80
|
-
for await (const chunk of response.body) {
|
|
81
|
-
chunks.push(chunk);
|
|
82
|
-
}
|
|
83
|
-
const buffer = Buffer.concat(chunks);
|
|
84
99
|
const tempFile = {
|
|
85
100
|
filename: path.basename(url).split('?')[0],
|
|
86
101
|
mimetype: response.headers.get('content-type'),
|
|
87
102
|
encoding: 'binary',
|
|
88
|
-
|
|
89
|
-
const readable = new require('stream').Readable();
|
|
90
|
-
readable.push(buffer);
|
|
91
|
-
readable.push(null);
|
|
92
|
-
return readable;
|
|
93
|
-
}
|
|
103
|
+
file: response.body
|
|
94
104
|
};
|
|
95
105
|
return await uploadToFileSystem({ id, file: tempFile, namespace });
|
|
96
106
|
};
|
|
@@ -135,9 +145,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
135
145
|
targetFile = await ossServices.downloadFile({ filename: targetFileName });
|
|
136
146
|
}
|
|
137
147
|
return Object.assign({}, file.get({ pain: true }), {
|
|
138
|
-
id: file.uuid,
|
|
139
|
-
filePath: targetFileName,
|
|
140
|
-
targetFile
|
|
148
|
+
id: file.uuid, filePath: targetFileName, targetFile
|
|
141
149
|
});
|
|
142
150
|
};
|
|
143
151
|
|
|
@@ -166,13 +174,10 @@ module.exports = fp(async (fastify, options) => {
|
|
|
166
174
|
}
|
|
167
175
|
|
|
168
176
|
const { count, rows } = await models.fileRecord.findAndCountAll({
|
|
169
|
-
where: queryFilter,
|
|
170
|
-
offset: perPage * (currentPage - 1),
|
|
171
|
-
limit: perPage
|
|
177
|
+
where: queryFilter, offset: perPage * (currentPage - 1), limit: perPage
|
|
172
178
|
});
|
|
173
179
|
return {
|
|
174
|
-
pageData: rows.map(item => Object.assign({}, item.get({ plain: true }), { id: item.uuid })),
|
|
175
|
-
totalCount: count
|
|
180
|
+
pageData: rows.map(item => Object.assign({}, item.get({ plain: true }), { id: item.uuid })), totalCount: count
|
|
176
181
|
};
|
|
177
182
|
};
|
|
178
183
|
|
|
@@ -198,8 +203,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
198
203
|
};
|
|
199
204
|
|
|
200
205
|
Object.assign(services, {
|
|
201
|
-
uploadToFileSystem, uploadFromUrl, getFileUrl, getFileInfo, getFileList, deleteFiles, renameFile,
|
|
202
|
-
// 兼容之前api,后面可能会删掉
|
|
206
|
+
uploadToFileSystem, uploadFromUrl, getFileUrl, getFileInfo, getFileList, deleteFiles, renameFile, // 兼容之前api,后面可能会删掉
|
|
203
207
|
fileRecord: { uploadToFileSystem, uploadFromUrl, getFileUrl, getFileInfo, getFileList, deleteFiles, renameFile }
|
|
204
208
|
});
|
|
205
209
|
});
|