@kne/fastify-file-manager 2.0.8 → 2.0.9

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.
@@ -79,7 +79,7 @@ module.exports = fp(async (fastify, options) => {
79
79
  storageType = 'local';
80
80
  }
81
81
 
82
- //清楚临时文件
82
+ //清除临时文件
83
83
  await fs.remove(tmpPath);
84
84
 
85
85
  const outputFile = await (async create => {
@@ -95,17 +95,9 @@ module.exports = fp(async (fastify, options) => {
95
95
  file.storageType = storageType;
96
96
  await file.save();
97
97
  return file;
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
- );
98
+ })(() => models.fileRecord.create({
99
+ filename, namespace: namespace || options.namespace, encoding, mimetype, hash: digest, size: fileSize, storageType
100
+ }));
109
101
  return Object.assign({}, outputFile.get({ plain: true }), { id: outputFile.uuid });
110
102
  };
111
103
 
@@ -135,12 +127,28 @@ module.exports = fp(async (fastify, options) => {
135
127
  nodeStream.emit('error', err);
136
128
  }
137
129
  };
138
- readChunk();
130
+ readChunk().catch(err => {
131
+ throw err;
132
+ });
133
+
134
+ let filename = path.basename(url).split('?')[0];
135
+
136
+ const contentDisposition = response.headers.get('content-disposition');
137
+
138
+ if (contentDisposition) {
139
+ const filenameMatch = contentDisposition.match(/filename="?(.+)"?/i);
140
+ if (filenameMatch && filenameMatch[1]) {
141
+ filename = filenameMatch[1];
142
+ }
143
+ }
144
+
145
+ const searchParams = new URLSearchParams(url.split('?')[1]);
146
+ if (searchParams.get('filename')) {
147
+ filename = searchParams.get('filename');
148
+ }
149
+
139
150
  const tempFile = {
140
- filename: path.basename(url).split('?')[0],
141
- mimetype: response.headers.get('content-type'),
142
- encoding: 'binary',
143
- file: nodeStream
151
+ filename, mimetype: response.headers.get('content-type'), encoding: 'binary', file: nodeStream
144
152
  };
145
153
  return await uploadToFileSystem({ id, file: tempFile, namespace });
146
154
  };
@@ -175,9 +183,7 @@ module.exports = fp(async (fastify, options) => {
175
183
  targetFile = await ossServices.downloadFile({ filename: targetFileName });
176
184
  }
177
185
  return Object.assign({}, file.get({ pain: true }), {
178
- id: file.uuid,
179
- filePath: targetFileName,
180
- targetFile
186
+ id: file.uuid, filePath: targetFileName, targetFile
181
187
  });
182
188
  };
183
189
 
@@ -206,13 +212,10 @@ module.exports = fp(async (fastify, options) => {
206
212
  }
207
213
 
208
214
  const { count, rows } = await models.fileRecord.findAndCountAll({
209
- where: queryFilter,
210
- offset: perPage * (currentPage - 1),
211
- limit: perPage
215
+ where: queryFilter, offset: perPage * (currentPage - 1), limit: perPage
212
216
  });
213
217
  return {
214
- pageData: rows.map(item => Object.assign({}, item.get({ plain: true }), { id: item.uuid })),
215
- totalCount: count
218
+ pageData: rows.map(item => Object.assign({}, item.get({ plain: true }), { id: item.uuid })), totalCount: count
216
219
  };
217
220
  };
218
221
 
@@ -257,32 +260,30 @@ module.exports = fp(async (fastify, options) => {
257
260
  }
258
261
 
259
262
  return Object.assign({}, file.get({ plain: true }), {
260
- id: file.uuid,
261
- buffer
263
+ id: file.uuid, buffer
262
264
  });
263
265
  };
264
266
 
