2016-12-28 03:55:51 +08:00
|
|
|
import {IGalleryManager} from "../interfaces/IGalleryManager";
|
|
|
|
import {DirectoryDTO} from "../../../common/entities/DirectoryDTO";
|
|
|
|
import * as path from "path";
|
2016-12-28 19:30:26 +08:00
|
|
|
import {DirectoryEntity} from "./enitites/DirectoryEntity";
|
2016-12-28 03:55:51 +08:00
|
|
|
import {MySQLConnection} from "./MySQLConnection";
|
|
|
|
import {DiskManager} from "../DiskManger";
|
|
|
|
import {PhotoEntity} from "./enitites/PhotoEntity";
|
|
|
|
import {Utils} from "../../../common/Utils";
|
2016-12-28 21:26:19 +08:00
|
|
|
import {ProjectPath} from "../../ProjectPath";
|
2016-12-28 03:55:51 +08:00
|
|
|
|
|
|
|
export class GalleryManager implements IGalleryManager {
|
|
|
|
|
|
|
|
|
|
|
|
public listDirectory(relativeDirectoryName, cb: (error: any, result: DirectoryDTO) => void) {
|
2016-12-28 21:26:19 +08:00
|
|
|
relativeDirectoryName = path.normalize(path.join("." + path.sep, relativeDirectoryName));
|
|
|
|
let directoryName = path.basename(relativeDirectoryName);
|
|
|
|
let directoryParent = path.join(path.dirname(relativeDirectoryName), path.sep);
|
2016-12-28 03:55:51 +08:00
|
|
|
console.log("GalleryManager:listDirectory");
|
2016-12-28 21:26:19 +08:00
|
|
|
console.log(directoryName, directoryParent, path.dirname(relativeDirectoryName), ProjectPath.normalizeRelative(path.dirname(relativeDirectoryName)));
|
2016-12-28 03:55:51 +08:00
|
|
|
MySQLConnection.getConnection().then(async connection => {
|
|
|
|
|
|
|
|
let dir = await connection
|
2016-12-28 19:30:26 +08:00
|
|
|
.getRepository(DirectoryEntity)
|
2016-12-28 21:26:19 +08:00
|
|
|
.createQueryBuilder("directory")
|
|
|
|
.where("directory.name = :name AND directory.path = :path", {
|
2016-12-28 03:55:51 +08:00
|
|
|
name: directoryName,
|
|
|
|
path: directoryParent
|
|
|
|
})
|
2016-12-28 21:26:19 +08:00
|
|
|
.leftJoinAndSelect("directory.directories", "directories")
|
|
|
|
.leftJoinAndSelect("directory.photos", "photos")
|
2016-12-28 03:55:51 +08:00
|
|
|
.getOne();
|
|
|
|
|
2016-12-28 22:35:27 +08:00
|
|
|
if (dir && dir.scanned == true) {
|
2016-12-28 21:26:19 +08:00
|
|
|
if (dir.photos) {
|
|
|
|
for (let i = 0; i < dir.photos.length; i++) {
|
|
|
|
dir.photos[i].directory = dir;
|
|
|
|
dir.photos[i].metadata.keywords = <any>JSON.parse(<any>dir.photos[i].metadata.keywords);
|
|
|
|
dir.photos[i].metadata.cameraData = <any>JSON.parse(<any>dir.photos[i].metadata.cameraData);
|
|
|
|
dir.photos[i].metadata.positionData = <any>JSON.parse(<any>dir.photos[i].metadata.positionData);
|
|
|
|
dir.photos[i].metadata.size = <any>JSON.parse(<any>dir.photos[i].metadata.size);
|
2016-12-29 22:38:20 +08:00
|
|
|
dir.photos[i].readyThumbnails = [];
|
2016-12-28 21:26:19 +08:00
|
|
|
}
|
2016-12-28 03:55:51 +08:00
|
|
|
}
|
2016-12-28 22:35:27 +08:00
|
|
|
|
|
|
|
cb(null, dir); //WARNING: only on the fly indexing should happen after this point
|
|
|
|
|
|
|
|
//on the fly updating
|
2016-12-29 22:18:23 +08:00
|
|
|
return this.indexDirectory(relativeDirectoryName, (err, res) => {
|
|
|
|
if (!!err || !res) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
});
|
2016-12-28 03:55:51 +08:00
|
|
|
}
|
|
|
|
return this.indexDirectory(relativeDirectoryName, cb);
|
|
|
|
|
|
|
|
|
|
|
|
}).catch((error) => {
|
|
|
|
return cb(error, null);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public indexDirectory(relativeDirectoryName, cb: (error: any, result: DirectoryDTO) => void) {
|
|
|
|
DiskManager.scanDirectory(relativeDirectoryName, (err, scannedDirectory) => {
|
2016-12-29 22:18:23 +08:00
|
|
|
if (!!err || !scannedDirectory) {
|
|
|
|
return cb(err, null);
|
|
|
|
}
|
|
|
|
|
2016-12-28 03:55:51 +08:00
|
|
|
MySQLConnection.getConnection().then(async connection => {
|
|
|
|
|
2016-12-29 22:18:23 +08:00
|
|
|
//returning with the result
|
2016-12-29 22:38:20 +08:00
|
|
|
scannedDirectory.photos.forEach(p => p.readyThumbnails = []);
|
2016-12-29 22:18:23 +08:00
|
|
|
cb(null, scannedDirectory);
|
|
|
|
|
|
|
|
//saving to db
|
2016-12-28 19:30:26 +08:00
|
|
|
let directoryRepository = connection.getRepository(DirectoryEntity);
|
2016-12-28 03:55:51 +08:00
|
|
|
let photosRepository = connection.getRepository(PhotoEntity);
|
|
|
|
|
2016-12-28 22:35:27 +08:00
|
|
|
|
|
|
|
let parentDir = await directoryRepository.createQueryBuilder("directory")
|
|
|
|
.where("directory.name = :name AND directory.path = :path", {
|
|
|
|
name: scannedDirectory.name,
|
|
|
|
path: scannedDirectory.path
|
|
|
|
}).getOne();
|
|
|
|
|
|
|
|
if (!!parentDir) {
|
|
|
|
parentDir.scanned = true;
|
|
|
|
parentDir.lastUpdate = Date.now();
|
|
|
|
parentDir = await directoryRepository.persist(parentDir);
|
|
|
|
} else {
|
|
|
|
(<DirectoryEntity>scannedDirectory).scanned = true;
|
|
|
|
parentDir = await directoryRepository.persist(<DirectoryEntity>scannedDirectory);
|
|
|
|
}
|
2016-12-28 03:55:51 +08:00
|
|
|
|
2016-12-29 22:18:23 +08:00
|
|
|
|
2016-12-28 03:55:51 +08:00
|
|
|
for (let i = 0; i < scannedDirectory.directories.length; i++) {
|
2016-12-29 22:18:23 +08:00
|
|
|
//TODO: simplify algorithm
|
2016-12-29 22:38:20 +08:00
|
|
|
let dir = await directoryRepository.createQueryBuilder("directory")
|
|
|
|
.where("directory.name = :name AND directory.path = :path", {
|
|
|
|
name: scannedDirectory.directories[i].name,
|
|
|
|
path: scannedDirectory.directories[i].path
|
|
|
|
}).getOne();
|
|
|
|
|
|
|
|
if (dir) {
|
|
|
|
dir.parent = parentDir;
|
|
|
|
await directoryRepository.persist(dir);
|
|
|
|
} else {
|
|
|
|
scannedDirectory.directories[i].parent = parentDir;
|
|
|
|
(<DirectoryEntity>scannedDirectory.directories[i]).scanned = false;
|
|
|
|
await directoryRepository.persist(<DirectoryEntity>scannedDirectory.directories[i]);
|
2016-12-28 22:35:27 +08:00
|
|
|
}
|
2016-12-28 03:55:51 +08:00
|
|
|
}
|
|
|
|
|
2016-12-29 22:18:23 +08:00
|
|
|
|
|
|
|
let indexedPhotos = await photosRepository.createQueryBuilder("photo")
|
|
|
|
.where("photo.directory = :dir", {
|
|
|
|
dir: parentDir.id
|
|
|
|
}).getMany();
|
|
|
|
|
|
|
|
|
|
|
|
let photosToSave = [];
|
2016-12-28 03:55:51 +08:00
|
|
|
for (let i = 0; i < scannedDirectory.photos.length; i++) {
|
2016-12-29 22:18:23 +08:00
|
|
|
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;
|
2016-12-28 22:35:27 +08:00
|
|
|
}
|
2016-12-29 22:18:23 +08:00
|
|
|
|
2016-12-28 03:55:51 +08:00
|
|
|
//typeorm not supports recursive embended: TODO:fix it
|
2016-12-29 22:18:23 +08:00
|
|
|
let keyStr = <any>JSON.stringify(scannedDirectory.photos[i].metadata.keywords);
|
|
|
|
let camStr = <any>JSON.stringify(scannedDirectory.photos[i].metadata.cameraData);
|
|
|
|
let posStr = <any>JSON.stringify(scannedDirectory.photos[i].metadata.positionData);
|
|
|
|
let sizeStr = <any>JSON.stringify(scannedDirectory.photos[i].metadata.size);
|
|
|
|
|
|
|
|
if (photo.metadata.keywords != keyStr ||
|
|
|
|
photo.metadata.cameraData != camStr ||
|
|
|
|
photo.metadata.positionData != posStr ||
|
|
|
|
photo.metadata.size != sizeStr) {
|
|
|
|
|
|
|
|
photo.metadata.keywords = keyStr;
|
|
|
|
photo.metadata.cameraData = camStr;
|
|
|
|
photo.metadata.positionData = posStr;
|
|
|
|
photo.metadata.size = sizeStr;
|
|
|
|
photosToSave.push(photo);
|
|
|
|
}
|
2016-12-28 03:55:51 +08:00
|
|
|
}
|
2016-12-29 22:18:23 +08:00
|
|
|
await photosRepository.persist(photosToSave);
|
|
|
|
await photosRepository.remove(indexedPhotos);
|
2016-12-28 03:55:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
}).catch((error) => {
|
|
|
|
return cb(error, null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|