@plastichub/osr-mail
Version:
This is a CLI(CommandLineInterface) toolset to convert media files
107 lines • 9.59 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.meta = exports.imageToBase64 = exports.base64ToBuffer = exports.targets = exports.fileAsBuffer = void 0;
const path = require("path");
const fs = require("fs");
const index_1 = require("../../../index");
const exists_1 = require("@plastichub/fs/exists");
const read_1 = require("@plastichub/fs/read");
const fs_1 = require("@plastichub/osr-cli-commons/fs");
const fileAsBuffer = (path) => (0, read_1.sync)(path, 'buffer') || Buffer.from("-");
exports.fileAsBuffer = fileAsBuffer;
const clone = (obj) => {
if (null == obj || "object" != typeof obj)
return obj;
var copy = obj.constructor();
for (var attr in obj) {
if (obj.hasOwnProperty(attr))
copy[attr] = obj[attr];
}
return copy;
};
const targets = (f, options) => {
const srcParts = path.parse(f);
const variables = clone(options.variables || {});
const targets = [];
const rel = path.relative(options.srcInfo.DIR, srcParts.dir);
if (options.dstInfo.IS_GLOB) {
options.dstInfo.GLOB_EXTENSIONS.forEach((e) => {
variables.SRC_NAME = srcParts.name;
variables.SRC_DIR = srcParts.dir;
let targetPath = (0, index_1.substitute)(options.alt, variables.DST_PATH, variables);
targetPath = targetPath.replace(variables.DST_GLOB, '');
if (variables.DST_FILE_EXT) {
targetPath = targetPath.replace('.' + variables.DST_FILE_EXT, '');
}
const parts = path.parse(targetPath);
//back compat
if (variables.DST_NAME === '*') {
variables.DST_NAME = '';
}
if (!parts.ext) {
if (variables.DST_PATH.indexOf(`{SRC_NAME}`) === -1) {
targetPath = path.join(targetPath, rel, srcParts.name + variables.DST_NAME.replace(variables.DST_GLOB, '') + '.' + e);
}
else {
targetPath = targetPath + variables.DST_NAME.replace(variables.DST_GLOB, '') + '.' + e;
}
}
//src.base contains dots
if (!targetPath.endsWith('.' + e)) {
targetPath += '.' + e;
}
targets.push(path.resolve(targetPath));
});
}
else {
let targetPath = '';
if (!variables.DST_PATH) {
targetPath = path.join(srcParts.dir, srcParts.base);
}
else {
targetPath = (0, index_1.substitute)(options.alt, variables.DST_PATH, variables);
if ((0, fs_1.isFile)(f) && (0, exists_1.sync)(f)) {
// targetPath = path.join(targetPath, srcParts.base)
}
else {
targetPath = path.join(targetPath, srcParts.base);
}
const targetParts = path.parse(targetPath);
if (!targetParts.ext) {
targetPath = path.join(targetPath, srcParts.base);
}
}
targets.push(path.resolve((0, fs_1.resolve)(targetPath, options.alt, variables)));
}
return targets;
};
exports.targets = targets;
const base64ToBuffer = async (b64string) => {
return Buffer.from(b64string, 'base64');
};
exports.base64ToBuffer = base64ToBuffer;
const imageToBase64 = async (imagePath) => {
try {
const imageBuffer = await fs.promises.readFile(imagePath);
const imageBase64 = imageBuffer.toString('base64');
const mimeType = path.extname(imagePath).slice(1);
const dataUri = `data:image/${mimeType};base64,${imageBase64}`;
return dataUri;
}
catch (error) {
throw new Error(`Failed to convert image to base64: ${error}`);
}
};
exports.imageToBase64 = imageToBase64;
const meta = (file, image) => {
return new Promise((resolve, reject) => {
image.metadata().then((meta) => {
resolve(meta);
}).catch((e) => {
console.error(`Error creating meta data ${file}`);
resolve(null);
});
});
};
exports.meta = meta;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibGliLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vc3JjL2xpYi9tZWRpYS9pbWFnZXMvbGliLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7OztBQUFBLDZCQUE0QjtBQUM1Qix5QkFBd0I7QUFDeEIsMENBQTJDO0FBRzNDLGtEQUFzRDtBQUN0RCw4Q0FBa0Q7QUFDbEQsdURBQWdFO0FBTXpELE1BQU0sWUFBWSxHQUFHLENBQUMsSUFBWSxFQUFFLEVBQUUsQ0FBQyxJQUFBLFdBQUksRUFBQyxJQUFJLEVBQUUsUUFBUSxDQUFXLElBQUksTUFBTSxDQUFDLElBQUksQ0FBQyxHQUFHLENBQUMsQ0FBQTtBQUFuRixRQUFB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