picgo-plugin-s3-own
Version:
picgo amazon s3 uploader for Own use
172 lines (171 loc) • 5.03 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatPath = formatPath;
exports.extractInfo = extractInfo;
exports.getProxyAgent = getProxyAgent;
const crypto_1 = __importDefault(require("crypto"));
const file_type_1 = require("file-type");
const mime_1 = __importDefault(require("mime"));
const hpagent_1 = require("hpagent");
class FileNameGenerator {
constructor(info) {
this.date = new Date();
this.info = info;
}
year() {
return `${this.date.getFullYear()}`;
}
month() {
return this.date.getMonth() < 9
? `0${this.date.getMonth() + 1}`
: `${this.date.getMonth() + 1}`;
}
day() {
return this.date.getDate() < 9
? `0${this.date.getDate()}`
: `${this.date.getDate()}`;
}
hour() {
return this.date.getHours().toString().padStart(2, "0");
}
minute() {
return this.date.getMinutes().toString().padStart(2, "0");
}
second() {
return this.date.getSeconds().toString().padStart(2, "0");
}
millisecond() {
return this.date.getMilliseconds().toString().padStart(3, "0");
}
fullName() {
return this.info.fileName;
}
fileName() {
return this.info.fileName.replace(this.info.extname, "");
}
extName() {
return this.info.extname.replace(".", "");
}
md5() {
return crypto_1.default.createHash("md5").update(this.imgBuffer()).digest("hex");
}
md5B64() {
return crypto_1.default
.createHash("md5")
.update(this.imgBuffer())
.digest("base64")
.replace(/\+/g, "-")
.replace(/\//g, "_")
.replace(/=+$/, "");
}
md5B64Short() {
return crypto_1.default
.createHash("md5")
.update(this.imgBuffer())
.digest("base64")
.replace(/\+/g, "-")
.replace(/\//g, "_")
.slice(0, 7);
}
sha1() {
return crypto_1.default.createHash("sha1").update(this.imgBuffer()).digest("hex");
}
sha256() {
return crypto_1.default.createHash("sha256").update(this.imgBuffer()).digest("hex");
}
timestamp() {
return Math.floor(Date.now() / 1000).toString();
}
timestampMS() {
return Date.now().toString();
}
imgBuffer() {
return this.info.base64Image ? this.info.base64Image : this.info.buffer;
}
}
FileNameGenerator.fields = [
"year",
"month",
"day",
"hour",
"minute",
"second",
"millisecond",
"fullName",
"fileName",
"extName",
"md5",
"md5B64",
"md5B64Short",
"sha1",
"sha256",
"timestamp",
"timestampMS",
];
function formatPath(info, format) {
if (!format) {
return info.fileName;
}
const fileNameGenerator = new FileNameGenerator(info);
let formatPath = format;
for (const key of FileNameGenerator.fields) {
const re = new RegExp(`{${key}}`, "g");
formatPath = formatPath.replace(re, fileNameGenerator[key]());
}
return formatPath;
}
async function extractInfo(info) {
var _a;
const result = {};
if (info.base64Image) {
const body = info.base64Image.replace(/^data:[/\w]+;base64,/, "");
result.contentType = (_a = info.base64Image.match(/[^:]\w+\/[\w-+\d.]+(?=;|,)/)) === null || _a === void 0 ? void 0 : _a[0];
result.body = Buffer.from(body, "base64");
result.contentEncoding = "base64";
}
else {
if (info.extname) {
result.contentType = mime_1.default.getType(info.extname);
}
result.body = info.buffer;
}
// fallback to detect from buffer
if (!result.contentType) {
const fileType = await (0, file_type_1.fromBuffer)(result.body);
result.contentType = fileType === null || fileType === void 0 ? void 0 : fileType.mime;
}
return result;
}
function formatHttpProxyURL(url = "") {
if (!url)
return "";
if (!/^https?:\/\//.test(url)) {
const [host, port] = url.split(":");
return `http://${host.replace("127.0.0.1", "localhost")}:${port}`;
}
try {
const { protocol, hostname, port } = new URL(url);
return `${protocol}//${hostname.replace("127.0.0.1", "localhost")}:${port}`;
}
catch (e) {
return "";
}
}
function getProxyAgent(proxy, sslEnabled, rejectUnauthorized) {
const formatedProxy = formatHttpProxyURL(proxy);
if (!formatedProxy) {
return undefined;
}
const Agent = sslEnabled ? hpagent_1.HttpsProxyAgent : hpagent_1.HttpProxyAgent;
const options = {
keepAlive: true,
keepAliveMsecs: 1000,
scheduling: "lifo",
rejectUnauthorized,
proxy: formatedProxy,
};
return new Agent(options);
}