mirror of
https://github.com/strapi/strapi.git
synced 2025-07-24 01:18:17 +00:00
34 lines
749 B
JavaScript
34 lines
749 B
JavaScript
'use strict';
|
|
/**
|
|
* Utils file containing file treatment utils
|
|
*/
|
|
|
|
const bytesToKbytes = bytes => Math.round((bytes / 1000) * 100) / 100;
|
|
|
|
const streamToBuffer = stream =>
|
|
new Promise((resolve, reject) => {
|
|
const chunks = [];
|
|
stream.on('data', chunk => {
|
|
chunks.push(chunk);
|
|
});
|
|
stream.on('end', () => {
|
|
resolve(Buffer.concat(chunks));
|
|
});
|
|
stream.on('error', reject);
|
|
});
|
|
|
|
const getStreamSize = stream =>
|
|
new Promise((resolve, reject) => {
|
|
let size = 0;
|
|
stream.on('data', chunk => (size += Buffer.byteLength(chunk)));
|
|
stream.on('close', () => resolve(size));
|
|
stream.on('error', reject);
|
|
stream.resume();
|
|
});
|
|
|
|
module.exports = {
|
|
streamToBuffer,
|
|
bytesToKbytes,
|
|
getStreamSize,
|
|
};
|