///
import {DirectoryDTO} from "../../../common/entities/DirectoryDTO";
import {CameraMetadata, GPSMetadata, ImageSize, PhotoDTO, PhotoMetadata} from "../../../common/entities/PhotoDTO";
import {Logger} from "../../Logger";
import * as fs from "fs";
import * as path from "path";
import {IptcParser} from "ts-node-iptc";
import * as exif_parser from "exif-parser";
import {ProjectPath} from "../../ProjectPath";
const LOG_TAG = "[DiskManagerTask]";
export class DiskMangerWorker {
private static isImage(fullPath: string) {
const extensions = [
'.bmp',
'.gif',
'.jpeg', '.jpg', '.jpe',
'.png',
'.tiff', '.tif',
'.webp',
'.ico',
'.tga'
];
const extension = path.extname(fullPath).toLowerCase();
return extensions.indexOf(extension) !== -1;
}
private static loadPhotoMetadata(fullPath: string): Promise {
return new Promise((resolve, reject) => {
fs.readFile(fullPath, (err, data) => {
if (err) {
return reject({file: fullPath, error: err});
}
const metadata: PhotoMetadata = {
keywords: {},
cameraData: {},
positionData: null,
size: {},
creationDate: 0,
fileSize: 0
};
try {
try {
const stat = fs.statSync(fullPath);
metadata.fileSize = stat.size;
} catch (err) {
}
try {
const exif = exif_parser.create(data).parse();
metadata.cameraData = {
ISO: exif.tags.ISO,
model: exif.tags.Model,
make: exif.tags.Make,
fStop: exif.tags.FNumber,
exposure: exif.tags.ExposureTime,
focalLength: exif.tags.FocalLength,
lens: exif.tags.LensModel,
};
if (!isNaN(exif.tags.GPSLatitude) || exif.tags.GPSLongitude || exif.tags.GPSAltitude) {
metadata.positionData = metadata.positionData || {};
metadata.positionData.GPSData = {
latitude: exif.tags.GPSLatitude,
longitude: exif.tags.GPSLongitude,
altitude: exif.tags.GPSAltitude
};
}
metadata.size = {width: exif.imageSize.width, height: exif.imageSize.height};
} catch (err) {
Logger.info(LOG_TAG, "Error parsing exif", fullPath);
metadata.size = {width: 1, height: 1};
}
try {
const iptcData = IptcParser.parse(data);
if (iptcData.country_or_primary_location_name || iptcData.province_or_state || iptcData.city) {
metadata.positionData = metadata.positionData || {};
metadata.positionData.country = iptcData.country_or_primary_location_name;
metadata.positionData.state = iptcData.province_or_state;
metadata.positionData.city = iptcData.city;
}
metadata.keywords = (iptcData.keywords || []);
metadata.creationDate = (iptcData.date_time ? iptcData.date_time.getTime() : 0);
} catch (err) {
Logger.info(LOG_TAG, "Error parsing iptc data", fullPath, err);
}
return resolve(metadata);
} catch (err) {
return reject({file: fullPath, error: err});
}
});
}
);
}
public static scanDirectory(relativeDirectoryName: string, maxPhotos: number = null, photosOnly: boolean = false): Promise {
return new Promise((resolve, reject) => {
const directoryName = path.basename(relativeDirectoryName);
const directoryParent = path.join(path.dirname(relativeDirectoryName), path.sep);
const absoluteDirectoryName = path.join(ProjectPath.ImageFolder, relativeDirectoryName);
// let promises: Array> = [];
let directory = {
name: directoryName,
path: directoryParent,
lastUpdate: Date.now(),
directories: [],
photos: []
};
fs.readdir(absoluteDirectoryName, async (err, list) => {
if (err) {
return reject(err);
}
try {
for (let i = 0; i < list.length; i++) {
let file = list[i];
let fullFilePath = path.normalize(path.resolve(absoluteDirectoryName, file));
if (photosOnly == false && fs.statSync(fullFilePath).isDirectory()) {
directory.directories.push(await DiskMangerWorker.scanDirectory(path.join(relativeDirectoryName, file),
5, true
));
} else if (DiskMangerWorker.isImage(fullFilePath)) {
directory.photos.push({
name: file,
directory: null,
metadata: await DiskMangerWorker.loadPhotoMetadata(fullFilePath)
});
if (maxPhotos != null && directory.photos.length > maxPhotos) {
break;
}
}
}
return resolve(directory);
} catch (err) {
return reject({error: err});
}
});
});
}
}