2018-03-31 03:30:30 +08:00
|
|
|
import * as fs from 'fs';
|
2019-01-07 06:15:52 +08:00
|
|
|
import {Stats} from 'fs';
|
2018-03-31 03:30:30 +08:00
|
|
|
import * as path from 'path';
|
|
|
|
import {DirectoryDTO} from '../../../common/entities/DirectoryDTO';
|
2018-12-21 06:02:49 +08:00
|
|
|
import {PhotoDTO} from '../../../common/entities/PhotoDTO';
|
2018-03-31 03:30:30 +08:00
|
|
|
import {ProjectPath} from '../../ProjectPath';
|
|
|
|
import {Config} from '../../../common/config/private/Config';
|
2018-12-21 06:02:49 +08:00
|
|
|
import {VideoDTO} from '../../../common/entities/VideoDTO';
|
2018-11-26 07:26:29 +08:00
|
|
|
import {FileDTO} from '../../../common/entities/FileDTO';
|
2018-12-21 06:02:49 +08:00
|
|
|
import {MetadataLoader} from './MetadataLoader';
|
2018-03-31 03:30:30 +08:00
|
|
|
|
|
|
|
const LOG_TAG = '[DiskManagerTask]';
|
2017-07-20 02:47:09 +08:00
|
|
|
|
2017-07-04 16:24:20 +08:00
|
|
|
export class DiskMangerWorker {
|
2018-12-06 00:29:33 +08:00
|
|
|
|
|
|
|
private static readonly SupportedEXT = {
|
|
|
|
photo: [
|
2017-07-14 05:39:09 +08:00
|
|
|
'.gif',
|
|
|
|
'.jpeg', '.jpg', '.jpe',
|
|
|
|
'.png',
|
|
|
|
'.webp',
|
2019-01-13 01:08:34 +08:00
|
|
|
'.svg'
|
2018-12-06 00:29:33 +08:00
|
|
|
],
|
|
|
|
video: [
|
2018-11-25 22:22:07 +08:00
|
|
|
'.mp4',
|
|
|
|
'.webm',
|
|
|
|
'.ogv',
|
|
|
|
'.ogg'
|
2018-12-06 00:29:33 +08:00
|
|
|
],
|
|
|
|
metaFile: [
|
|
|
|
'.gpx'
|
|
|
|
]
|
|
|
|
};
|
2018-11-05 02:28:32 +08:00
|
|
|
|
2018-12-06 00:29:33 +08:00
|
|
|
private static isImage(fullPath: string) {
|
2018-11-05 02:28:32 +08:00
|
|
|
const extension = path.extname(fullPath).toLowerCase();
|
2018-12-06 00:29:33 +08:00
|
|
|
return this.SupportedEXT.photo.indexOf(extension) !== -1;
|
2018-11-05 02:28:32 +08:00
|
|
|
}
|
|
|
|
|
2018-12-06 00:29:33 +08:00
|
|
|
private static isVideo(fullPath: string) {
|
|
|
|
const extension = path.extname(fullPath).toLowerCase();
|
|
|
|
return this.SupportedEXT.video.indexOf(extension) !== -1;
|
|
|
|
}
|
2018-11-26 07:26:29 +08:00
|
|
|
|
2018-12-06 00:29:33 +08:00
|
|
|
private static isMetaFile(fullPath: string) {
|
2018-11-26 07:26:29 +08:00
|
|
|
const extension = path.extname(fullPath).toLowerCase();
|
2018-12-06 00:29:33 +08:00
|
|
|
return this.SupportedEXT.metaFile.indexOf(extension) !== -1;
|
2018-11-26 07:26:29 +08:00
|
|
|
}
|
|
|
|
|
2019-01-07 06:15:52 +08:00
|
|
|
public static calcLastModified(stat: Stats) {
|
|
|
|
return Math.max(stat.ctime.getTime(), stat.mtime.getTime());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static normalizeDirPath(dirPath: string) {
|
|
|
|
return path.normalize(path.join('.' + path.sep, dirPath));
|
|
|
|
}
|
|
|
|
|
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) => {
|
2019-01-07 06:15:52 +08:00
|
|
|
relativeDirectoryName = this.normalizeDirPath(relativeDirectoryName);
|
2017-07-20 02:47:09 +08:00
|
|
|
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));
|
2018-11-26 07:26:29 +08:00
|
|
|
const directory: DirectoryDTO = {
|
|
|
|
id: null,
|
|
|
|
parent: null,
|
2017-07-20 02:47:09 +08:00
|
|
|
name: directoryName,
|
|
|
|
path: directoryParent,
|
2019-01-07 06:15:52 +08:00
|
|
|
lastModified: this.calcLastModified(stat),
|
2017-07-20 02:47:09 +08:00
|
|
|
lastScanned: Date.now(),
|
|
|
|
directories: [],
|
2017-07-22 01:14:22 +08:00
|
|
|
isPartial: false,
|
2019-01-07 06:15:52 +08:00
|
|
|
mediaCount: 0,
|
2018-11-26 07:26:29 +08:00
|
|
|
media: [],
|
|
|
|
metaFile: []
|
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));
|
2018-12-01 00:13:43 +08:00
|
|
|
if (fs.statSync(fullFilePath).isDirectory()) {
|
|
|
|
if (photosOnly === true) {
|
|
|
|
continue;
|
|
|
|
}
|
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
|
|
|
);
|
2018-05-13 00:19:51 +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,
|
2018-12-21 06:02:49 +08:00
|
|
|
metadata: await MetadataLoader.loadPhotoMetadata(fullFilePath)
|
2017-07-20 02:47:09 +08:00
|
|
|
});
|
|
|
|
|
2018-11-05 02:28:32 +08:00
|
|
|
if (maxPhotos != null && directory.media.length > maxPhotos) {
|
|
|
|
break;
|
|
|
|
}
|
2018-12-01 00:13:43 +08:00
|
|
|
} else if (photosOnly === false && Config.Client.Video.enabled === true &&
|
2018-11-19 03:26:29 +08:00
|
|
|
DiskMangerWorker.isVideo(fullFilePath)) {
|
2018-11-05 02:28:32 +08:00
|
|
|
directory.media.push(<VideoDTO>{
|
|
|
|
name: file,
|
|
|
|
directory: null,
|
2018-12-21 06:02:49 +08:00
|
|
|
metadata: await MetadataLoader.loadVideoMetadata(fullFilePath)
|
2018-11-05 02:28:32 +08:00
|
|
|
});
|
|
|
|
|
2018-12-01 00:13:43 +08:00
|
|
|
} else if (photosOnly === false && Config.Client.MetaFile.enabled === true &&
|
|
|
|
DiskMangerWorker.isMetaFile(fullFilePath)) {
|
2018-11-26 07:26:29 +08:00
|
|
|
directory.metaFile.push(<FileDTO>{
|
|
|
|
name: file,
|
|
|
|
directory: null,
|
|
|
|
});
|
|
|
|
|
2017-07-20 02:47:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-07 06:15:52 +08:00
|
|
|
directory.mediaCount = directory.media.length;
|
|
|
|
|
2017-07-20 02:47:09 +08:00
|
|
|
return resolve(directory);
|
|
|
|
} catch (err) {
|
2018-11-19 03:26:29 +08:00
|
|
|
return reject({error: err.toString()});
|
2017-07-20 02:47:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-07-04 16:24:20 +08:00
|
|
|
}
|