@mtucourses/thumbor
Version:
👍 a URL builder for Thumbor
169 lines • 5.68 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Thumbor = exports.HORIZONTAL_POSITION = exports.VERTICAL_POSITION = exports.FIT_IN_TYPE = void 0;
const crypto_1 = __importDefault(require("crypto"));
const utils_1 = require("./utils");
var FIT_IN_TYPE;
(function (FIT_IN_TYPE) {
FIT_IN_TYPE["DEFAULT"] = "DEFAULT";
FIT_IN_TYPE["FULL"] = "FULL";
FIT_IN_TYPE["ADAPTIVE"] = "ADAPTIVE";
})(FIT_IN_TYPE = exports.FIT_IN_TYPE || (exports.FIT_IN_TYPE = {}));
var VERTICAL_POSITION;
(function (VERTICAL_POSITION) {
VERTICAL_POSITION["TOP"] = "TOP";
VERTICAL_POSITION["MIDDLE"] = "MIDDLE";
VERTICAL_POSITION["BOTTOM"] = "BOTTOM";
})(VERTICAL_POSITION = exports.VERTICAL_POSITION || (exports.VERTICAL_POSITION = {}));
var HORIZONTAL_POSITION;
(function (HORIZONTAL_POSITION) {
HORIZONTAL_POSITION["LEFT"] = "LEFT";
HORIZONTAL_POSITION["CENTER"] = "CENTER";
HORIZONTAL_POSITION["RIGHT"] = "RIGHT";
})(HORIZONTAL_POSITION = exports.HORIZONTAL_POSITION || (exports.HORIZONTAL_POSITION = {}));
const defaultParametersFactory = () => ({
imagePath: '',
width: 0,
height: 0,
smart: false,
trimFlag: false,
withFlipHorizontally: false,
withFlipVertically: false,
filtersCalls: []
});
class Thumbor {
constructor({ url, key }) {
this.parameters = defaultParametersFactory();
this.url = url;
this.key = key;
}
setPath(path) {
this.parameters.imagePath = (path.startsWith('/')) ? path.slice(1, path.length) : path;
return this;
}
resize(width, height) {
this.parameters.width = width;
this.parameters.height = height;
this.parameters.fitInType = undefined;
return this;
}
smartCrop(smartCrop) {
this.parameters.smart = smartCrop;
return this;
}
trim() {
this.parameters.trimFlag = true;
return this;
}
fitIn(width, height, type = FIT_IN_TYPE.DEFAULT) {
this.parameters.width = width;
this.parameters.height = height;
this.parameters.fitInType = type;
return this;
}
flipHorizontally() {
this.parameters.withFlipHorizontally = true;
return this;
}
flipVertically() {
this.parameters.withFlipVertically = true;
return this;
}
halign(halign) {
this.parameters.halignValue = halign;
return this;
}
valign(valign) {
this.parameters.valignValue = valign;
return this;
}
filter(filterCall) {
this.parameters.filtersCalls.push(filterCall);
return this;
}
crop(crop) {
this.parameters.cropValues = crop;
return this;
}
buildURL() {
const operation = this.getOperationPath();
const dataToEncrypt = operation + '/' + this.parameters.imagePath;
if (this.key) {
const digest = crypto_1.default
.createHmac('sha1', this.key)
.update(dataToEncrypt)
.digest('base64')
.replace(/\+/g, '-').replace(/\//g, '_');
this.parameters = defaultParametersFactory();
return this.url + '/' + digest + '/' + dataToEncrypt;
}
this.parameters = defaultParametersFactory();
return this.url + '/unsafe/' + dataToEncrypt;
}
getURLParts() {
const parts = [];
if (this.parameters.trimFlag) {
parts.push('trim');
}
if (this.parameters.cropValues) {
parts.push(this.parameters.cropValues.left.toString() +
'x' + this.parameters.cropValues.top.toString() +
':' + this.parameters.cropValues.right.toString() +
'x' + this.parameters.cropValues.bottom.toString());
}
switch (this.parameters.fitInType) {
case FIT_IN_TYPE.DEFAULT:
parts.push('fit-in');
break;
case FIT_IN_TYPE.FULL:
parts.push('full-fit-in');
break;
case FIT_IN_TYPE.ADAPTIVE:
parts.push('adaptative-fit-in');
break;
default:
break;
}
if (utils_1.isDefined(this.parameters.width) ||
utils_1.isDefined(this.parameters.height) ||
this.parameters.withFlipHorizontally ||
this.parameters.withFlipVertically) {
let sizeString = '';
if (this.parameters.withFlipHorizontally) {
sizeString += '-';
}
sizeString += this.parameters.width;
sizeString += 'x';
if (this.parameters.withFlipVertically) {
sizeString += '-';
}
sizeString += this.parameters.height;
parts.push(sizeString);
}
if (this.parameters.halignValue) {
parts.push(this.parameters.halignValue);
}
if (this.parameters.valignValue) {
parts.push(this.parameters.valignValue);
}
if (this.parameters.smart) {
parts.push('smart');
}
if (this.parameters.filtersCalls.length > 0) {
parts.push('filters:' + this.parameters.filtersCalls.join(':'));
}
return parts;
}
getOperationPath() {
const parts = this.getURLParts();
if (parts.length === 0) {
return '';
}
return parts.join('/');
}
}
exports.Thumbor = Thumbor;
//# sourceMappingURL=index.js.map