2017-07-04 16:24:20 +08:00
|
|
|
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";
|
2017-07-16 20:03:16 +08:00
|
|
|
import {IptcParser} from "ts-node-iptc";
|
2017-07-19 05:27:14 +08:00
|
|
|
import {ExifParserFactory} from "ts-exif-parser";
|
2017-07-04 16:24:20 +08:00
|
|
|
import {ProjectPath} from "../../ProjectPath";
|
2017-07-18 05:12:12 +08:00
|
|
|
import {Config} from "../../../common/config/private/Config";
|
2017-07-04 16:24:20 +08:00
|
|
|
|
|
|
|
const LOG_TAG = "[DiskManagerTask]";
|
|
|
|
export class DiskMangerWorker {
|
|
|
|
private static isImage(fullPath: string) {
|
2017-07-14 05:39:09 +08:00
|
|
|
const extensions = [
|
|
|
|
'.bmp',
|
|
|
|
'.gif',
|
|
|
|
'.jpeg', '.jpg', '.jpe',
|
|
|
|
'.png',
|
|
|
|
'.tiff', '.tif',
|
|
|
|
'.webp',
|
|
|
|
'.ico',
|
|
|
|
'.tga'
|
2017-07-04 16:24:20 +08:00
|
|
|
];
|
|
|
|
|
2017-07-14 05:39:09 +08:00
|
|
|
const extension = path.extname(fullPath).toLowerCase();
|
|
|
|
return extensions.indexOf(extension) !== -1;
|
2017-07-04 16:24:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static loadPhotoMetadata(fullPath: string): Promise<PhotoMetadata> {
|
|
|
|
return new Promise<PhotoMetadata>((resolve, reject) => {
|
2017-07-10 04:00:42 +08:00
|
|
|
fs.readFile(fullPath, (err, data) => {
|
2017-07-04 16:24:20 +08:00
|
|
|
if (err) {
|
|
|
|
return reject({file: fullPath, error: err});
|
|
|
|
}
|
|
|
|
const metadata: PhotoMetadata = <PhotoMetadata>{
|
2017-07-16 23:15:42 +08:00
|
|
|
keywords: [],
|
2017-07-04 16:24:20 +08:00
|
|
|
cameraData: {},
|
|
|
|
positionData: null,
|
|
|
|
size: {},
|
2017-07-10 04:00:42 +08:00
|
|
|
creationDate: 0,
|
|
|
|
fileSize: 0
|
2017-07-04 16:24:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
2017-07-16 03:04:41 +08:00
|
|
|
try {
|
|
|
|
const stat = fs.statSync(fullPath);
|
|
|
|
metadata.fileSize = stat.size;
|
|
|
|
} catch (err) {
|
|
|
|
}
|
2017-07-11 15:32:28 +08:00
|
|
|
|
2017-07-04 16:24:20 +08:00
|
|
|
try {
|
2017-07-19 05:27:14 +08:00
|
|
|
const exif = ExifParserFactory.create(data).parse();
|
2017-07-04 16:24:20 +08:00
|
|
|
metadata.cameraData = <CameraMetadata> {
|
|
|
|
ISO: exif.tags.ISO,
|
2017-07-16 20:03:16 +08:00
|
|
|
model: exif.tags.Model,
|
2017-07-11 04:00:22 +08:00
|
|
|
make: exif.tags.Make,
|
2017-07-04 16:24:20 +08:00
|
|
|
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 = <GPSMetadata> {
|
|
|
|
latitude: exif.tags.GPSLatitude,
|
|
|
|
longitude: exif.tags.GPSLongitude,
|
|
|
|
altitude: exif.tags.GPSAltitude
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-16 23:15:42 +08:00
|
|
|
if (exif.tags.CreateDate || exif.tags.DateTimeOriginal || exif.tags.ModifyDate) {
|
|
|
|
metadata.creationDate = exif.tags.CreateDate || exif.tags.DateTimeOriginal || exif.tags.ModifyDate;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (exif.imageSize) {
|
|
|
|
metadata.size = <ImageSize> {width: exif.imageSize.width, height: exif.imageSize.height};
|
|
|
|
} else if (exif.tags.RelatedImageWidth && exif.tags.RelatedImageHeight) {
|
|
|
|
metadata.size = <ImageSize> {width: exif.tags.RelatedImageWidth, height: exif.tags.RelatedImageHeight};
|
|
|
|
} else {
|
|
|
|
metadata.size = <ImageSize> {width: 1, height: 1};
|
|
|
|
}
|
2017-07-04 16:24:20 +08:00
|
|
|
} catch (err) {
|
2017-07-16 23:15:42 +08:00
|
|
|
Logger.debug(LOG_TAG, "Error parsing exif", fullPath, err);
|
2017-07-04 16:24:20 +08:00
|
|
|
metadata.size = <ImageSize> {width: 1, height: 1};
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2017-07-16 20:03:16 +08:00
|
|
|
const iptcData = IptcParser.parse(data);
|
2017-07-04 16:24:20 +08:00
|
|
|
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;
|
|
|
|
}
|
2017-07-16 20:03:16 +08:00
|
|
|
metadata.keywords = <string[]> (iptcData.keywords || []);
|
2017-07-16 23:15:42 +08:00
|
|
|
metadata.creationDate = <number> (iptcData.date_time ? iptcData.date_time.getTime() : metadata.creationDate);
|
|
|
|
|
2017-07-04 16:24:20 +08:00
|
|
|
} catch (err) {
|
2017-07-16 23:15:42 +08:00
|
|
|
Logger.debug(LOG_TAG, "Error parsing iptc data", fullPath, err);
|
2017-07-04 16:24:20 +08:00
|
|
|
}
|
|
|
|
|
2017-07-16 23:15:42 +08:00
|
|
|
metadata.creationDate = metadata.creationDate || 0;
|
|
|
|
|
2017-07-04 16:24:20 +08:00
|
|
|
return resolve(metadata);
|
|
|
|
} catch (err) {
|
|
|
|
return reject({file: fullPath, error: err});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static scanDirectory(relativeDirectoryName: string, maxPhotos: number = null, photosOnly: boolean = false): Promise<DirectoryDTO> {
|
|
|
|
return new Promise<DirectoryDTO>((resolve, reject) => {
|
|
|
|
|
|
|
|
const directoryName = path.basename(relativeDirectoryName);
|
|
|
|
const directoryParent = path.join(path.dirname(relativeDirectoryName), path.sep);
|
|
|
|
const absoluteDirectoryName = path.join(ProjectPath.ImageFolder, relativeDirectoryName);
|
|
|
|
|
2017-07-18 05:12:12 +08:00
|
|
|
const stat = fs.statSync(path.join(ProjectPath.ImageFolder, relativeDirectoryName));
|
2017-07-04 16:24:20 +08:00
|
|
|
let directory = <DirectoryDTO>{
|
|
|
|
name: directoryName,
|
|
|
|
path: directoryParent,
|
2017-07-18 05:12:12 +08:00
|
|
|
lastUpdate: Math.max(stat.ctime.getTime(), stat.mtime.getTime()),
|
2017-07-04 16:24:20 +08:00
|
|
|
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),
|
2017-07-18 05:12:12 +08:00
|
|
|
Config.Server.folderPreviewSize, true
|
2017-07-04 16:24:20 +08:00
|
|
|
));
|
|
|
|
} else if (DiskMangerWorker.isImage(fullFilePath)) {
|
|
|
|
directory.photos.push(<PhotoDTO>{
|
|
|
|
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});
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|