2016-05-26 03:58:56 +08:00
|
|
|
///<reference path="exif.d.ts"/>
|
2016-05-09 23:04:56 +08:00
|
|
|
import * as path from "path";
|
2016-12-28 03:55:51 +08:00
|
|
|
import {DirectoryDTO} from "../../common/entities/DirectoryDTO";
|
2016-05-13 00:24:26 +08:00
|
|
|
import {
|
2017-06-03 04:31:51 +08:00
|
|
|
CameraMetadata,
|
|
|
|
GPSMetadata,
|
|
|
|
ImageSize,
|
2016-12-28 03:55:51 +08:00
|
|
|
PhotoDTO,
|
2016-05-13 00:24:26 +08:00
|
|
|
PhotoMetadata,
|
2017-06-03 04:31:51 +08:00
|
|
|
PositionMetaData
|
2016-12-28 03:55:51 +08:00
|
|
|
} from "../../common/entities/PhotoDTO";
|
2016-06-25 20:13:06 +08:00
|
|
|
import {ProjectPath} from "../ProjectPath";
|
2017-06-04 04:35:47 +08:00
|
|
|
import {Logger} from "../Logger";
|
2016-03-20 02:59:19 +08:00
|
|
|
|
2017-01-23 03:30:23 +08:00
|
|
|
const Pool = require('threads').Pool;
|
|
|
|
const pool = new Pool();
|
2017-06-04 04:35:47 +08:00
|
|
|
|
|
|
|
const LOG_TAG = "[DiskManager]";
|
|
|
|
|
2017-01-23 03:30:23 +08:00
|
|
|
pool.run(
|
|
|
|
(input: {
|
|
|
|
relativeDirectoryName: string,
|
|
|
|
directoryName: string,
|
|
|
|
directoryParent: string,
|
|
|
|
absoluteDirectoryName: string
|
|
|
|
}, done) => {
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
|
|
|
const mime = require("mime");
|
|
|
|
const iptc = require("node-iptc");
|
|
|
|
const exif_parser = require("exif-parser");
|
|
|
|
|
|
|
|
|
|
|
|
let isImage = (fullPath: string) => {
|
|
|
|
let imageMimeTypes = [
|
|
|
|
'image/bmp',
|
|
|
|
'image/gif',
|
|
|
|
'image/jpeg',
|
|
|
|
'image/png',
|
|
|
|
'image/pjpeg',
|
|
|
|
'image/tiff',
|
|
|
|
'image/webp',
|
|
|
|
'image/x-tiff',
|
|
|
|
'image/x-windows-bmp'
|
|
|
|
];
|
|
|
|
|
|
|
|
let extension = mime.lookup(fullPath);
|
|
|
|
|
|
|
|
return imageMimeTypes.indexOf(extension) !== -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
let loadPhotoMetadata = (fullPath: string): Promise<PhotoMetadata> => {
|
|
|
|
return new Promise<PhotoMetadata>((resolve: (metadata: PhotoMetadata) => void, reject) => {
|
|
|
|
fs.readFile(fullPath, function (err, data) {
|
|
|
|
if (err) {
|
2017-06-04 04:35:47 +08:00
|
|
|
return reject({file: fullPath, error: err});
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
|
|
|
|
const exif = exif_parser.create(data).parse();
|
|
|
|
const iptcData = iptc(data);
|
2017-01-23 03:30:23 +08:00
|
|
|
|
2017-06-04 04:35:47 +08:00
|
|
|
const imageSize: ImageSize = {width: exif.imageSize.width, height: exif.imageSize.height};
|
|
|
|
const cameraData: CameraMetadata = {
|
2017-01-23 03:30:23 +08:00
|
|
|
ISO: exif.tags.ISO,
|
|
|
|
model: exif.tags.Modeol,
|
|
|
|
maker: exif.tags.Make,
|
|
|
|
fStop: exif.tags.FNumber,
|
|
|
|
exposure: exif.tags.ExposureTime,
|
|
|
|
focalLength: exif.tags.FocalLength,
|
|
|
|
lens: exif.tags.LensModel,
|
|
|
|
};
|
2017-06-04 04:35:47 +08:00
|
|
|
const GPS: GPSMetadata = {
|
2017-01-23 03:30:23 +08:00
|
|
|
latitude: exif.tags.GPSLatitude,
|
|
|
|
longitude: exif.tags.GPSLongitude,
|
|
|
|
altitude: exif.tags.GPSAltitude
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-06-04 04:35:47 +08:00
|
|
|
const positionData: PositionMetaData = {
|
2017-01-23 03:30:23 +08:00
|
|
|
GPSData: GPS,
|
|
|
|
country: iptcData.country_or_primary_location_name,
|
|
|
|
state: iptcData.province_or_state,
|
|
|
|
city: iptcData.city
|
|
|
|
};
|
|
|
|
|
|
|
|
//Decode characters to UTF8
|
2017-06-04 04:35:47 +08:00
|
|
|
const decode = (s: any) => {
|
2017-01-23 03:30:23 +08:00
|
|
|
for (let a, b, i = -1, l = (s = s.split("")).length, o = String.fromCharCode, c = "charCodeAt"; ++i < l;
|
|
|
|
((a = s[i][c](0)) & 0x80) &&
|
|
|
|
(s[i] = (a & 0xfc) == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ?
|
|
|
|
o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = "")
|
|
|
|
);
|
|
|
|
return s.join("");
|
|
|
|
};
|
|
|
|
|
2017-06-04 04:35:47 +08:00
|
|
|
|
|
|
|
const keywords: string[] = (iptcData.keywords || []).map((s: string) => decode(s));
|
|
|
|
const creationDate: number = iptcData.date_time ? iptcData.date_time.getTime() : 0;
|
2017-01-23 03:30:23 +08:00
|
|
|
|
|
|
|
|
2017-06-04 04:35:47 +08:00
|
|
|
const metadata: PhotoMetadata = <PhotoMetadata>{
|
2017-01-23 03:30:23 +08:00
|
|
|
keywords: keywords,
|
|
|
|
cameraData: cameraData,
|
|
|
|
positionData: positionData,
|
|
|
|
size: imageSize,
|
|
|
|
creationDate: creationDate
|
|
|
|
};
|
|
|
|
return resolve(metadata);
|
2017-06-04 04:35:47 +08:00
|
|
|
} catch (err) {
|
|
|
|
return reject({file: fullPath, error: err});
|
2017-01-23 03:30:23 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-03-18 07:11:53 +08:00
|
|
|
let parseDir = (directoryInfo: {
|
|
|
|
relativeDirectoryName: string,
|
|
|
|
directoryName: string,
|
|
|
|
directoryParent: string,
|
|
|
|
absoluteDirectoryName: string
|
|
|
|
}, maxPhotos: number = null, photosOnly: boolean = false): Promise<DirectoryDTO> => {
|
|
|
|
return new Promise<DirectoryDTO>((resolve, reject) => {
|
|
|
|
let promises: Array<Promise<any>> = [];
|
|
|
|
let directory = <DirectoryDTO>{
|
|
|
|
name: directoryInfo.directoryName,
|
|
|
|
path: directoryInfo.directoryParent,
|
|
|
|
lastUpdate: Date.now(),
|
|
|
|
directories: [],
|
|
|
|
photos: []
|
|
|
|
};
|
|
|
|
fs.readdir(directoryInfo.absoluteDirectoryName, (err, list) => {
|
2016-12-28 03:55:51 +08:00
|
|
|
|
2017-03-18 07:11:53 +08:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
2016-03-20 17:49:49 +08:00
|
|
|
|
2017-06-04 04:35:47 +08:00
|
|
|
try {
|
|
|
|
for (let i = 0; i < list.length; i++) {
|
|
|
|
let file = list[i];
|
|
|
|
let fullFilePath = path.normalize(path.resolve(directoryInfo.absoluteDirectoryName, file));
|
|
|
|
if (photosOnly == false && fs.statSync(fullFilePath).isDirectory()) {
|
|
|
|
let promise = parseDir({
|
|
|
|
relativeDirectoryName: path.join(directoryInfo.relativeDirectoryName, path.sep),
|
|
|
|
directoryName: file,
|
|
|
|
directoryParent: path.join(directoryInfo.relativeDirectoryName, path.sep),
|
|
|
|
absoluteDirectoryName: fullFilePath
|
|
|
|
},
|
|
|
|
5, true
|
|
|
|
).then((dir) => {
|
|
|
|
directory.directories.push(dir);
|
|
|
|
});
|
|
|
|
promises.push(promise);
|
|
|
|
} else if (isImage(fullFilePath)) {
|
|
|
|
|
|
|
|
|
|
|
|
let promise = loadPhotoMetadata(fullFilePath).then((photoMetadata) => {
|
|
|
|
directory.photos.push(<PhotoDTO>{
|
|
|
|
name: file,
|
|
|
|
directory: null,
|
|
|
|
metadata: photoMetadata
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
promises.push(promise);
|
|
|
|
if (maxPhotos != null && promises.length > maxPhotos) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-18 07:11:53 +08:00
|
|
|
}
|
|
|
|
}
|
2016-05-13 00:24:26 +08:00
|
|
|
|
2017-06-04 04:35:47 +08:00
|
|
|
Promise.all(promises).then(() => {
|
|
|
|
return resolve(directory);
|
|
|
|
}).catch((err) => {
|
|
|
|
return reject({directoryInfo: directoryInfo, error: err});
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
return reject({directoryInfo: directoryInfo, error: err});
|
|
|
|
}
|
2016-05-13 00:24:26 +08:00
|
|
|
|
2017-03-18 07:11:53 +08:00
|
|
|
});
|
2016-03-20 17:49:49 +08:00
|
|
|
|
2016-05-13 00:24:26 +08:00
|
|
|
});
|
2017-03-18 07:11:53 +08:00
|
|
|
};
|
2016-03-20 17:49:49 +08:00
|
|
|
|
2017-03-18 07:11:53 +08:00
|
|
|
|
|
|
|
parseDir(input).then((dir) => {
|
|
|
|
done(null, dir);
|
|
|
|
}).catch((err) => {
|
|
|
|
done(err, null);
|
2016-03-20 17:49:49 +08:00
|
|
|
});
|
2017-03-18 07:11:53 +08:00
|
|
|
|
2017-01-23 03:30:23 +08:00
|
|
|
});
|
2016-05-13 00:24:26 +08:00
|
|
|
|
2017-01-23 03:30:23 +08:00
|
|
|
export class DiskManager {
|
|
|
|
public static scanDirectory(relativeDirectoryName: string, cb: (error: any, result: DirectoryDTO) => void) {
|
2017-06-04 21:25:08 +08:00
|
|
|
Logger.silly(LOG_TAG, "scanning directory:", relativeDirectoryName);
|
2017-01-23 03:30:23 +08:00
|
|
|
let directoryName = path.basename(relativeDirectoryName);
|
|
|
|
let directoryParent = path.join(path.dirname(relativeDirectoryName), path.sep);
|
|
|
|
let absoluteDirectoryName = path.join(ProjectPath.ImageFolder, relativeDirectoryName);
|
2016-05-13 00:24:26 +08:00
|
|
|
|
2017-01-23 03:30:23 +08:00
|
|
|
pool.send({
|
|
|
|
relativeDirectoryName,
|
|
|
|
directoryName,
|
|
|
|
directoryParent,
|
|
|
|
absoluteDirectoryName
|
2017-06-04 04:35:47 +08:00
|
|
|
}).on('done', (error: any, result: DirectoryDTO) => {
|
|
|
|
if (error || !result) {
|
|
|
|
return cb(error, result);
|
|
|
|
}
|
|
|
|
|
2017-03-18 07:11:53 +08:00
|
|
|
let addDirs = (dir: DirectoryDTO) => {
|
|
|
|
dir.photos.forEach((ph) => {
|
|
|
|
ph.directory = dir;
|
|
|
|
});
|
|
|
|
dir.directories.forEach((d) => {
|
|
|
|
addDirs(d);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
addDirs(result);
|
2017-01-23 03:30:23 +08:00
|
|
|
return cb(error, result);
|
2017-06-04 04:35:47 +08:00
|
|
|
}).on('error', (error) => {
|
2017-01-23 03:30:23 +08:00
|
|
|
return cb(error, null);
|
2016-05-13 00:24:26 +08:00
|
|
|
});
|
|
|
|
}
|
2017-01-23 03:30:23 +08:00
|
|
|
|
2016-03-20 02:59:19 +08:00
|
|
|
}
|