2016-12-28 03:55:51 +08:00
|
|
|
import {IGalleryManager} from "../interfaces/IGalleryManager";
|
2017-07-21 05:00:49 +08:00
|
|
|
import {DirectoryDTO} from "../../../common/entities/DirectoryDTO";
|
2016-12-28 03:55:51 +08:00
|
|
|
import * as path from "path";
|
2017-07-18 05:12:12 +08:00
|
|
|
import * as fs from "fs";
|
2016-12-28 19:30:26 +08:00
|
|
|
import {DirectoryEntity} from "./enitites/DirectoryEntity";
|
2017-07-21 05:37:10 +08:00
|
|
|
import {SQLConnection} from "./SQLConnection";
|
2016-12-28 03:55:51 +08:00
|
|
|
import {DiskManager} from "../DiskManger";
|
2017-10-20 00:08:07 +08:00
|
|
|
import {PhotoEntity} from "./enitites/PhotoEntity";
|
2016-12-28 03:55:51 +08:00
|
|
|
import {Utils} from "../../../common/Utils";
|
2016-12-28 21:26:19 +08:00
|
|
|
import {ProjectPath} from "../../ProjectPath";
|
2017-07-18 05:12:12 +08:00
|
|
|
import {Config} from "../../../common/config/private/Config";
|
2017-07-26 03:09:37 +08:00
|
|
|
import {ISQLGalleryManager} from "./IGalleryManager";
|
2017-07-28 05:10:16 +08:00
|
|
|
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,
|
2017-07-21 05:00:49 +08:00
|
|
|
knownLastScanned?: number): Promise<DirectoryDTO> {
|
2017-07-04 01:17:49 +08:00
|
|
|
relativeDirectoryName = path.normalize(path.join("." + path.sep, relativeDirectoryName));
|
|
|
|
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));
|
|
|
|
const lastModified = Math.max(stat.ctime.getTime(), stat.mtime.getTime());
|
2017-07-04 01:17:49 +08:00
|
|
|
let dir = await connection
|
|
|
|
.getRepository(DirectoryEntity)
|
|
|
|
.createQueryBuilder("directory")
|
|
|
|
.where("directory.name = :name AND directory.path = :path", {
|
|
|
|
name: directoryName,
|
|
|
|
path: directoryParent
|
|
|
|
})
|
|
|
|
.leftJoinAndSelect("directory.directories", "directories")
|
|
|
|
.leftJoinAndSelect("directory.photos", "photos")
|
|
|
|
.getOne();
|
|
|
|
|
2017-07-20 02:47:09 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
if (dir && dir.scanned == true) {
|
2017-10-20 00:08:07 +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
|
|
|
|
&& lastModified == knownLastModified &&
|
|
|
|
dir.lastScanned == knownLastScanned) {
|
|
|
|
|
|
|
|
if (Config.Server.indexing.reIndexingSensitivity == ReIndexingSensitivity.low) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (Date.now() - knownLastScanned <= Config.Server.indexing.cachedFolderTimeout &&
|
|
|
|
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-04 01:17:49 +08:00
|
|
|
if (dir.photos) {
|
|
|
|
for (let i = 0; i < dir.photos.length; i++) {
|
|
|
|
dir.photos[i].directory = dir;
|
2017-10-20 00:08:07 +08:00
|
|
|
//PhotoMetadataEntity.open(dir.photos[i].metadata);
|
2017-07-04 01:17:49 +08:00
|
|
|
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)
|
|
|
|
.createQueryBuilder("photo")
|
|
|
|
.where("photo.directory = :dir", {
|
|
|
|
dir: dir.directories[i].id
|
|
|
|
})
|
2017-07-22 01:14:22 +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];
|
2017-10-20 00:08:07 +08:00
|
|
|
// PhotoMetadataEntity.open(dir.directories[i].photos[j].metadata);
|
2017-07-18 05:12:12 +08:00
|
|
|
dir.directories[i].photos[j].readyThumbnails = [];
|
|
|
|
dir.directories[i].photos[j].readyIcon = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-20 02:47:09 +08:00
|
|
|
if (dir.lastModified != lastModified) {
|
2017-07-18 05:12:12 +08:00
|
|
|
return this.indexDirectory(relativeDirectoryName);
|
|
|
|
}
|
2017-07-04 01:17:49 +08:00
|
|
|
|
2017-07-28 05:10:16 +08:00
|
|
|
if ((Date.now() - dir.lastScanned > Config.Server.indexing.cachedFolderTimeout &&
|
|
|
|
Config.Server.indexing.reIndexingSensitivity >= ReIndexingSensitivity.medium) ||
|
|
|
|
Config.Server.indexing.reIndexingSensitivity >= ReIndexingSensitivity.high) {
|
2017-07-20 02:47:09 +08:00
|
|
|
//on the fly reindexing
|
|
|
|
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
|
|
|
}
|
|
|
|
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);
|
2017-07-21 05:37:10 +08:00
|
|
|
const connection = await SQLConnection.getConnection();
|
2017-07-04 01:17:49 +08:00
|
|
|
|
|
|
|
//returning with the result
|
|
|
|
scannedDirectory.photos.forEach(p => p.readyThumbnails = []);
|
|
|
|
resolve(scannedDirectory);
|
|
|
|
|
|
|
|
//saving to db
|
|
|
|
let directoryRepository = connection.getRepository(DirectoryEntity);
|
|
|
|
let photosRepository = connection.getRepository(PhotoEntity);
|
2016-12-29 22:18:23 +08:00
|
|
|
|
2016-12-28 03:55:51 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
let parentDir = await directoryRepository.createQueryBuilder("directory")
|
|
|
|
.where("directory.name = :name AND directory.path = :path", {
|
|
|
|
name: scannedDirectory.name,
|
|
|
|
path: scannedDirectory.path
|
|
|
|
}).getOne();
|
|
|
|
|
2017-10-20 00:08:07 +08:00
|
|
|
if (!!parentDir) {//Updated parent dir (if it was in the DB previously)
|
2017-07-04 01:17:49 +08:00
|
|
|
parentDir.scanned = true;
|
2017-07-20 02:47:09 +08:00
|
|
|
parentDir.lastModified = scannedDirectory.lastModified;
|
|
|
|
parentDir.lastScanned = scannedDirectory.lastScanned;
|
2017-10-20 00:08:07 +08:00
|
|
|
parentDir = await directoryRepository.save(parentDir);
|
2017-07-04 01:17:49 +08:00
|
|
|
} else {
|
|
|
|
(<DirectoryEntity>scannedDirectory).scanned = true;
|
2017-10-20 00:08:07 +08:00
|
|
|
parentDir = await directoryRepository.save(<DirectoryEntity>scannedDirectory);
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|
|
|
|
|
2017-07-18 05:12:12 +08:00
|
|
|
let indexedDirectories = await directoryRepository.createQueryBuilder("directory")
|
|
|
|
.where("directory.parent = :dir", {
|
|
|
|
dir: parentDir.id
|
|
|
|
}).getMany();
|
2017-07-04 01:17:49 +08:00
|
|
|
|
|
|
|
for (let i = 0; i < scannedDirectory.directories.length; i++) {
|
2017-07-18 05:12:12 +08:00
|
|
|
|
2017-10-20 00:08:07 +08:00
|
|
|
//Was this child Dir already indexed before?
|
2017-07-18 05:12:12 +08:00
|
|
|
let directory: DirectoryEntity = null;
|
|
|
|
for (let j = 0; j < indexedDirectories.length; j++) {
|
|
|
|
if (indexedDirectories[j].name == scannedDirectory.directories[i].name) {
|
|
|
|
directory = indexedDirectories[j];
|
|
|
|
indexedDirectories.splice(j, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 00:08:07 +08:00
|
|
|
if (directory != null) { //update existing directory
|
2017-07-18 05:12:12 +08:00
|
|
|
if (!directory.parent && !directory.parent.id) {
|
|
|
|
directory.parent = parentDir;
|
|
|
|
delete directory.photos;
|
2017-10-20 00:08:07 +08:00
|
|
|
await directoryRepository.save(directory);
|
2017-07-18 05:12:12 +08:00
|
|
|
}
|
2017-07-04 01:17:49 +08:00
|
|
|
} else {
|
|
|
|
scannedDirectory.directories[i].parent = parentDir;
|
|
|
|
(<DirectoryEntity>scannedDirectory.directories[i]).scanned = false;
|
2017-10-20 00:08:07 +08:00
|
|
|
const d = await directoryRepository.save(<DirectoryEntity>scannedDirectory.directories[i]);
|
2017-07-18 05:12:12 +08:00
|
|
|
for (let j = 0; j < scannedDirectory.directories[i].photos.length; j++) {
|
2017-10-20 00:08:07 +08:00
|
|
|
// PhotoMetadataEntity.close(scannedDirectory.directories[i].photos[j].metadata);
|
2017-07-18 05:12:12 +08:00
|
|
|
scannedDirectory.directories[i].photos[j].directory = d;
|
|
|
|
}
|
2017-10-20 00:08:07 +08:00
|
|
|
|
|
|
|
await photosRepository.save(scannedDirectory.directories[i].photos);
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 00:08:07 +08:00
|
|
|
//Remove child Dirs that are not anymore in the parent dir
|
2017-07-18 05:12:12 +08:00
|
|
|
await directoryRepository.remove(indexedDirectories);
|
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
|
|
|
|
let indexedPhotos = await photosRepository.createQueryBuilder("photo")
|
|
|
|
.where("photo.directory = :dir", {
|
|
|
|
dir: parentDir.id
|
|
|
|
}).getMany();
|
|
|
|
|
|
|
|
|
|
|
|
let 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (photo == null) {
|
|
|
|
scannedDirectory.photos[i].directory = null;
|
|
|
|
photo = Utils.clone(scannedDirectory.photos[i]);
|
|
|
|
scannedDirectory.photos[i].directory = scannedDirectory;
|
|
|
|
photo.directory = parentDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
//typeorm not supports recursive embended: TODO:fix it
|
2017-10-20 00:08:07 +08:00
|
|
|
// PhotoMetadataEntity.close(scannedDirectory.photos[i].metadata);
|
2017-07-18 05:12:12 +08:00
|
|
|
|
|
|
|
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;
|
2017-07-04 01:17:49 +08:00
|
|
|
photosToSave.push(photo);
|
|
|
|
}
|
|
|
|
}
|
2017-10-20 00:08:07 +08:00
|
|
|
await photosRepository.save(photosToSave);
|
2017-07-04 01:17:49 +08:00
|
|
|
await photosRepository.remove(indexedPhotos);
|
|
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
return reject(error);
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|