mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-14 15:57:42 +02:00
Move some functions to Util, Bug Fixes, new stuff
This commit is contained in:
@@ -126,4 +126,24 @@ module.exports = class CanvasUtil {
|
||||
return resolve(lines);
|
||||
});
|
||||
}
|
||||
|
||||
static centerImage(base, data) {
|
||||
const dataRatio = data.width / data.height;
|
||||
const baseRatio = base.width / base.height;
|
||||
let { width, height } = data;
|
||||
let x = 0;
|
||||
let y = 0;
|
||||
if (baseRatio < dataRatio) {
|
||||
height = data.height;
|
||||
width = base.width * (height / base.height);
|
||||
x = (data.width - width) / 2;
|
||||
y = 0;
|
||||
} else if (baseRatio > dataRatio) {
|
||||
width = data.width;
|
||||
height = base.height * (width / base.width);
|
||||
x = 0;
|
||||
y = (data.height - height) / 2;
|
||||
}
|
||||
return { x, y, width, height };
|
||||
}
|
||||
};
|
||||
|
||||
@@ -75,6 +75,35 @@ module.exports = class Util {
|
||||
return crypto.createHash(algorithm).update(text).digest('hex');
|
||||
}
|
||||
|
||||
static streamToArray(stream) {
|
||||
if (!stream.readable) return Promise.resolve([]);
|
||||
return new Promise((resolve, reject) => {
|
||||
const array = [];
|
||||
function onData(data) {
|
||||
array.push(data);
|
||||
}
|
||||
function onEnd(error) {
|
||||
if (error) reject(error);
|
||||
else resolve(array);
|
||||
cleanup();
|
||||
}
|
||||
function onClose() {
|
||||
resolve(array);
|
||||
cleanup();
|
||||
}
|
||||
function cleanup() {
|
||||
stream.removeListener('data', onData);
|
||||
stream.removeListener('end', onEnd);
|
||||
stream.removeListener('error', onEnd);
|
||||
stream.removeListener('close', onClose);
|
||||
}
|
||||
stream.on('data', onData);
|
||||
stream.on('end', onEnd);
|
||||
stream.on('error', onEnd);
|
||||
stream.on('close', onClose);
|
||||
});
|
||||
}
|
||||
|
||||
static today(timeZone) {
|
||||
const now = new Date();
|
||||
if (timeZone) now.setUTCHours(timeZone);
|
||||
|
||||
Reference in New Issue
Block a user