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

119 lines
4.2 KiB
TypeScript
Raw Normal View History

2019-01-14 00:38:39 +08:00
import {IIndexingTaskManager} from '../interfaces/IIndexingTaskManager';
2019-07-28 04:56:12 +08:00
import {TaskProgressDTO} from '../../../common/entities/settings/TaskProgressDTO';
import {ObjectManagers} from '../ObjectManagers';
2019-01-14 00:38:39 +08:00
import * as path from 'path';
import * as fs from 'fs';
import {SQLConnection} from './SQLConnection';
import {DirectoryEntity} from './enitites/DirectoryEntity';
import {Logger} from '../../Logger';
import {RendererInput, ThumbnailSourceType, ThumbnailWorker} from '../threading/ThumbnailWorker';
import {Config} from '../../../common/config/private/Config';
import {MediaDTO} from '../../../common/entities/MediaDTO';
import {ProjectPath} from '../../ProjectPath';
import {ThumbnailGeneratorMWs} from '../../middlewares/thumbnail/ThumbnailGeneratorMWs';
const LOG_TAG = '[IndexingTaskManager]';
export class IndexingTaskManager implements IIndexingTaskManager {
directoriesToIndex: string[] = [];
2019-07-28 04:56:12 +08:00
indexingProgress: TaskProgressDTO = null;
2019-01-14 00:38:39 +08:00
enabled = false;
private indexNewDirectory = async (createThumbnails: boolean = false) => {
if (this.directoriesToIndex.length === 0) {
this.indexingProgress = null;
if (global.gc) {
global.gc();
}
return;
}
const directory = this.directoriesToIndex.shift();
2019-07-28 04:56:12 +08:00
this.indexingProgress.comment = directory;
2019-01-14 00:38:39 +08:00
this.indexingProgress.left = this.directoriesToIndex.length;
const scanned = await ObjectManagers.getInstance().IndexingManager.indexDirectory(directory);
2019-01-14 00:38:39 +08:00
if (this.enabled === false) {
return;
}
2019-07-28 04:56:12 +08:00
this.indexingProgress.progress++;
2019-01-14 00:38:39 +08:00
this.indexingProgress.time.current = Date.now();
for (let i = 0; i < scanned.directories.length; i++) {
this.directoriesToIndex.push(path.join(scanned.directories[i].path, scanned.directories[i].name));
}
if (createThumbnails) {
for (let i = 0; i < scanned.media.length; i++) {
try {
const media = scanned.media[i];
const mPath = path.join(ProjectPath.ImageFolder, media.directory.path, media.directory.name, media.name);
const thPath = path.join(ProjectPath.ThumbnailFolder,
ThumbnailGeneratorMWs.generateThumbnailName(mPath, Config.Client.Thumbnail.thumbnailSizes[0]));
if (fs.existsSync(thPath)) { // skip existing thumbnails
continue;
}
await ThumbnailWorker.render(<RendererInput>{
type: MediaDTO.isVideo(media) ? ThumbnailSourceType.Video : ThumbnailSourceType.Image,
mediaPath: mPath,
size: Config.Client.Thumbnail.thumbnailSizes[0],
thPath: thPath,
makeSquare: false,
qualityPriority: Config.Server.thumbnail.qualityPriority
}, Config.Server.thumbnail.processingLibrary);
} catch (e) {
console.error(e);
Logger.error(LOG_TAG, 'Error during indexing job: ' + e.toString());
}
}
}
process.nextTick(() => {
this.indexNewDirectory(createThumbnails).catch(console.error);
});
};
startIndexing(createThumbnails: boolean = false): void {
if (this.directoriesToIndex.length === 0 && this.enabled === false) {
Logger.info(LOG_TAG, 'Starting indexing');
this.indexingProgress = {
2019-07-28 04:56:12 +08:00
progress: 0,
2019-01-14 00:38:39 +08:00
left: 0,
2019-07-28 04:56:12 +08:00
comment: '',
2019-01-14 00:38:39 +08:00
time: {
start: Date.now(),
current: Date.now()
}
};
this.directoriesToIndex.push('/');
this.enabled = true;
this.indexNewDirectory(createThumbnails).catch(console.error);
} else {
Logger.info(LOG_TAG, 'Already indexing..');
}
}
2019-07-28 04:56:12 +08:00
getProgress(): TaskProgressDTO {
2019-01-14 00:38:39 +08:00
return this.indexingProgress;
}
cancelIndexing(): void {
Logger.info(LOG_TAG, 'Canceling indexing');
this.directoriesToIndex = [];
this.indexingProgress = null;
this.enabled = false;
if (global.gc) {
global.gc();
}
}
async reset(): Promise<void> {
Logger.info(LOG_TAG, 'Resetting DB');
this.directoriesToIndex = [];
this.indexingProgress = null;
this.enabled = false;
const connection = await SQLConnection.getConnection();
return connection
.getRepository(DirectoryEntity)
.createQueryBuilder('directory')
.delete()
.execute().then(() => {
});
}
}