2018-10-22 06:24:17 +08:00
|
|
|
import {IGalleryManager, RandomQuery} from '../interfaces/IGalleryManager';
|
2018-03-31 03:30:30 +08:00
|
|
|
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';
|
2019-01-14 00:38:39 +08:00
|
|
|
import {PhotoEntity} from './enitites/PhotoEntity';
|
2018-03-31 03:30:30 +08:00
|
|
|
import {ProjectPath} from '../../ProjectPath';
|
|
|
|
import {Config} from '../../../common/config/private/Config';
|
|
|
|
import {ISQLGalleryManager} from './IGalleryManager';
|
2018-11-21 23:07:37 +08:00
|
|
|
import {DatabaseType, ReIndexingSensitivity} from '../../../common/config/private/IPrivateConfig';
|
2019-01-14 00:38:39 +08:00
|
|
|
import {PhotoDTO} from '../../../common/entities/PhotoDTO';
|
2018-10-22 06:24:17 +08:00
|
|
|
import {OrientationType} from '../../../common/entities/RandomQueryDTO';
|
2019-01-14 00:38:39 +08:00
|
|
|
import {Brackets, Connection} from 'typeorm';
|
2018-11-18 02:32:31 +08:00
|
|
|
import {MediaEntity} from './enitites/MediaEntity';
|
|
|
|
import {VideoEntity} from './enitites/VideoEntity';
|
2019-01-07 06:15:52 +08:00
|
|
|
import {DiskMangerWorker} from '../threading/DiskMangerWorker';
|
|
|
|
import {Logger} from '../../Logger';
|
2019-01-12 23:41:45 +08:00
|
|
|
import {FaceRegionEntry} from './enitites/FaceRegionEntry';
|
2019-01-14 00:38:39 +08:00
|
|
|
import {ObjectManagerRepository} from '../ObjectManagerRepository';
|
2019-01-18 07:26:20 +08:00
|
|
|
import {DuplicatesDTO} from '../../../common/entities/DuplicatesDTO';
|
2019-01-07 06:15:52 +08:00
|
|
|
|
|
|
|
const LOG_TAG = '[GalleryManager]';
|
2016-12-28 03:55:51 +08:00
|
|
|
|
2017-07-26 03:09:37 +08:00
|
|
|
export class GalleryManager implements IGalleryManager, ISQLGalleryManager {
|
2016-12-28 03:55:51 +08:00
|
|
|
|
|
|
|
|
2017-07-20 02:47:09 +08:00
|
|
|
public async listDirectory(relativeDirectoryName: string,
|
|
|
|
knownLastModified?: number,
|
2017-07-21 05:00:49 +08:00
|
|
|
knownLastScanned?: number): Promise<DirectoryDTO> {
|
2018-12-09 18:37:12 +08:00
|
|
|
|
2019-01-07 06:15:52 +08:00
|
|
|
relativeDirectoryName = DiskMangerWorker.normalizeDirPath(relativeDirectoryName);
|
2017-07-04 01:17:49 +08:00
|
|
|
const directoryName = path.basename(relativeDirectoryName);
|
|
|
|
const directoryParent = path.join(path.dirname(relativeDirectoryName), path.sep);
|
2017-07-21 05:37:10 +08:00
|
|
|
const connection = await SQLConnection.getConnection();
|
2017-07-20 02:47:09 +08:00
|
|
|
const stat = fs.statSync(path.join(ProjectPath.ImageFolder, relativeDirectoryName));
|
2019-01-07 06:15:52 +08:00
|
|
|
const lastModified = DiskMangerWorker.calcLastModified(stat);
|
2017-07-04 01:17:49 +08:00
|
|
|
|
2017-07-20 02:47:09 +08:00
|
|
|
|
2018-12-09 18:37:12 +08:00
|
|
|
const dir = await this.selectParentDir(connection, directoryName, directoryParent);
|
2017-12-20 07:20:37 +08:00
|
|
|
if (dir && dir.lastScanned != null) {
|
2018-05-13 00:19:51 +08:00
|
|
|
// If it seems that the content did not changed, do not work on it
|
2017-07-28 05:10:16 +08:00
|
|
|
if (knownLastModified && knownLastScanned
|
2018-05-13 00:19:51 +08:00
|
|
|
&& lastModified === knownLastModified &&
|
|
|
|
dir.lastScanned === knownLastScanned) {
|
|
|
|
if (Config.Server.indexing.reIndexingSensitivity === ReIndexingSensitivity.low) {
|
2017-07-28 05:10:16 +08:00
|
|
|
return null;
|
|
|
|
}
|
2018-12-09 19:02:02 +08:00
|
|
|
if (Date.now() - dir.lastScanned <= Config.Server.indexing.cachedFolderTimeout &&
|
2018-05-13 00:19:51 +08:00
|
|
|
Config.Server.indexing.reIndexingSensitivity === ReIndexingSensitivity.medium) {
|
2017-07-21 05:00:49 +08:00
|
|
|
return null;
|
2017-07-20 02:47:09 +08:00
|
|
|
}
|
|
|
|
}
|
2017-07-18 05:12:12 +08:00
|
|
|
|
|
|
|
|
2018-05-13 00:19:51 +08:00
|
|
|
if (dir.lastModified !== lastModified) {
|
2019-01-07 06:15:52 +08:00
|
|
|
Logger.silly(LOG_TAG, 'Reindexing reason: lastModified mismatch: known: '
|
|
|
|
+ dir.lastModified + ', current:' + lastModified);
|
2019-01-14 00:38:39 +08:00
|
|
|
return ObjectManagerRepository.getInstance().IndexingManager.indexDirectory(relativeDirectoryName);
|
2017-07-18 05:12:12 +08:00
|
|
|
}
|
2017-07-04 01:17:49 +08:00
|
|
|
|
2018-12-22 07:09:07 +08:00
|
|
|
|
2018-05-13 00:19:51 +08:00
|
|
|
// not indexed since a while, index it in a lazy manner
|
2017-07-28 05:10:16 +08:00
|
|
|
if ((Date.now() - dir.lastScanned > Config.Server.indexing.cachedFolderTimeout &&
|
2018-03-31 03:30:30 +08:00
|
|
|
Config.Server.indexing.reIndexingSensitivity >= ReIndexingSensitivity.medium) ||
|
2017-07-28 05:10:16 +08:00
|
|
|
Config.Server.indexing.reIndexingSensitivity >= ReIndexingSensitivity.high) {
|
2018-05-13 00:19:51 +08:00
|
|
|
// on the fly reindexing
|
2018-12-22 07:09:07 +08:00
|
|
|
|
2019-01-07 06:15:52 +08:00
|
|
|
Logger.silly(LOG_TAG, 'lazy reindexing reason: cache timeout: lastScanned: '
|
|
|
|
+ (Date.now() - dir.lastScanned) + ', cachedFolderTimeout:' + Config.Server.indexing.cachedFolderTimeout);
|
2019-01-14 00:38:39 +08:00
|
|
|
ObjectManagerRepository.getInstance().IndexingManager.indexDirectory(relativeDirectoryName).catch((err) => {
|
2017-07-20 02:47:09 +08:00
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
}
|
2018-12-22 07:09:07 +08:00
|
|
|
await this.fillParentDir(connection, dir);
|
2017-07-04 01:17:49 +08:00
|
|
|
return dir;
|
|
|
|
}
|
2017-12-20 07:20:37 +08:00
|
|
|
|
2018-05-13 00:19:51 +08:00
|
|
|
// never scanned (deep indexed), do it and return with it
|
2019-01-07 06:15:52 +08:00
|
|
|
Logger.silly(LOG_TAG, 'Reindexing reason: never scanned');
|
2019-01-14 00:38:39 +08:00
|
|
|
return ObjectManagerRepository.getInstance().IndexingManager.indexDirectory(relativeDirectoryName);
|
2017-07-04 01:17:49 +08:00
|
|
|
|
|
|
|
|
2017-12-18 10:34:07 +08:00
|
|
|
}
|
2017-07-18 05:12:12 +08:00
|
|
|
|
2018-11-24 18:50:11 +08:00
|
|
|
public async getRandomPhoto(queryFilter: RandomQuery): Promise<PhotoDTO> {
|
|
|
|
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();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-10 06:25:39 +08:00
|
|
|
async countDirectories(): Promise<number> {
|
|
|
|
const connection = await SQLConnection.getConnection();
|
|
|
|
return await connection.getRepository(DirectoryEntity)
|
|
|
|
.createQueryBuilder('directory')
|
|
|
|
.getCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
async countMediaSize(): Promise<number> {
|
|
|
|
const connection = await SQLConnection.getConnection();
|
2018-12-22 07:09:07 +08:00
|
|
|
const {sum} = await connection.getRepository(MediaEntity)
|
2018-12-10 06:25:39 +08:00
|
|
|
.createQueryBuilder('media')
|
|
|
|
.select('SUM(media.metadata.fileSize)', 'sum')
|
|
|
|
.getRawOne();
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
async countPhotos(): Promise<number> {
|
|
|
|
const connection = await SQLConnection.getConnection();
|
|
|
|
return await connection.getRepository(PhotoEntity)
|
|
|
|
.createQueryBuilder('directory')
|
|
|
|
.getCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
async countVideos(): Promise<number> {
|
|
|
|
const connection = await SQLConnection.getConnection();
|
|
|
|
return await connection.getRepository(VideoEntity)
|
|
|
|
.createQueryBuilder('directory')
|
|
|
|
.getCount();
|
|
|
|
}
|
|
|
|
|
2019-01-18 03:17:17 +08:00
|
|
|
public async getPossibleDuplicates() {
|
|
|
|
const connection = await SQLConnection.getConnection();
|
|
|
|
const mediaRepository = connection.getRepository(MediaEntity);
|
|
|
|
|
2019-01-18 07:26:20 +08:00
|
|
|
let duplicates = await mediaRepository.createQueryBuilder('media')
|
2019-01-18 03:17:17 +08:00
|
|
|
.innerJoin(query => query.from(MediaEntity, 'innerMedia')
|
|
|
|
.select(['innerMedia.name as name', 'innerMedia.metadata.fileSize as fileSize', 'count(*)'])
|
|
|
|
.groupBy('innerMedia.name, innerMedia.metadata.fileSize')
|
|
|
|
.having('count(*)>1'),
|
|
|
|
'innerMedia',
|
|
|
|
'media.name=innerMedia.name AND media.metadata.fileSize = innerMedia.fileSize')
|
2019-01-19 07:18:20 +08:00
|
|
|
.innerJoinAndSelect('media.directory', 'directory')
|
|
|
|
.orderBy('media.name, media.metadata.fileSize')
|
|
|
|
.limit(Config.Server.duplicates.listingLimit).getMany();
|
|
|
|
|
2019-01-18 07:26:20 +08:00
|
|
|
|
|
|
|
const duplicateParis: DuplicatesDTO[] = [];
|
2019-01-19 07:18:20 +08:00
|
|
|
const processDuplicates = (duplicateList: MediaEntity[],
|
|
|
|
equalFn: (a: MediaEntity, b: MediaEntity) => boolean,
|
|
|
|
checkDuplicates: boolean = false) => {
|
|
|
|
let i = duplicateList.length - 1;
|
|
|
|
while (i >= 0) {
|
|
|
|
const list = [duplicateList[i]];
|
|
|
|
let j = i - 1;
|
|
|
|
while (j >= 0 && equalFn(duplicateList[i], duplicateList[j])) {
|
|
|
|
list.push(duplicateList[j]);
|
|
|
|
j--;
|
|
|
|
}
|
|
|
|
i = j;
|
|
|
|
// if we cut the select list with the SQL LIMIT, filter unpaired media
|
|
|
|
if (list.length < 2) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (checkDuplicates) {
|
|
|
|
// ad to group if one already existed
|
|
|
|
const foundDuplicates = duplicateParis.find(dp =>
|
|
|
|
!!dp.media.find(m =>
|
|
|
|
!!list.find(lm => lm.id === m.id)));
|
|
|
|
if (foundDuplicates) {
|
|
|
|
list.forEach(lm => {
|
|
|
|
if (!!foundDuplicates.media.find(m => m.id === lm.id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
foundDuplicates.media.push(lm);
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
duplicateParis.push({media: list});
|
2019-01-18 07:26:20 +08:00
|
|
|
}
|
2019-01-19 07:18:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
processDuplicates(duplicates,
|
|
|
|
(a, b) => a.name === b.name &&
|
|
|
|
a.metadata.fileSize === b.metadata.fileSize);
|
2019-01-18 07:26:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
duplicates = await mediaRepository.createQueryBuilder('media')
|
|
|
|
.innerJoin(query => query.from(MediaEntity, 'innerMedia')
|
|
|
|
.select(['innerMedia.metadata.creationDate as creationDate', 'innerMedia.metadata.fileSize as fileSize', 'count(*)'])
|
2019-01-19 07:18:20 +08:00
|
|
|
.groupBy('innerMedia.metadata.creationDate, innerMedia.metadata.fileSize')
|
2019-01-18 07:26:20 +08:00
|
|
|
.having('count(*)>1'),
|
|
|
|
'innerMedia',
|
|
|
|
'media.metadata.creationDate=innerMedia.creationDate AND media.metadata.fileSize = innerMedia.fileSize')
|
2019-01-19 07:18:20 +08:00
|
|
|
.innerJoinAndSelect('media.directory', 'directory')
|
|
|
|
.orderBy('media.metadata.creationDate, media.metadata.fileSize')
|
|
|
|
.limit(Config.Server.duplicates.listingLimit).getMany();
|
2019-01-18 07:26:20 +08:00
|
|
|
|
2019-01-19 07:18:20 +08:00
|
|
|
processDuplicates(duplicates,
|
|
|
|
(a, b) => a.metadata.creationDate === b.metadata.creationDate &&
|
|
|
|
a.metadata.fileSize === b.metadata.fileSize, true);
|
2019-01-18 07:26:20 +08:00
|
|
|
|
|
|
|
return duplicateParis;
|
2019-01-18 03:17:17 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-01-14 00:38:39 +08:00
|
|
|
protected async selectParentDir(connection: Connection, directoryName: string, directoryParent: string): Promise<DirectoryEntity> {
|
|
|
|
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<void> {
|
|
|
|
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')
|
2019-01-18 03:17:17 +08:00
|
|
|
.select(['face.id', 'face.box.x',
|
2019-01-14 00:38:39 +08:00
|
|
|
'face.box.y', 'face.box.width', 'face.box.height',
|
2019-01-18 03:17:17 +08:00
|
|
|
'media.id', 'person.name', 'person.id'])
|
2019-01-14 00:38:39 +08:00
|
|
|
.getMany();
|
|
|
|
for (let i = 0; i < dir.media.length; i++) {
|
|
|
|
dir.media[i].directory = dir;
|
|
|
|
dir.media[i].readyThumbnails = [];
|
|
|
|
dir.media[i].readyIcon = false;
|
|
|
|
(<PhotoDTO>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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|