2016-05-26 03:58:56 +08:00
|
|
|
///<reference path="exif.d.ts"/>
|
2016-05-09 23:04:56 +08:00
|
|
|
import * as fs from "fs";
|
|
|
|
import * as path from "path";
|
|
|
|
import * as mime from "mime";
|
2016-05-26 03:58:56 +08:00
|
|
|
import * as iptc from "node-iptc";
|
|
|
|
import * as exif_parser from "exif-parser";
|
2016-03-20 17:49:49 +08:00
|
|
|
import {Directory} from "../../common/entities/Directory";
|
2016-05-13 00:24:26 +08:00
|
|
|
import {
|
|
|
|
Photo,
|
|
|
|
PhotoMetadata,
|
|
|
|
ImageSize,
|
|
|
|
CameraMetadata,
|
|
|
|
PositionMetaData,
|
|
|
|
GPSMetadata
|
|
|
|
} from "../../common/entities/Photo";
|
2016-06-25 20:13:06 +08:00
|
|
|
import {ProjectPath} from "../ProjectPath";
|
2016-03-20 02:59:19 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
export class DiskManager {
|
2016-12-27 06:36:38 +08:00
|
|
|
public static scanDirectory(relativeDirectoryName: string, cb: (error: any, result: Directory) => void) {
|
2016-04-22 19:23:44 +08:00
|
|
|
console.log("DiskManager: scanDirectory");
|
2016-03-20 17:49:49 +08:00
|
|
|
let directoryName = path.basename(relativeDirectoryName);
|
2016-05-09 23:04:56 +08:00
|
|
|
let directoryParent = path.join(path.dirname(relativeDirectoryName), "/");
|
2016-06-25 20:13:06 +08:00
|
|
|
let absoluteDirectoryName = path.join(ProjectPath.ImageFolder, relativeDirectoryName);
|
2016-03-20 02:59:19 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
let directory = new Directory(1, directoryName, directoryParent, new Date(), [], []);
|
2016-03-20 17:49:49 +08:00
|
|
|
|
2016-12-27 06:36:38 +08:00
|
|
|
let promises: Array< Promise<any> > = [];
|
2016-03-20 17:49:49 +08:00
|
|
|
fs.readdir(absoluteDirectoryName, function (err, list) {
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
if (err) {
|
|
|
|
return cb(err, null);
|
2016-03-20 17:49:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < list.length; i++) {
|
|
|
|
let file = list[i];
|
|
|
|
let fullFilePath = path.resolve(absoluteDirectoryName, file);
|
2016-05-09 23:04:56 +08:00
|
|
|
if (fs.statSync(fullFilePath).isDirectory()) {
|
|
|
|
directory.directories.push(new Directory(2, file, relativeDirectoryName, new Date(), [], []));
|
2016-03-20 17:49:49 +08:00
|
|
|
}
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
if (DiskManager.isImage(fullFilePath)) {
|
2016-05-13 00:24:26 +08:00
|
|
|
|
|
|
|
|
2016-12-27 06:36:38 +08:00
|
|
|
let promise = DiskManager.loadPhotoMetadata(fullFilePath).then((photoMetadata) => {
|
2016-05-13 00:24:26 +08:00
|
|
|
directory.photos.push(new Photo(1, file, directory, photoMetadata));
|
|
|
|
});
|
|
|
|
|
|
|
|
promises.push(promise);
|
2016-03-20 17:49:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-27 06:36:38 +08:00
|
|
|
Promise.all(promises).then(() => {
|
2016-05-13 00:24:26 +08:00
|
|
|
return cb(err, directory);
|
|
|
|
});
|
2016-03-20 17:49:49 +08:00
|
|
|
|
|
|
|
});
|
2016-03-20 02:59:19 +08:00
|
|
|
}
|
2016-03-20 17:49:49 +08:00
|
|
|
|
2016-12-27 06:36:38 +08:00
|
|
|
private static isImage(fullPath: string) {
|
2016-03-20 17:49:49 +08:00
|
|
|
let imageMimeTypes = [
|
|
|
|
'image/bmp',
|
|
|
|
'image/gif',
|
|
|
|
'image/jpeg',
|
|
|
|
'image/png',
|
|
|
|
'image/pjpeg',
|
|
|
|
'image/tiff',
|
|
|
|
'image/webp',
|
|
|
|
'image/x-tiff',
|
|
|
|
'image/x-windows-bmp'
|
|
|
|
];
|
|
|
|
|
2016-12-27 06:36:38 +08:00
|
|
|
let extension = mime.lookup(fullPath);
|
2016-03-20 17:49:49 +08:00
|
|
|
|
|
|
|
if (imageMimeTypes.indexOf(extension) !== -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-03-20 02:59:19 +08:00
|
|
|
}
|
2016-05-13 00:24:26 +08:00
|
|
|
|
2016-07-11 18:52:15 +08:00
|
|
|
/*
|
|
|
|
UTF8 = {
|
2016-05-13 00:24:26 +08:00
|
|
|
|
2016-07-11 18:52:15 +08:00
|
|
|
encode: function(s){
|
|
|
|
|
|
|
|
for(var c, i = -1, l = (s = s.split("")).length, o = String.fromCharCode; ++i < l;
|
|
|
|
|
|
|
|
s[i] = (c = s[i].charCodeAt(0)) >= 127 ? o(0xc0 | (c >>> 6)) + o(0x80 | (c & 0x3f)) : s[i]
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
return s.join("");
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
decode: function(s){
|
|
|
|
|
|
|
|
for(var 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("");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};*/
|
2016-12-27 06:36:38 +08:00
|
|
|
private static loadPhotoMetadata(fullPath: string): Promise<PhotoMetadata> {
|
|
|
|
return new Promise<PhotoMetadata>((resolve: (metadata: PhotoMetadata) => void, reject) => {
|
2016-05-13 00:24:26 +08:00
|
|
|
fs.readFile(fullPath, function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
let exif = exif_parser.create(data).parse();
|
|
|
|
let iptcData = iptc(data);
|
|
|
|
|
2016-12-27 06:36:38 +08:00
|
|
|
let imageSize: ImageSize = {width: exif.imageSize.width, height: exif.imageSize.height};
|
|
|
|
let cameraData: CameraMetadata = {
|
2016-05-13 00:24:26 +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,
|
|
|
|
};
|
2016-12-27 06:36:38 +08:00
|
|
|
let GPS: GPSMetadata = {
|
2016-05-13 00:24:26 +08:00
|
|
|
latitude: exif.tags.GPSLatitude,
|
|
|
|
longitude: exif.tags.GPSLongitude,
|
|
|
|
altitude: exif.tags.GPSAltitude
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-12-27 06:36:38 +08:00
|
|
|
let positionData: PositionMetaData = {
|
2016-05-13 00:24:26 +08:00
|
|
|
GPSData: GPS,
|
|
|
|
country: iptcData.country_or_primary_location_name,
|
|
|
|
state: iptcData.province_or_state,
|
|
|
|
city: iptcData.city
|
|
|
|
};
|
|
|
|
|
2016-07-11 18:52:15 +08:00
|
|
|
//Decode characters to UTF8
|
2016-12-27 06:36:38 +08:00
|
|
|
let decode = (s: any) => {
|
|
|
|
for (let a, b, i = -1, l = (s = s.split("")).length, o = String.fromCharCode, c = "charCodeAt"; ++i < l;
|
2016-07-11 18:52:15 +08:00
|
|
|
((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("");
|
|
|
|
};
|
|
|
|
|
2016-12-27 06:36:38 +08:00
|
|
|
let keywords: [string] = iptcData.keywords.map((s: string) => decode(s));
|
|
|
|
let creationDate: Date = iptcData.date_time;
|
2016-07-11 18:52:15 +08:00
|
|
|
console.log(keywords);
|
2016-05-13 00:24:26 +08:00
|
|
|
|
|
|
|
|
2016-12-27 06:36:38 +08:00
|
|
|
let metadata: PhotoMetadata = new PhotoMetadata(keywords, cameraData, positionData, imageSize, creationDate);
|
2016-05-13 00:24:26 +08:00
|
|
|
resolve(metadata);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2016-03-20 02:59:19 +08:00
|
|
|
}
|