2016-06-26 17:08:05 +08:00
|
|
|
import {Injectable} from "@angular/core";
|
2016-12-28 03:55:51 +08:00
|
|
|
import {PhotoDTO} from "../../../common/entities/PhotoDTO";
|
2017-07-08 06:18:24 +08:00
|
|
|
import {DirectoryDTO, DirectoryUtil} from "../../../common/entities/DirectoryDTO";
|
2016-06-26 17:08:05 +08:00
|
|
|
import {Utils} from "../../../common/Utils";
|
2017-06-04 21:25:08 +08:00
|
|
|
import {Config} from "../../../common/config/public/Config";
|
2016-06-26 17:08:05 +08:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class GalleryCacheService {
|
|
|
|
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public getDirectory(directoryName: string): DirectoryDTO {
|
|
|
|
if (Config.Client.enableCache == false) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let value = localStorage.getItem(directoryName);
|
|
|
|
if (value != null) {
|
|
|
|
let directory: DirectoryDTO = JSON.parse(value);
|
2016-06-26 17:08:05 +08:00
|
|
|
|
2017-07-08 06:18:24 +08:00
|
|
|
DirectoryUtil.addReferences(directory);
|
2017-06-11 04:32:56 +08:00
|
|
|
return directory;
|
2016-06-26 17:08:05 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
return null;
|
|
|
|
}
|
2016-06-26 17:08:05 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public setDirectory(directory: DirectoryDTO): void {
|
|
|
|
if (Config.Client.enableCache == false) {
|
|
|
|
return;
|
|
|
|
}
|
2016-12-27 06:36:38 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
localStorage.setItem(Utils.concatUrls(directory.path, directory.name), JSON.stringify(directory));
|
2016-06-26 17:08:05 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
directory.directories.forEach((dir: DirectoryDTO) => {
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
});
|
2016-06-26 17:08:05 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-06-26 17:08:05 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
/**
|
|
|
|
* Update photo state at cache too (Eg.: thumbnail rendered)
|
|
|
|
* @param photo
|
|
|
|
*/
|
|
|
|
public photoUpdated(photo: PhotoDTO): void {
|
2016-06-26 21:54:10 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
if (Config.Client.enableCache == false) {
|
|
|
|
return;
|
|
|
|
}
|
2016-06-26 21:54:10 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
let directoryName = Utils.concatUrls(photo.directory.path, photo.directory.name);
|
|
|
|
let value = localStorage.getItem(directoryName);
|
|
|
|
if (value != null) {
|
|
|
|
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 21:54:10 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
});
|
2016-06-26 21:54:10 +08:00
|
|
|
}
|
2016-06-26 17:08:05 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
|
|
|
|
2016-06-26 17:08:05 +08:00
|
|
|
}
|