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

82 lines
2.5 KiB
TypeScript
Raw Normal View History

2018-05-04 06:23:48 +08:00
import {IIndexingManager} from '../interfaces/IIndexingManager';
import {IndexingProgressDTO} from '../../../common/entities/settings/IndexingProgressDTO';
import {ObjectManagerRepository} from '../ObjectManagerRepository';
import {ISQLGalleryManager} from './IGalleryManager';
import * as path from 'path';
import {SQLConnection} from './SQLConnection';
import {DirectoryEntity} from './enitites/DirectoryEntity';
import {Logger} from '../../Logger';
2017-07-26 03:09:37 +08:00
2018-05-04 06:23:48 +08:00
const LOG_TAG = '[IndexingManager]';
2017-07-26 03:09:37 +08:00
export class IndexingManager implements IIndexingManager {
directoriesToIndex: string[] = [];
indexingProgress = null;
enabled = false;
private indexNewDirectory = async () => {
if (this.directoriesToIndex.length == 0) {
this.indexingProgress = null;
if (global.gc) {
global.gc();
}
return;
}
const directory = this.directoriesToIndex.shift();
this.indexingProgress.current = directory;
this.indexingProgress.left = this.directoriesToIndex.length;
const scanned = await (<ISQLGalleryManager>ObjectManagerRepository.getInstance().GalleryManager).indexDirectory(directory);
if (this.enabled == false) {
return;
}
this.indexingProgress.indexed++;
for (let i = 0; i < scanned.directories.length; i++) {
2018-05-04 06:23:48 +08:00
this.directoriesToIndex.push(path.join(scanned.directories[i].path, scanned.directories[i].name));
2017-07-26 03:09:37 +08:00
}
process.nextTick(this.indexNewDirectory);
};
startIndexing(): void {
if (this.directoriesToIndex.length == 0 && this.enabled == false) {
2018-05-04 06:23:48 +08:00
Logger.info(LOG_TAG, 'Starting indexing');
2017-07-26 03:09:37 +08:00
this.indexingProgress = <IndexingProgressDTO>{
indexed: 0,
left: 0,
2018-05-04 06:23:48 +08:00
current: ''
2017-07-26 03:09:37 +08:00
};
2018-05-04 06:23:48 +08:00
this.directoriesToIndex.push('/');
2017-07-26 03:09:37 +08:00
this.enabled = true;
this.indexNewDirectory();
} else {
2018-05-04 06:23:48 +08:00
Logger.info(LOG_TAG, 'Already indexing..');
2017-07-26 03:09:37 +08:00
}
}
getProgress(): IndexingProgressDTO {
return this.indexingProgress;
}
cancelIndexing(): void {
2018-05-04 06:23:48 +08:00
Logger.info(LOG_TAG, 'Canceling indexing');
2017-07-26 03:09:37 +08:00
this.directoriesToIndex = [];
this.indexingProgress = null;
this.enabled = false;
if (global.gc) {
global.gc();
}
}
async reset(): Promise<void> {
2018-05-04 06:23:48 +08:00
Logger.info(LOG_TAG, 'Resetting DB');
2017-07-26 03:09:37 +08:00
this.directoriesToIndex = [];
this.indexingProgress = null;
this.enabled = false;
const connection = await SQLConnection.getConnection();
return connection
.getRepository(DirectoryEntity)
2018-05-04 06:23:48 +08:00
.createQueryBuilder('directory')
2017-07-26 03:09:37 +08:00
.delete()
2018-05-04 06:23:48 +08:00
.execute().then(() => {
});
2017-07-26 03:09:37 +08:00
}
}