@kne/fastify-file-manager 2.0.7 → 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 +60 -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; // 更新文件大小
|
|
@@ -73,9 +74,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
73
74
|
const writeStream = fs.createWriteStream(filepath);
|
|
74
75
|
const readStream = fs.createReadStream(tmpPath);
|
|
75
76
|
await new Promise((resolve, reject) => {
|
|
76
|
-
readStream.pipe(writeStream)
|
|
77
|
-
.on('finish', resolve)
|
|
78
|
-
.on('error', reject);
|
|
77
|
+
readStream.pipe(writeStream).on('finish', resolve).on('error', reject);
|
|
79
78
|
});
|
|
80
79
|
storageType = 'local';
|
|
81
80
|
}
|
|
@@ -96,9 +95,17 @@ module.exports = fp(async (fastify, options) => {
|
|
|
96
95
|
file.storageType = storageType;
|
|
97
96
|
await file.save();
|
|
98
97
|
return file;
|
|
99
|
-
})(() =>
|
|
100
|
-
|
|
101
|
-
|
|
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
|
+
);
|
|
102
109
|
return Object.assign({}, outputFile.get({ plain: true }), { id: outputFile.uuid });
|
|
103
110
|
};
|
|
104
111
|
|
|
@@ -168,7 +175,9 @@ module.exports = fp(async (fastify, options) => {
|
|
|
168
175
|
targetFile = await ossServices.downloadFile({ filename: targetFileName });
|
|
169
176
|
}
|
|
170
177
|
return Object.assign({}, file.get({ pain: true }), {
|
|
171
|
-
id: file.uuid,
|
|
178
|
+
id: file.uuid,
|
|
179
|
+
filePath: targetFileName,
|
|
180
|
+
targetFile
|
|
172
181
|
});
|
|
173
182
|
};
|
|
174
183
|
|
|
@@ -197,10 +206,13 @@ module.exports = fp(async (fastify, options) => {
|
|
|
197
206
|
}
|
|
198
207
|
|
|
199
208
|
const { count, rows } = await models.fileRecord.findAndCountAll({
|
|
200
|
-
where: queryFilter,
|
|
209
|
+
where: queryFilter,
|
|
210
|
+
offset: perPage * (currentPage - 1),
|
|
211
|
+
limit: perPage
|
|
201
212
|
});
|
|
202
213
|
return {
|
|
203
|
-
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
|
|
204
216
|
};
|
|
205
217
|
};
|
|
206
218
|
|
|
@@ -208,7 +220,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
208
220
|
await models.fileRecord.destroy({
|
|
209
221
|
where: {
|
|
210
222
|
uuid: {
|
|
211
|
-
[Op.in]: ids.map(
|
|
223
|
+
[Op.in]: ids.map(str => str.split('?')[0])
|
|
212
224
|
}
|
|
213
225
|
}
|
|
214
226
|
});
|
|
@@ -245,7 +257,8 @@ module.exports = fp(async (fastify, options) => {
|
|
|
245
257
|
}
|
|
246
258
|
|
|
247
259
|
return Object.assign({}, file.get({ plain: true }), {
|
|
248
|
-
id: file.uuid,
|
|
260
|
+
id: file.uuid,
|
|
261
|
+
buffer
|
|
249
262
|
});
|
|
250
263
|
};
|
|
251
264
|
|
|
@@ -270,6 +283,35 @@ module.exports = fp(async (fastify, options) => {
|
|
|
270
283
|
}
|
|
271
284
|
};
|
|
272
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
|
+
|
|
273
315
|
const getFileInstance = async ({ id, uuid }) => {
|
|
274
316
|
return detail({ id, uuid });
|
|
275
317
|
};
|
|
@@ -283,8 +325,11 @@ module.exports = fp(async (fastify, options) => {
|
|
|
283
325
|
deleteFiles,
|
|
284
326
|
renameFile,
|
|
285
327
|
getFileBlob,
|
|
286
|
-
getFileStream,
|
|
328
|
+
getFileStream,
|
|
329
|
+
getCompressFileStream,
|
|
330
|
+
getCompressFileBlob,
|
|
287
331
|
getFileInstance,
|
|
332
|
+
// 兼容之前api,后面可能会删掉
|
|
288
333
|
fileRecord: {
|
|
289
334
|
uploadToFileSystem,
|
|
290
335
|
uploadFromUrl,
|
|
@@ -295,6 +340,8 @@ module.exports = fp(async (fastify, options) => {
|
|
|
295
340
|
renameFile,
|
|
296
341
|
getFileBlob,
|
|
297
342
|
getFileStream,
|
|
343
|
+
getCompressFileStream,
|
|
344
|
+
getCompressFileBlob,
|
|
298
345
|
getFileInstance
|
|
299
346
|
}
|
|
300
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
|
}
|