magica
Version:
ImageMagick for browser and Node.js, easy setup, high level API and Command Line Interface, including WASM binary for an easy setup.
57 lines • 1.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const file_1 = require("../file/file");
const magickLoaded_1 = require("../imageMagick/magickLoaded");
const options_1 = require("../options");
/**
* if given a file it ignores its contents and alwasys read again from FS
*/
function readFile(f, FS = magickLoaded_1.getFS()) {
return new file_1.File(getFileName(f), FS.readFile(getFilePath(f)));
}
exports.readFile = readFile;
/**
* Returns file name / path of given file relative to emscripten FS root (in the context of emscripten FS)
*/
function getFileName(f) {
const { emscriptenNodeFsRoot } = options_1.getOptions();
const path = typeof f === 'string' ? f : f.name;
return path.startsWith(`${emscriptenNodeFsRoot}/`) ? path.substring(`${emscriptenNodeFsRoot}/`.length, path.length) : `${path}`;
}
exports.getFileName = getFileName;
/**
* Returns absolute path of given file (in the context of emscripten FS)
*/
function getFilePath(f) {
const { emscriptenNodeFsRoot } = options_1.getOptions();
const path = typeof f === 'string' ? f : f.name;
return path.startsWith(`${emscriptenNodeFsRoot}/`) ? path : `${emscriptenNodeFsRoot}/${path}`;
}
exports.getFilePath = getFilePath;
function writeFile(f, FS = magickLoaded_1.getFS()) {
FS.writeFile(getFilePath(f.name), f.content);
}
exports.writeFile = writeFile;
function removeFile(f, FS = magickLoaded_1.getFS()) {
FS.unlink(getFilePath(getFilePath(f)));
}
exports.removeFile = removeFile;
function isDir(f, FS = magickLoaded_1.getFS()) {
try {
return FS.isDir(FS.stat(getFilePath(f)).mode);
}
catch (error) {
return false;
}
}
exports.isDir = isDir;
function isFile(f, FS = magickLoaded_1.getFS()) {
try {
return FS.isFile(FS.stat(getFilePath(f)).mode);
}
catch (error) {
return false;
}
}
exports.isFile = isFile;
//# sourceMappingURL=fileUtil.js.map