Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public static async getFileDimensions(type: string, file: string, canvasJson: any, annotationJson: any): Promise {
console.log(chalk.green('getting file dimensions for: ') + file);
if (!Utils.isJsonFile(file)) {
switch (type.toLowerCase()) {
// if it's an image, get the width and height and add to the annotation body and canvas
case ExternalResourceType.IMAGE :
const image: any = await Jimp.read(file);
const width: number = image.bitmap.width;
const height: number = image.bitmap.height;
canvasJson.width = Math.max(canvasJson.width || 0, width);
canvasJson.height = Math.max(canvasJson.height || 0, height);
annotationJson.body.width = width;
annotationJson.body.height = height;
break;
// if it's a sound, get the duration and add to the canvas
case ExternalResourceType.SOUND :
case ExternalResourceType.VIDEO :
try {
const info: any = await ffprobe(file, { path: ffprobeStatic.path });
if (info && info.streams && info.streams.length) {
const duration: number = Number(info.streams[0].duration);
canvasJson.duration = duration;
if (!annotationJson.body.format) {
delete annotationJson.body.format;
console.warn(chalk.yellow('unable to determine format of ' + file));
}
if (yml.label) {
annotationJson.body.label = Utils.getLabel(yml.label);
canvasJson.label = Utils.getLabel(yml.label);
} else {
annotationJson.body.label = Utils.getLabel(this.infoYml.label);
}
// if the annotation is an image and the id points to an info.json
// add an image service pointing to the info.json
if (annotationJson.body.type && annotationJson.body.type.toLowerCase() === ExternalResourceType.IMAGE && extname(annotationJson.body.id) === '.json') {
const service: any = Utils.cloneJson(imageServiceBoilerplate);
service[0].id = annotationJson.body.id.substr(0, annotationJson.body.id.lastIndexOf('/'));
annotationJson.body.service = service;
}
// if there's a value, and we're using a recognised motivation (except painting)
if (yml.value && this._config.annotation.motivations[motivation] && motivation !== Utils.normaliseType(AnnotationMotivation.PAINTING)) {
annotationJson.body.value = yml.value;
}
if (yml.value && !Utils.isURL(yml.value) && annotationJson.body.type) {
// get the path to the annotated file
const dirName: string = dirname(file);
let path: string = join(dirName, yml.value);
path = Utils.normaliseFilePath(path);
await Utils.getFileDimensions(annotationJson.body.type, path, canvasJson, annotationJson)