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

195 lines
6.3 KiB
TypeScript
Raw Normal View History

2018-03-31 03:30:30 +08:00
import * as fs from 'fs';
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';
import {Logger} from '../../Logger';
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 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'
],
video: [
'.mp4',
'.webm',
'.ogv',
'.ogg'
],
metaFile: [
'.gpx'
]
};
2018-11-05 02:28:32 +08:00
public static calcLastModified(stat: Stats) {
return Math.max(stat.ctime.getTime(), stat.mtime.getTime());
}
public static normalizeDirPath(dirPath: string): string {
return path.normalize(path.join('.' + path.sep, dirPath));
}
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);
}
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));
for (let j = 0; j < Config.Server.indexing.excludeFolderList.length; j++) {
const exclude=Config.Server.indexing.excludeFolderList[j];
if (exclude.startsWith('/')) {
if (exclude==absoluteName) {
return true;
}
} else if (exclude.includes('/')) {
if (path.normalize(exclude)==relativeName) {
return true;
}
} else {
if (exclude==name) {
return true;
}
}
}
for (let j = 0; j < Config.Server.indexing.excludeFileList.length; j++) {
const exclude=Config.Server.indexing.excludeFileList[j];
if (fs.existsSync(path.join(absoluteName,exclude))) {
return true;
}
}
return false;
}
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) => {
relativeDirectoryName = this.normalizeDirPath(relativeDirectoryName);
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,
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,
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));
if (fs.statSync(fullFilePath).isDirectory()) {
if (photosOnly === true) {
continue;
}
if (DiskMangerWorker.excludeDir(file,relativeDirectoryName,absoluteDirectoryName)) {
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
);
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;
}
} else if (photosOnly === false && Config.Client.Video.enabled === true &&
2018-11-19 03:26:29 +08:00
DiskMangerWorker.isVideo(fullFilePath)) {
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());
}
2018-11-05 02:28:32 +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
}
}
directory.mediaCount = directory.media.length;
2017-07-20 02:47:09 +08:00
return resolve(directory);
} catch (err) {
return reject({error: err});
2017-07-20 02:47:09 +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;
}
}