@kne/fastify-file-manager 2.0.6 → 2.0.8
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 +67 -13
- package/package.json +2 -1
|
@@ -5,6 +5,7 @@ const path = require('node:path');
|
|
|
5
5
|
const { NotFound } = require('http-errors');
|
|
6
6
|
const os = require('node:os');
|
|
7
7
|
const { Readable } = require('node:stream');
|
|
8
|
+
const compressing = require('compressing');
|
|
8
9
|
|
|
9
10
|
module.exports = fp(async (fastify, options) => {
|
|
10
11
|
const { models, services } = fastify.fileManager;
|
|
@@ -29,7 +30,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
29
30
|
const writeStream = fs.createWriteStream(tmpPath);
|
|
30
31
|
let fileSize = 0;
|
|
31
32
|
if (file.file) {
|
|
32
|
-
file.file.on('data',
|
|
33
|
+
file.file.on('data', chunk => {
|
|
33
34
|
hash.update(chunk); // 更新哈希
|
|
34
35
|
writeStream.write(chunk); // 写入文件
|
|
35
36
|
fileSize += chunk.length; // 更新文件大小
|
|
@@ -51,6 +52,13 @@ module.exports = fp(async (fastify, options) => {
|
|
|
51
52
|
throw new Error('文件类型不支持');
|
|
52
53
|
}
|
|
53
54
|
|
|
55
|
+
await new Promise((resolve, reject) => {
|
|
56
|
+
writeStream.on('finish', () => {
|
|
57
|
+
resolve();
|
|
58
|
+
});
|
|
59
|
+
writeStream.on('error', reject);
|
|
60
|
+
});
|
|
61
|
+
|
|
54
62
|
const digest = hash.digest('hex');
|
|
55
63
|
|
|
56
64
|
let storageType;
|
|
@@ -66,9 +74,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
66
74
|
const writeStream = fs.createWriteStream(filepath);
|
|
67
75
|
const readStream = fs.createReadStream(tmpPath);
|
|
68
76
|
await new Promise((resolve, reject) => {
|
|
69
|
-
readStream.pipe(writeStream)
|
|
70
|
-
.on('finish', resolve)
|
|
71
|
-
.on('error', reject);
|
|
77
|
+
readStream.pipe(writeStream).on('finish', resolve).on('error', reject);
|
|
72
78
|
});
|
|
73
79
|
storageType = 'local';
|
|
74
80
|
}
|
|
@@ -89,9 +95,17 @@ module.exports = fp(async (fastify, options) => {
|
|
|
89
95
|
file.storageType = storageType;
|
|
90
96
|
await file.save();
|
|
91
97
|
return file;
|
|
92
|
-
})(() =>
|
|
93
|
-
|
|
94
|
-
|
|
98
|
+
})(() =>
|
|
99
|
+
models.fileRecord.create({
|
|
100
|
+
filename,
|
|
101
|
+
namespace: namespace || options.namespace,
|
|
102
|
+
encoding,
|
|
103
|
+
mimetype,
|
|
104
|
+
hash: digest,
|
|
105
|
+
size: fileSize,
|
|
106
|
+
storageType
|
|
107
|
+
})
|
|
108
|
+
);
|
|
95
109
|
return Object.assign({}, outputFile.get({ plain: true }), { id: outputFile.uuid });
|
|
96
110
|
};
|
|
97
111
|
|
|
@@ -161,7 +175,9 @@ module.exports = fp(async (fastify, options) => {
|
|
|
161
175
|
targetFile = await ossServices.downloadFile({ filename: targetFileName });
|
|
162
176
|
}
|
|
163
177
|
return Object.assign({}, file.get({ pain: true }), {
|
|
164
|
-
id: file.uuid,
|
|
178
|
+
id: file.uuid,
|
|
179
|
+
filePath: targetFileName,
|
|
180
|
+
targetFile
|
|
165
181
|
});
|
|
166
182
|
};
|
|
167
183
|
|
|
@@ -190,10 +206,13 @@ module.exports = fp(async (fastify, options) => {
|
|
|
190
206
|
}
|
|
191
207
|
|
|
192
208
|
const { count, rows } = await models.fileRecord.findAndCountAll({
|
|
193
|
-
where: queryFilter,
|
|
209
|
+
where: queryFilter,
|
|
210
|
+
offset: perPage * (currentPage - 1),
|
|
211
|
+
limit: perPage
|
|
194
212
|
});
|
|
195
213
|
return {
|
|
196
|
-
pageData: rows.map(item => Object.assign({}, item.get({ plain: true }), { id: item.uuid })),
|
|
214
|
+
pageData: rows.map(item => Object.assign({}, item.get({ plain: true }), { id: item.uuid })),
|
|
215
|
+
totalCount: count
|
|
197
216
|
};
|
|
198
217
|
};
|
|
199
218
|
|
|
@@ -201,7 +220,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
201
220
|
await models.fileRecord.destroy({
|
|
202
221
|
where: {
|
|
203
222
|
uuid: {
|
|
204
|
-
[Op.in]: ids.map(
|
|
223
|
+
[Op.in]: ids.map(str => str.split('?')[0])
|
|
205
224
|
}
|
|
206
225
|
}
|
|
207
226
|
});
|
|
@@ -238,7 +257,8 @@ module.exports = fp(async (fastify, options) => {
|
|
|
238
257
|
}
|
|
239
258
|
|
|
240
259
|
return Object.assign({}, file.get({ plain: true }), {
|
|
241
|
-
id: file.uuid,
|
|
260
|
+
id: file.uuid,
|
|
261
|
+
buffer
|
|
242
262
|
});
|
|
243
263
|
};
|
|
244
264
|
|
|
@@ -263,6 +283,35 @@ module.exports = fp(async (fastify, options) => {
|
|
|
263
283
|
}
|
|
264
284
|
};
|
|
265
285
|
|
|
286
|
+
const getCompressFileStream = async ({ ids, type = 'zip' }) => {
|
|
287
|
+
const fileList = await models.fileRecord.findAll({
|
|
288
|
+
where: {
|
|
289
|
+
uuid: {
|
|
290
|
+
[Op.in]: ids.map(str => str.split('?')[0])
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
const compressStream = new compressing[type].Stream();
|
|
295
|
+
for (const file of fileList) {
|
|
296
|
+
const fileStream = await getFileStream({ id: file.uuid });
|
|
297
|
+
compressStream.addEntry(fileStream, {
|
|
298
|
+
name: file.filename,
|
|
299
|
+
relativePath: file.filename
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
return compressStream;
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
const getCompressFileBlob = async (...args) => {
|
|
306
|
+
const compressStream = await getCompressFileStream(...args);
|
|
307
|
+
const chunks = [];
|
|
308
|
+
return new Promise((resolve, reject) => {
|
|
309
|
+
compressStream.on('data', chunk => chunks.push(chunk));
|
|
310
|
+
compressStream.on('end', () => resolve(new Blob(chunks)));
|
|
311
|
+
compressStream.on('error', reject);
|
|
312
|
+
});
|
|
313
|
+
};
|
|
314
|
+
|
|
266
315
|
const getFileInstance = async ({ id, uuid }) => {
|
|
267
316
|
return detail({ id, uuid });
|
|
268
317
|
};
|
|
@@ -276,8 +325,11 @@ module.exports = fp(async (fastify, options) => {
|
|
|
276
325
|
deleteFiles,
|
|
277
326
|
renameFile,
|
|
278
327
|
getFileBlob,
|
|
279
|
-
getFileStream,
|
|
328
|
+
getFileStream,
|
|
329
|
+
getCompressFileStream,
|
|
330
|
+
getCompressFileBlob,
|
|
280
331
|
getFileInstance,
|
|
332
|
+
// 兼容之前api,后面可能会删掉
|
|
281
333
|
fileRecord: {
|
|
282
334
|
uploadToFileSystem,
|
|
283
335
|
uploadFromUrl,
|
|
@@ -288,6 +340,8 @@ module.exports = fp(async (fastify, options) => {
|
|
|
288
340
|
renameFile,
|
|
289
341
|
getFileBlob,
|
|
290
342
|
getFileStream,
|
|
343
|
+
getCompressFileStream,
|
|
344
|
+
getCompressFileBlob,
|
|
291
345
|
getFileInstance
|
|
292
346
|
}
|
|
293
347
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kne/fastify-file-manager",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.8",
|
|
4
4
|
"description": "用于管理静态文件上传查看等",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -49,6 +49,7 @@
|
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"@fastify/multipart": "^9.0.3",
|
|
51
51
|
"@fastify/static": "^8.1.1",
|
|
52
|
+
"compressing": "^2.0.0",
|
|
52
53
|
"fs-extra": "^11.2.0",
|
|
53
54
|
"http-errors": "^2.0.0"
|
|
54
55
|
}
|