1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-14 11:23:17 +02:00
pigallery2/frontend/app/gallery/cache.gallery.service.ts

79 lines
2.4 KiB
TypeScript
Raw Normal View History

2016-06-26 11:08:05 +02:00
import {Injectable} from "@angular/core";
2016-12-27 21:55:51 +02:00
import {PhotoDTO} from "../../../common/entities/PhotoDTO";
import {DirectoryDTO} from "../../../common/entities/DirectoryDTO";
2016-06-26 11:08:05 +02:00
import {Utils} from "../../../common/Utils";
import {Config} from "../config/Config";
2016-06-26 11:08:05 +02:00
@Injectable()
export class GalleryCacheService {
2016-12-27 21:55:51 +02:00
public getDirectory(directoryName: string): DirectoryDTO {
if (Config.Client.enableCache == false) {
return null;
}
2016-06-26 11:08:05 +02:00
let value = localStorage.getItem(directoryName);
if (value != null) {
2016-12-27 21:55:51 +02:00
let directory: DirectoryDTO = JSON.parse(value);
2016-06-26 11:08:05 +02:00
2016-07-04 18:12:58 +02:00
2016-12-27 21:55:51 +02:00
directory.photos.forEach((photo: PhotoDTO) => {
2016-07-04 18:12:58 +02:00
photo.metadata.creationDate = new Date(<any>photo.metadata.creationDate);
});
2016-12-27 00:36:38 +02:00
2016-12-27 21:55:51 +02:00
directory.photos.forEach((photo: PhotoDTO) => {
2016-06-26 11:08:05 +02:00
photo.directory = directory;
});
return directory;
}
return null;
}
2016-12-27 21:55:51 +02:00
public setDirectory(directory: DirectoryDTO): void {
if (Config.Client.enableCache == false) {
return;
}
2016-12-27 00:36:38 +02:00
2016-06-26 11:08:05 +02:00
localStorage.setItem(Utils.concatUrls(directory.path, directory.name), JSON.stringify(directory));
2016-12-27 21:55:51 +02:00
directory.directories.forEach((dir: DirectoryDTO) => {
2016-06-26 11:08:05 +02:00
let name = Utils.concatUrls(dir.path, dir.name);
if (localStorage.getItem(name) == null) { //don't override existing
localStorage.setItem(Utils.concatUrls(dir.path, dir.name), JSON.stringify(dir));
}
});
}
/**
* Update photo state at cache too (Eg.: thumbnail rendered)
* @param photo
*/
2016-12-27 21:55:51 +02:00
public photoUpdated(photo: PhotoDTO): void {
if (Config.Client.enableCache == false) {
return;
}
let directoryName = Utils.concatUrls(photo.directory.path, photo.directory.name);
let value = localStorage.getItem(directoryName);
if (value != null) {
2016-12-27 21:55:51 +02:00
let directory: DirectoryDTO = JSON.parse(value);
directory.photos.forEach((p) => {
if (p.name === photo.name) {
//update data
p.metadata = photo.metadata;
p.readyThumbnails = photo.readyThumbnails;
//save changes
localStorage.setItem(directoryName, JSON.stringify(directory));
return;
}
});
}
}
2016-06-26 11:08:05 +02:00
}