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';
|
2018-12-02 19:22:05 +08:00
|
|
|
import * as fs from 'fs';
|
2018-05-04 06:23:48 +08:00
|
|
|
import {SQLConnection} from './SQLConnection';
|
|
|
|
import {DirectoryEntity} from './enitites/DirectoryEntity';
|
|
|
|
import {Logger} from '../../Logger';
|
2018-11-24 20:08:34 +08:00
|
|
|
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';
|
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[] = [];
|
2019-01-07 06:15:52 +08:00
|
|
|
indexingProgress: IndexingProgressDTO = null;
|
2017-07-26 03:09:37 +08:00
|
|
|
enabled = false;
|
2018-11-24 20:08:34 +08:00
|
|
|
private indexNewDirectory = async (createThumbnails: boolean = false) => {
|
2018-05-13 00:19:51 +08:00
|
|
|
if (this.directoriesToIndex.length === 0) {
|
2017-07-26 03:09:37 +08:00
|
|
|
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);
|
2018-05-13 00:19:51 +08:00
|
|
|
if (this.enabled === false) {
|
2017-07-26 03:09:37 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.indexingProgress.indexed++;
|
2019-01-07 06:15:52 +08:00
|
|
|
this.indexingProgress.time.current = Date.now();
|
2017-07-26 03:09:37 +08:00
|
|
|
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
|
|
|
}
|
2018-11-24 20:08:34 +08:00
|
|
|
if (createThumbnails) {
|
|
|
|
for (let i = 0; i < scanned.media.length; i++) {
|
2018-12-04 20:21:21 +08:00
|
|
|
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());
|
2018-12-02 19:22:05 +08:00
|
|
|
}
|
2018-11-24 20:08:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
process.nextTick(() => {
|
|
|
|
this.indexNewDirectory(createThumbnails);
|
|
|
|
});
|
2017-07-26 03:09:37 +08:00
|
|
|
};
|
|
|
|
|
2018-11-24 20:08:34 +08:00
|
|
|
startIndexing(createThumbnails: boolean = false): void {
|
2018-05-13 00:19:51 +08:00
|
|
|
if (this.directoriesToIndex.length === 0 && this.enabled === false) {
|
2018-05-04 06:23:48 +08:00
|
|
|
Logger.info(LOG_TAG, 'Starting indexing');
|
2019-01-07 06:15:52 +08:00
|
|
|
this.indexingProgress = {
|
2017-07-26 03:09:37 +08:00
|
|
|
indexed: 0,
|
|
|
|
left: 0,
|
2019-01-07 06:15:52 +08:00
|
|
|
current: '',
|
|
|
|
time: {
|
|
|
|
start: Date.now(),
|
|
|
|
current: Date.now()
|
|
|
|
}
|
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;
|
2018-11-24 20:08:34 +08:00
|
|
|
this.indexNewDirectory(createThumbnails);
|
2017-07-26 03:09:37 +08:00
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|