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';
|
2019-02-08 00:11:13 +08:00
|
|
|
import {Logger} from '../../Logger';
|
2018-03-31 03:30:30 +08:00
|
|
|
|
|
|
|
const LOG_TAG = '[DiskManagerTask]';
|
2017-07-20 02:47:09 +08:00
|
|
|
|
2019-12-09 21:05:06 +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
|
|
|
|
2019-01-07 06:15:52 +08:00
|
|
|
public static calcLastModified(stat: Stats) {
|
|
|
|
return Math.max(stat.ctime.getTime(), stat.mtime.getTime());
|
|
|
|
}
|
|
|
|
|
2019-07-26 05:36:57 +08:00
|
|
|
public static normalizeDirPath(dirPath: string): string {
|
2019-01-07 06:15:52 +08:00
|
|
|
return path.normalize(path.join('.' + path.sep, dirPath));
|
|
|
|
}
|
|
|
|
|
2019-07-26 05:36:57 +08:00
|
|
|
public static pathFromRelativeDirName(relativeDirectoryName: string): string {
|
|
|
|
return path.join(path.dirname(this.normalizeDirPath(relativeDirectoryName)), path.sep);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static pathFromParent(parent: { path: string, name: string }): string {
|
|
|
|
return path.join(this.normalizeDirPath(path.join(parent.path, parent.name)), path.sep);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static dirName(name: string) {
|
|
|
|
if (name.trim().length === 0) {
|
|
|
|
return '.';
|
|
|
|
}
|
|
|
|
return path.basename(name);
|
|
|
|
}
|
|
|
|
|
2019-12-08 01:26:29 +08:00
|
|
|
public static excludeDir(name: string, relativeDirectoryName: string, absoluteDirectoryName: string) {
|
|
|
|
const absoluteName = path.normalize(path.join(absoluteDirectoryName, name));
|
|
|
|
const relativeName = path.normalize(path.join(relativeDirectoryName, name));
|
2019-12-05 05:40:57 +08:00
|
|
|
|
2019-12-09 21:05:06 +08:00
|
|
|
for (let j = 0; j < Config.Server.Indexing.excludeFolderList.length; j++) {
|
|
|
|
const exclude = Config.Server.Indexing.excludeFolderList[j];
|
2019-12-05 05:40:57 +08:00
|
|
|
|
|
|
|
if (exclude.startsWith('/')) {
|
2019-12-08 01:26:29 +08:00
|
|
|
if (exclude === absoluteName) {
|
2019-12-05 05:40:57 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (exclude.includes('/')) {
|
2019-12-08 01:26:29 +08:00
|
|
|
if (path.normalize(exclude) === relativeName) {
|
2019-12-05 05:40:57 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
2019-12-08 01:26:29 +08:00
|
|
|
if (exclude === name) {
|
2019-12-05 05:40:57 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-08 01:26:29 +08:00
|
|
|
// exclude dirs that have the given files (like .ignore)
|
2019-12-09 21:05:06 +08:00
|
|
|
for (let j = 0; j < Config.Server.Indexing.excludeFileList.length; j++) {
|
|
|
|
const exclude = Config.Server.Indexing.excludeFileList[j];
|
2019-12-05 05:40:57 +08:00
|
|
|
|
2019-12-08 01:26:29 +08:00
|
|
|
if (fs.existsSync(path.join(absoluteName, exclude))) {
|
2019-12-05 05:40:57 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-09 21:05:06 +08:00
|
|
|
public static scanDirectory(relativeDirectoryName: string, settings: DiskMangerWorker.DirectoryScanSettings = {}): Promise<DirectoryDTO> {
|
2017-07-20 02:47:09 +08:00
|
|
|
return new Promise<DirectoryDTO>((resolve, reject) => {
|
2019-01-07 06:15:52 +08:00
|
|
|
relativeDirectoryName = this.normalizeDirPath(relativeDirectoryName);
|
2019-07-26 05:36:57 +08:00
|
|
|
const directoryName = DiskMangerWorker.dirName(relativeDirectoryName);
|
|
|
|
const directoryParent = this.pathFromRelativeDirName(relativeDirectoryName);
|
2017-07-20 02:47:09 +08:00
|
|
|
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];
|
2019-02-23 06:39:01 +08:00
|
|
|
const fullFilePath = path.normalize(path.join(absoluteDirectoryName, file));
|
2018-12-01 00:13:43 +08:00
|
|
|
if (fs.statSync(fullFilePath).isDirectory()) {
|
2019-12-09 21:05:06 +08:00
|
|
|
if (settings.noDirectory === true) {
|
2018-12-01 00:13:43 +08:00
|
|
|
continue;
|
|
|
|
}
|
2019-12-08 01:26:29 +08:00
|
|
|
if (DiskMangerWorker.excludeDir(file, relativeDirectoryName, absoluteDirectoryName)) {
|
2019-12-05 05:40:57 +08:00
|
|
|
continue;
|
|
|
|
}
|
2019-12-09 21:05:06 +08:00
|
|
|
|
|
|
|
// create preview directory
|
2017-07-20 02:47:09 +08:00
|
|
|
const d = await DiskMangerWorker.scanDirectory(path.join(relativeDirectoryName, file),
|
2019-12-09 21:05:06 +08:00
|
|
|
{
|
|
|
|
maxPhotos: Config.Server.Indexing.folderPreviewSize,
|
|
|
|
noMetaFile: true,
|
|
|
|
noVideo: true,
|
|
|
|
noDirectory: false
|
|
|
|
}
|
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);
|
2019-12-09 21:05:06 +08:00
|
|
|
} else if (!settings.noPhoto && 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
|
|
|
});
|
|
|
|
|
2019-12-09 21:05:06 +08:00
|
|
|
if (settings.maxPhotos && directory.media.length > settings.maxPhotos) {
|
2018-11-05 02:28:32 +08:00
|
|
|
break;
|
|
|
|
}
|
2019-12-09 21:05:06 +08:00
|
|
|
} else if (!settings.noVideo && Config.Client.Video.enabled === true &&
|
2018-11-19 03:26:29 +08:00
|
|
|
DiskMangerWorker.isVideo(fullFilePath)) {
|
2019-02-08 00:11:13 +08:00
|
|
|
try {
|
|
|
|
directory.media.push(<VideoDTO>{
|
|
|
|
name: file,
|
|
|
|
directory: null,
|
|
|
|
metadata: await MetadataLoader.loadVideoMetadata(fullFilePath)
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2019-02-15 07:25:55 +08:00
|
|
|
Logger.warn('Media loading error, skipping: ' + file + ', reason: ' + e.toString());
|
2019-02-08 00:11:13 +08:00
|
|
|
}
|
2018-11-05 02:28:32 +08:00
|
|
|
|
2019-12-09 21:05:06 +08:00
|
|
|
} else if (!settings.noMetaFile && Config.Client.MetaFile.enabled === true &&
|
2018-12-01 00:13:43 +08:00
|
|
|
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) {
|
2019-04-12 04:56:30 +08:00
|
|
|
return reject({error: err});
|
2017-07-20 02:47:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-02-08 00:11:13 +08:00
|
|
|
private static isImage(fullPath: string) {
|
|
|
|
const extension = path.extname(fullPath).toLowerCase();
|
|
|
|
return this.SupportedEXT.photo.indexOf(extension) !== -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static isVideo(fullPath: string) {
|
|
|
|
const extension = path.extname(fullPath).toLowerCase();
|
|
|
|
return this.SupportedEXT.video.indexOf(extension) !== -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static isMetaFile(fullPath: string) {
|
|
|
|
const extension = path.extname(fullPath).toLowerCase();
|
|
|
|
return this.SupportedEXT.metaFile.indexOf(extension) !== -1;
|
|
|
|
}
|
|
|
|
|
2017-07-04 16:24:20 +08:00
|
|
|
}
|
2019-12-09 21:05:06 +08:00
|
|
|
|
|
|
|
export namespace DiskMangerWorker {
|
|
|
|
export interface DirectoryScanSettings {
|
|
|
|
maxPhotos?: number;
|
|
|
|
noMetaFile?: boolean;
|
|
|
|
noVideo?: boolean;
|
|
|
|
noPhoto?: boolean;
|
|
|
|
noDirectory?: boolean;
|
|
|
|
}
|
|
|
|
}
|