import {IGalleryManager, RandomQuery} from '../interfaces/IGalleryManager'; import {DirectoryDTO} from '../../../common/entities/DirectoryDTO'; import * as path from 'path'; import * as fs from 'fs'; import {DirectoryEntity} from './enitites/DirectoryEntity'; import {SQLConnection} from './SQLConnection'; import {PhotoEntity} from './enitites/PhotoEntity'; import {ProjectPath} from '../../ProjectPath'; import {Config} from '../../../common/config/private/Config'; import {ISQLGalleryManager} from './IGalleryManager'; import {DatabaseType, ReIndexingSensitivity} from '../../../common/config/private/IPrivateConfig'; import {PhotoDTO} from '../../../common/entities/PhotoDTO'; import {OrientationType} from '../../../common/entities/RandomQueryDTO'; import {Brackets, Connection} from 'typeorm'; import {MediaEntity} from './enitites/MediaEntity'; import {VideoEntity} from './enitites/VideoEntity'; import {DiskMangerWorker} from '../threading/DiskMangerWorker'; import {Logger} from '../../Logger'; import {FaceRegionEntry} from './enitites/FaceRegionEntry'; import {ObjectManagerRepository} from '../ObjectManagerRepository'; const LOG_TAG = '[GalleryManager]'; export class GalleryManager implements IGalleryManager, ISQLGalleryManager { public async listDirectory(relativeDirectoryName: string, knownLastModified?: number, knownLastScanned?: number): Promise { relativeDirectoryName = DiskMangerWorker.normalizeDirPath(relativeDirectoryName); const directoryName = path.basename(relativeDirectoryName); const directoryParent = path.join(path.dirname(relativeDirectoryName), path.sep); const connection = await SQLConnection.getConnection(); const stat = fs.statSync(path.join(ProjectPath.ImageFolder, relativeDirectoryName)); const lastModified = DiskMangerWorker.calcLastModified(stat); const dir = await this.selectParentDir(connection, directoryName, directoryParent); if (dir && dir.lastScanned != null) { // If it seems that the content did not changed, do not work on it if (knownLastModified && knownLastScanned && lastModified === knownLastModified && dir.lastScanned === knownLastScanned) { if (Config.Server.indexing.reIndexingSensitivity === ReIndexingSensitivity.low) { return null; } if (Date.now() - dir.lastScanned <= Config.Server.indexing.cachedFolderTimeout && Config.Server.indexing.reIndexingSensitivity === ReIndexingSensitivity.medium) { return null; } } if (dir.lastModified !== lastModified) { Logger.silly(LOG_TAG, 'Reindexing reason: lastModified mismatch: known: ' + dir.lastModified + ', current:' + lastModified); return ObjectManagerRepository.getInstance().IndexingManager.indexDirectory(relativeDirectoryName); } // not indexed since a while, index it in a lazy manner if ((Date.now() - dir.lastScanned > Config.Server.indexing.cachedFolderTimeout && Config.Server.indexing.reIndexingSensitivity >= ReIndexingSensitivity.medium) || Config.Server.indexing.reIndexingSensitivity >= ReIndexingSensitivity.high) { // on the fly reindexing Logger.silly(LOG_TAG, 'lazy reindexing reason: cache timeout: lastScanned: ' + (Date.now() - dir.lastScanned) + ', cachedFolderTimeout:' + Config.Server.indexing.cachedFolderTimeout); ObjectManagerRepository.getInstance().IndexingManager.indexDirectory(relativeDirectoryName).catch((err) => { console.error(err); }); } await this.fillParentDir(connection, dir); return dir; } // never scanned (deep indexed), do it and return with it Logger.silly(LOG_TAG, 'Reindexing reason: never scanned'); return ObjectManagerRepository.getInstance().IndexingManager.indexDirectory(relativeDirectoryName); } public async getRandomPhoto(queryFilter: RandomQuery): Promise { const connection = await SQLConnection.getConnection(); const photosRepository = connection.getRepository(PhotoEntity); const query = photosRepository.createQueryBuilder('photo'); query.innerJoinAndSelect('photo.directory', 'directory'); if (queryFilter.directory) { const directoryName = path.basename(queryFilter.directory); const directoryParent = path.join(path.dirname(queryFilter.directory), path.sep); query.where(new Brackets(qb => { qb.where('directory.name = :name AND directory.path = :path', { name: directoryName, path: directoryParent }); if (queryFilter.recursive) { qb.orWhere('directory.name LIKE :text COLLATE utf8_general_ci', {text: '%' + queryFilter.directory + '%'}); } })); } if (queryFilter.fromDate) { query.andWhere('photo.metadata.creationDate >= :fromDate', { fromDate: queryFilter.fromDate.getTime() }); } if (queryFilter.toDate) { query.andWhere('photo.metadata.creationDate <= :toDate', { toDate: queryFilter.toDate.getTime() }); } if (queryFilter.minResolution) { query.andWhere('photo.metadata.size.width * photo.metadata.size.height >= :minRes', { minRes: queryFilter.minResolution * 1000 * 1000 }); } if (queryFilter.maxResolution) { query.andWhere('photo.metadata.size.width * photo.metadata.size.height <= :maxRes', { maxRes: queryFilter.maxResolution * 1000 * 1000 }); } if (queryFilter.orientation === OrientationType.landscape) { query.andWhere('photo.metadata.size.width >= photo.metadata.size.height'); } if (queryFilter.orientation === OrientationType.portrait) { query.andWhere('photo.metadata.size.width <= photo.metadata.size.height'); } if (Config.Server.database.type === DatabaseType.mysql) { return await query.groupBy('RAND(), photo.id').limit(1).getOne(); } return await query.groupBy('RANDOM()').limit(1).getOne(); } async countDirectories(): Promise { const connection = await SQLConnection.getConnection(); return await connection.getRepository(DirectoryEntity) .createQueryBuilder('directory') .getCount(); } async countMediaSize(): Promise { const connection = await SQLConnection.getConnection(); const {sum} = await connection.getRepository(MediaEntity) .createQueryBuilder('media') .select('SUM(media.metadata.fileSize)', 'sum') .getRawOne(); return sum; } async countPhotos(): Promise { const connection = await SQLConnection.getConnection(); return await connection.getRepository(PhotoEntity) .createQueryBuilder('directory') .getCount(); } async countVideos(): Promise { const connection = await SQLConnection.getConnection(); return await connection.getRepository(VideoEntity) .createQueryBuilder('directory') .getCount(); } protected async selectParentDir(connection: Connection, directoryName: string, directoryParent: string): Promise { const query = connection .getRepository(DirectoryEntity) .createQueryBuilder('directory') .where('directory.name = :name AND directory.path = :path', { name: directoryName, path: directoryParent }) .leftJoinAndSelect('directory.directories', 'directories') .leftJoinAndSelect('directory.media', 'media'); if (Config.Client.MetaFile.enabled === true) { query.leftJoinAndSelect('directory.metaFile', 'metaFile'); } return await query.getOne(); } protected async fillParentDir(connection: Connection, dir: DirectoryEntity): Promise { if (dir.media) { const indexedFaces = await connection.getRepository(FaceRegionEntry) .createQueryBuilder('face') .leftJoinAndSelect('face.media', 'media') .where('media.directory = :directory', { directory: dir.id }) .leftJoinAndSelect('face.person', 'person') .select(['face.id', 'face.box.x', 'face.box.y', 'face.box.width', 'face.box.height', 'media.id', 'person.name', 'person.id']) .getMany(); for (let i = 0; i < dir.media.length; i++) { dir.media[i].directory = dir; dir.media[i].readyThumbnails = []; dir.media[i].readyIcon = false; (dir.media[i]).metadata.faces = indexedFaces .filter(fe => fe.media.id === dir.media[i].id) .map(f => ({box: f.box, name: f.person.name})); } } if (dir.directories) { for (let i = 0; i < dir.directories.length; i++) { dir.directories[i].media = await connection .getRepository(MediaEntity) .createQueryBuilder('media') .where('media.directory = :dir', { dir: dir.directories[i].id }) .orderBy('media.metadata.creationDate', 'ASC') .limit(Config.Server.indexing.folderPreviewSize) .getMany(); dir.directories[i].isPartial = true; for (let j = 0; j < dir.directories[i].media.length; j++) { dir.directories[i].media[j].directory = dir.directories[i]; dir.directories[i].media[j].readyThumbnails = []; dir.directories[i].media[j].readyIcon = false; } } } } }