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

230 lines
8.7 KiB
TypeScript
Raw Normal View History

2018-03-31 03:30:30 +08:00
import {IGalleryManager} 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 {DiskManager} from '../DiskManger';
import {PhotoEntity} from './enitites/PhotoEntity';
import {Utils} from '../../../common/Utils';
import {ProjectPath} from '../../ProjectPath';
import {Config} from '../../../common/config/private/Config';
import {ISQLGalleryManager} from './IGalleryManager';
import {ReIndexingSensitivity} from '../../../common/config/private/IPrivateConfig';
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,
knownLastScanned?: number): Promise<DirectoryDTO> {
2018-03-31 03:30:30 +08:00
relativeDirectoryName = path.normalize(path.join('.' + path.sep, relativeDirectoryName));
2017-07-04 01:17:49 +08:00
const directoryName = path.basename(relativeDirectoryName);
const directoryParent = path.join(path.dirname(relativeDirectoryName), path.sep);
const connection = await SQLConnection.getConnection();
2017-07-20 02:47:09 +08:00
const stat = fs.statSync(path.join(ProjectPath.ImageFolder, relativeDirectoryName));
const lastModified = Math.max(stat.ctime.getTime(), stat.mtime.getTime());
const dir = await connection
2017-07-04 01:17:49 +08:00
.getRepository(DirectoryEntity)
2018-03-31 03:30:30 +08:00
.createQueryBuilder('directory')
.where('directory.name = :name AND directory.path = :path', {
2017-07-04 01:17:49 +08:00
name: directoryName,
path: directoryParent
})
2018-03-31 03:30:30 +08:00
.leftJoinAndSelect('directory.directories', 'directories')
.leftJoinAndSelect('directory.photos', 'photos')
2017-07-04 01:17:49 +08:00
.getOne();
2017-07-20 02:47:09 +08:00
if (dir && dir.lastScanned != null) {
// 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
&& lastModified === knownLastModified &&
dir.lastScanned === knownLastScanned) {
2017-07-28 05:10:16 +08:00
if (Config.Server.indexing.reIndexingSensitivity === ReIndexingSensitivity.low) {
2017-07-28 05:10:16 +08:00
return null;
}
if (Date.now() - knownLastScanned <= Config.Server.indexing.cachedFolderTimeout &&
Config.Server.indexing.reIndexingSensitivity === ReIndexingSensitivity.medium) {
return null;
2017-07-20 02:47:09 +08:00
}
}
2017-07-04 01:17:49 +08:00
if (dir.photos) {
for (let i = 0; i < dir.photos.length; i++) {
dir.photos[i].directory = dir;
dir.photos[i].readyThumbnails = [];
dir.photos[i].readyIcon = false;
}
}
2017-07-18 05:12:12 +08:00
if (dir.directories) {
for (let i = 0; i < dir.directories.length; i++) {
dir.directories[i].photos = await connection
.getRepository(PhotoEntity)
2018-03-31 03:30:30 +08:00
.createQueryBuilder('photo')
.where('photo.directory = :dir', {
2017-07-18 05:12:12 +08:00
dir: dir.directories[i].id
})
2018-03-31 03:30:30 +08:00
.orderBy('photo.metadata.creationDate', 'ASC')
2017-10-20 00:08:07 +08:00
.limit(Config.Server.indexing.folderPreviewSize)
2017-07-18 05:12:12 +08:00
.getMany();
2017-07-22 01:14:22 +08:00
dir.directories[i].isPartial = true;
2017-07-18 05:12:12 +08:00
for (let j = 0; j < dir.directories[i].photos.length; j++) {
dir.directories[i].photos[j].directory = dir.directories[i];
dir.directories[i].photos[j].readyThumbnails = [];
dir.directories[i].photos[j].readyIcon = false;
}
}
}
if (dir.lastModified !== lastModified) {
2017-07-18 05:12:12 +08:00
return this.indexDirectory(relativeDirectoryName);
}
2017-07-04 01:17:49 +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) {
// on the fly reindexing
2017-07-20 02:47:09 +08:00
this.indexDirectory(relativeDirectoryName).catch((err) => {
console.error(err);
});
}
2017-07-04 01:17:49 +08:00
return dir;
2016-12-28 03:55:51 +08:00
2017-07-04 01:17:49 +08:00
}
// never scanned (deep indexed), do it and return with it
2017-07-04 01:17:49 +08:00
return this.indexDirectory(relativeDirectoryName);
2016-12-28 03:55:51 +08:00
2017-07-04 01:17:49 +08:00
}
2016-12-28 03:55:51 +08:00
2017-07-18 05:12:12 +08:00
public indexDirectory(relativeDirectoryName): Promise<DirectoryDTO> {
2017-07-04 01:17:49 +08:00
return new Promise(async (resolve, reject) => {
try {
const scannedDirectory = await DiskManager.scanDirectory(relativeDirectoryName);
// returning with the result
2017-07-04 01:17:49 +08:00
scannedDirectory.photos.forEach(p => p.readyThumbnails = []);
resolve(scannedDirectory);
await this.saveToDB(scannedDirectory);
2017-07-04 01:17:49 +08:00
} catch (error) {
console.error(error);
return reject(error);
}
2017-07-04 01:17:49 +08:00
});
}
2017-07-18 05:12:12 +08:00
2017-07-04 01:17:49 +08:00
private async saveToDB(scannedDirectory: DirectoryDTO) {
const connection = await SQLConnection.getConnection();
2017-07-04 01:17:49 +08:00
// saving to db
const directoryRepository = connection.getRepository(DirectoryEntity);
const photosRepository = connection.getRepository(PhotoEntity);
2017-07-04 01:17:49 +08:00
2018-03-31 03:30:30 +08:00
let currentDir = await directoryRepository.createQueryBuilder('directory')
.where('directory.name = :name AND directory.path = :path', {
name: scannedDirectory.name,
path: scannedDirectory.path
}).getOne();
if (!!currentDir) {// Updated parent dir (if it was in the DB previously)
currentDir.lastModified = scannedDirectory.lastModified;
currentDir.lastScanned = scannedDirectory.lastScanned;
currentDir = await directoryRepository.save(currentDir);
} else {
(<DirectoryEntity>scannedDirectory).lastScanned = scannedDirectory.lastScanned;
currentDir = await directoryRepository.save(<DirectoryEntity>scannedDirectory);
}
2017-07-18 05:12:12 +08:00
const childDirectories = await directoryRepository.createQueryBuilder('directory')
2018-03-31 03:30:30 +08:00
.where('directory.parent = :dir', {
dir: currentDir.id
}).getMany();
for (let i = 0; i < scannedDirectory.directories.length; i++) {
// Was this child Dir already indexed before?
let directory: DirectoryEntity = null;
for (let j = 0; j < childDirectories.length; j++) {
if (childDirectories[j].name === scannedDirectory.directories[i].name) {
directory = childDirectories[j];
childDirectories.splice(j, 1);
break;
}
}
2017-07-18 05:12:12 +08:00
if (directory != null) { // update existing directory
if (!directory.parent || !directory.parent.id) { // set parent if not set yet
directory.parent = currentDir;
delete directory.photos;
await directoryRepository.save(directory);
}
} else {
scannedDirectory.directories[i].parent = currentDir;
(<DirectoryEntity>scannedDirectory.directories[i]).lastScanned = null; // new child dir, not fully scanned yet
const d = await directoryRepository.save(<DirectoryEntity>scannedDirectory.directories[i]);
for (let j = 0; j < scannedDirectory.directories[i].photos.length; j++) {
scannedDirectory.directories[i].photos[j].directory = d;
2017-07-04 01:17:49 +08:00
}
await photosRepository.save(scannedDirectory.directories[i].photos);
}
}
// Remove child Dirs that are not anymore in the parent dir
await directoryRepository.remove(childDirectories);
const indexedPhotos = await photosRepository.createQueryBuilder('photo')
2018-03-31 03:30:30 +08:00
.where('photo.directory = :dir', {
dir: currentDir.id
}).getMany();
2017-07-04 01:17:49 +08:00
const photosToSave = [];
for (let i = 0; i < scannedDirectory.photos.length; i++) {
let photo = null;
for (let j = 0; j < indexedPhotos.length; j++) {
if (indexedPhotos[j].name === scannedDirectory.photos[i].name) {
photo = indexedPhotos[j];
indexedPhotos.splice(j, 1);
break;
}
2017-07-04 01:17:49 +08:00
}
if (photo == null) {
scannedDirectory.photos[i].directory = null;
photo = Utils.clone(scannedDirectory.photos[i]);
scannedDirectory.photos[i].directory = scannedDirectory;
photo.directory = currentDir;
}
if (photo.metadata.keywords !== scannedDirectory.photos[i].metadata.keywords ||
photo.metadata.cameraData !== scannedDirectory.photos[i].metadata.cameraData ||
photo.metadata.positionData !== scannedDirectory.photos[i].metadata.positionData ||
photo.metadata.size !== scannedDirectory.photos[i].metadata.size) {
photo.metadata.keywords = scannedDirectory.photos[i].metadata.keywords;
photo.metadata.cameraData = scannedDirectory.photos[i].metadata.cameraData;
photo.metadata.positionData = scannedDirectory.photos[i].metadata.positionData;
photo.metadata.size = scannedDirectory.photos[i].metadata.size;
photosToSave.push(photo);
}
}
await photosRepository.save(photosToSave);
await photosRepository.remove(indexedPhotos);
2017-07-04 01:17:49 +08:00
}
}