img-master
Version:
An image batch processing tool with multifunctional and unlimited
55 lines (48 loc) • 1.57 kB
JavaScript
const Fs = require("fs");
const Chalk = require("chalk");
const Glob = require("glob");
const { RandomNum } = require("trample/node");
const { ACTION_TEXT, OPERATION_TEXT } = require("../i18n");
const { EXTS, OUTPUT_DIR, TINYIMG_URL } = require("./getting");
function AutoBin(fn, ...rest) {
const lib = require(`../lib/${fn}`);
lib(...rest);
}
function FilterImg() {
const ignore = Object.values(OUTPUT_DIR);
const regexp = `**/*.{${EXTS.join(",")}}`;
const imgs = Glob.sync(regexp).filter(v => Fs.statSync(v).isFile() && ignore.every(w => !v.includes(w)));
console.log(OPERATION_TEXT.targetCount(imgs.length));
return imgs;
}
function FormatExt(path = "") {
const newPath = path.replace(/\.jpe?g$/ig, ".jpg").replace(/\.png$/ig, ".png");
return newPath;
}
function RandomHeader() {
const ip = new Array(4).fill(0).map(() => parseInt(Math.random() * 255)).join(".");
const index = RandomNum(0, 1);
return {
headers: {
"Cache-Control": "no-cache",
"Content-Type": "application/x-www-form-urlencoded",
"Postman-Token": Date.now(),
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36",
"X-Forwarded-For": ip
},
hostname: TINYIMG_URL[index],
method: "POST",
path: "/web/shrink",
rejectUnauthorized: false
};
}
function ShowTitle(type = "") {
console.log(Chalk.white.bgMagenta(`### ${ACTION_TEXT[type]} ###`));
}
module.exports = {
AutoBin,
FilterImg,
FormatExt,
RandomHeader,
ShowTitle
};