265
- const getFileStream = async ({ id }) => {
266
- const file = await detail({ id });
267
-
267
+ const getFileReadStream = file => {
268
268
  const extension = path.extname(file.filename);
269
269
  const targetFileName = `${file.hash}${extension}`;
270
270
  const ossServices = options.ossAdapter();
271
-
272
271
  if (file.storageType === 'oss') {
273
272
  if (typeof ossServices.getFileStream !== 'function') {
274
273
  throw new Error('ossAdapter未正确配置无法读取oss类型存储文件');
275
274
  }
276
- return await ossServices.getFileStream({ filename: targetFileName });
275
+ return ossServices.getFileStream({ filename: targetFileName });
277
276
  } else {
278
277
  const filePath = path.resolve(options.root, targetFileName);
279
- if (!(await fs.exists(filePath))) {
280
- throw new NotFound();
281
- }
282
278
  return fs.createReadStream(filePath);
283
279
  }
284
280
  };
285
281
 
282
+ const getFileStream = async ({ id }) => {
283
+ const file = await detail({ id });
284
+ return getFileReadStream(file);
285
+ };
286
+
286
287
  const getCompressFileStream = async ({ ids, type = 'zip' }) => {
287
288
  const fileList = await models.fileRecord.findAll({
288
289
  where: {
@@ -291,14 +292,30 @@ module.exports = fp(async (fastify, options) => {
291
292
  }
292
293
  }
293
294
  });
294
- const compressStream = new compressing[type].Stream();
295
+ const tmpPath = path.resolve(os.tmpdir(), `temp_compress_file_${crypto.randomBytes(6).toString('hex')}`);
296
+ await fs.mkdir(tmpPath);
297
+ const files = [];
295
298
  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
299
+ const filepath = path.resolve(tmpPath, file.filename);
300
+ const writeStream = fs.createWriteStream(filepath);
301
+ const fileStream = getFileReadStream(file);
302
+ fileStream.pipe(writeStream);
303
+ await new Promise((resolve, reject) => {
304
+ writeStream.on('finish', resolve);
305
+ writeStream.on('error', reject);
300
306
  });
307
+ files.push(filepath);
301
308
  }
309
+ const compressStream = new compressing[type].Stream();
310
+ files.forEach(filepath => {
311
+ compressStream.addEntry(path.resolve(filepath));
312
+ });
313
+ compressStream.on('error', () => {
314
+ fs.remove(tmpPath);
315
+ });
316
+ compressStream.on('end', () => {
317
+ fs.remove(tmpPath);
318
+ });
302
319
  return compressStream;
303
320
  };
304
321
 
@@ -307,7 +324,7 @@ module.exports = fp(async (fastify, options) => {
307
324
  const chunks = [];
308
325
  return new Promise((resolve, reject) => {
309
326
  compressStream.on('data', chunk => chunks.push(chunk));
310
- compressStream.on('end', () => resolve(new Blob(chunks)));
327
+ compressStream.on('end', () => resolve(Buffer.concat(chunks)));
311
328
  compressStream.on('error', reject);
312
329
  });
313
330
  };
@@ -326,10 +343,10 @@ module.exports = fp(async (fastify, options) => {
326
343
  renameFile,
327
344
  getFileBlob,
328
345
  getFileStream,
346
+ getFileReadStream,
329
347
  getCompressFileStream,
330
348
  getCompressFileBlob,
331
- getFileInstance,
332
- // 兼容之前api,后面可能会删掉
349
+ getFileInstance, // 兼容之前api,后面可能会删掉
333
350
  fileRecord: {
334
351
  uploadToFileSystem,
335
352
  uploadFromUrl,
@@ -340,6 +357,7 @@ module.exports = fp(async (fastify, options) => {
340
357
  renameFile,
341
358
  getFileBlob,
342
359
  getFileStream,
360
+ getFileReadStream,
343
361
  getCompressFileStream,
344
362
  getCompressFileBlob,
345
363
  getFileInstance
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-file-manager",
3
- "version": "2.0.8",
3
+ "version": "2.0.9",
4
4
  "description": "用于管理静态文件上传查看等",
5
5
  "main": "index.js",
6
6
  "scripts": {