1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/backend/model/threading/DiskMangerWorker.ts

243 lines
8.5 KiB
TypeScript
Raw Normal View History

2018-03-31 03:30:30 +08:00
import * as fs from 'fs';
import * as path from 'path';
import {DirectoryDTO} from '../../../common/entities/DirectoryDTO';
import {CameraMetadata, GPSMetadata, PhotoDTO, PhotoMetadata} from '../../../common/entities/PhotoDTO';
2018-03-31 03:30:30 +08:00
import {Logger} from '../../Logger';
import {IptcParser} from 'ts-node-iptc';
2018-11-02 17:40:09 +08:00
import {ExifParserFactory, OrientationTypes} from 'ts-exif-parser';
2018-11-05 02:28:32 +08:00
import * as ffmpeg from 'fluent-ffmpeg';
import {FfprobeData} from 'fluent-ffmpeg';
2018-03-31 03:30:30 +08:00
import {ProjectPath} from '../../ProjectPath';
import {Config} from '../../../common/config/private/Config';
import {VideoDTO, VideoMetadata} from '../../../common/entities/VideoDTO';
import {MediaDimension, MediaMetadata} from '../../../common/entities/MediaDTO';
2018-03-31 03:30:30 +08:00
const LOG_TAG = '[DiskManagerTask]';
2017-07-20 02:47:09 +08:00
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-14 05:39:09 +08:00
const extension = path.extname(fullPath).toLowerCase();
return extensions.indexOf(extension) !== -1;
}
2018-11-05 02:28:32 +08:00
private static isVideo(fullPath: string) {
const extensions = [
'.mp4',
'.webm'
];
const extension = path.extname(fullPath).toLowerCase();
return extensions.indexOf(extension) !== -1;
}
2017-07-20 02:47:09 +08:00
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);
const stat = fs.statSync(path.join(ProjectPath.ImageFolder, relativeDirectoryName));
2017-07-24 04:37:29 +08:00
const directory = <DirectoryDTO>{
2017-07-20 02:47:09 +08:00
name: directoryName,
path: directoryParent,
lastModified: Math.max(stat.ctime.getTime(), stat.mtime.getTime()),
lastScanned: Date.now(),
directories: [],
2017-07-22 01:14:22 +08:00
isPartial: false,
2018-11-05 02:28:32 +08:00
media: []
2017-07-20 02:47:09 +08:00
};
2018-11-05 02:28:32 +08:00
fs.readdir(absoluteDirectoryName, async (err, list: string[]) => {
2017-07-20 02:47:09 +08:00
if (err) {
return reject(err);
}
try {
for (let i = 0; i < list.length; i++) {
2017-07-24 04:37:29 +08:00
const file = list[i];
const fullFilePath = path.normalize(path.resolve(absoluteDirectoryName, file));
if (photosOnly === false && fs.statSync(fullFilePath).isDirectory()) {
2017-07-20 02:47:09 +08:00
const d = await DiskMangerWorker.scanDirectory(path.join(relativeDirectoryName, file),
2017-07-28 05:10:16 +08:00
Config.Server.indexing.folderPreviewSize, true
2017-07-20 02:47:09 +08:00
);
d.lastScanned = 0; // it was not a fully scan
2017-07-22 01:14:22 +08:00
d.isPartial = true;
2017-07-20 02:47:09 +08:00
directory.directories.push(d);
} else if (DiskMangerWorker.isImage(fullFilePath)) {
2018-11-05 02:28:32 +08:00
directory.media.push(<PhotoDTO>{
2017-07-20 02:47:09 +08:00
name: file,
directory: null,
metadata: await DiskMangerWorker.loadPhotoMetadata(fullFilePath)
});
2018-11-05 02:28:32 +08:00
if (maxPhotos != null && directory.media.length > maxPhotos) {
break;
}
} else if (DiskMangerWorker.isVideo(fullFilePath)) {
directory.media.push(<VideoDTO>{
name: file,
directory: null,
metadata: await DiskMangerWorker.loadVideoMetadata(fullFilePath)
2018-11-05 02:28:32 +08:00
});
if (maxPhotos != null && directory.media.length > maxPhotos) {
2017-07-20 02:47:09 +08:00
break;
}
}
}
return resolve(directory);
} catch (err) {
return reject({error: err});
}
});
});
}
public static loadVideoMetadata(fullPath: string): Promise<VideoMetadata> {
return new Promise<VideoMetadata>((resolve, reject) => {
const metadata: VideoMetadata = <VideoMetadata>{
2018-11-05 02:28:32 +08:00
size: {
width: 0,
height: 0
},
bitRate: 0,
duration: 0,
2018-11-05 02:28:32 +08:00
creationDate: 0,
fileSize: 0
};
try {
const stat = fs.statSync(fullPath);
metadata.fileSize = stat.size;
} catch (err) {
}
ffmpeg(fullPath).ffprobe((err: any, data: FfprobeData) => {
if (!!err || data === null) {
return reject(err);
}
if (!data.streams[0]) {
return resolve(metadata);
}
2018-11-05 02:28:32 +08:00
try {
metadata.size = {
width: data.streams[0].width,
height: data.streams[0].height
};
metadata.duration = Math.floor(data.streams[0].duration * 1000);
metadata.bitRate = data.streams[0].bit_rate;
metadata.creationDate = Date.parse(data.streams[0].tags.creation_time);
2018-11-05 02:28:32 +08:00
} catch (err) {
}
return resolve(metadata);
});
});
}
public static loadPhotoMetadata(fullPath: string): Promise<PhotoMetadata> {
return new Promise<PhotoMetadata>((resolve, reject) => {
2017-07-25 22:14:35 +08:00
fs.readFile(fullPath, (err, data) => {
if (err) {
return reject({file: fullPath, error: err});
}
const metadata: PhotoMetadata = <PhotoMetadata>{
2017-07-16 23:15:42 +08:00
keywords: [],
cameraData: {},
positionData: null,
size: {},
2018-11-02 18:44:13 +08:00
orientation: OrientationTypes.TOP_LEFT,
creationDate: 0,
fileSize: 0
};
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
try {
2017-07-19 05:27:14 +08:00
const exif = ExifParserFactory.create(data).parse();
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,
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;
}
2018-11-02 17:40:09 +08:00
if (exif.tags.Orientation) {
metadata.orientation = exif.tags.Orientation;
}
2017-07-16 23:15:42 +08:00
2018-11-02 18:44:13 +08:00
2017-07-16 23:15:42 +08:00
if (exif.imageSize) {
2018-11-05 02:28:32 +08:00
metadata.size = <MediaDimension> {width: exif.imageSize.width, height: exif.imageSize.height};
2017-07-16 23:15:42 +08:00
} else if (exif.tags.RelatedImageWidth && exif.tags.RelatedImageHeight) {
2018-11-05 02:28:32 +08:00
metadata.size = <MediaDimension> {width: exif.tags.RelatedImageWidth, height: exif.tags.RelatedImageHeight};
2017-07-16 23:15:42 +08:00
} else {
2018-11-05 02:28:32 +08:00
metadata.size = <MediaDimension> {width: 1, height: 1};
2017-07-16 23:15:42 +08:00
}
} catch (err) {
2018-03-31 03:30:30 +08:00
Logger.debug(LOG_TAG, 'Error parsing exif', fullPath, err);
2018-11-05 02:28:32 +08:00
metadata.size = <MediaDimension> {width: 1, height: 1};
}
try {
2017-07-16 20:03:16 +08:00
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;
}
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);
} catch (err) {
Logger.debug(LOG_TAG, 'Error parsing iptc data', fullPath, err);
}
2017-07-16 23:15:42 +08:00
metadata.creationDate = metadata.creationDate || 0;
return resolve(metadata);
} catch (err) {
return reject({file: fullPath, error: err});
}
});
}
);
}
}