2016-06-26 17:08:05 +08:00
|
|
|
///<reference path="../../browser.d.ts"/>
|
|
|
|
|
|
|
|
import {Injectable} from "@angular/core";
|
|
|
|
import {Photo} from "../../../common/entities/Photo";
|
|
|
|
import {Directory} from "../../../common/entities/Directory";
|
|
|
|
import {Utils} from "../../../common/Utils";
|
2016-06-26 21:54:10 +08:00
|
|
|
import {Config} from "../config/Config";
|
2016-06-26 17:08:05 +08:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class GalleryCacheService {
|
|
|
|
|
|
|
|
|
|
|
|
public getDirectory(directoryName:string):Directory {
|
2016-06-26 21:54:10 +08:00
|
|
|
if (Config.Client.enableCache == false) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-06-26 17:08:05 +08:00
|
|
|
let value = localStorage.getItem(directoryName);
|
|
|
|
if (value != null) {
|
|
|
|
let directory:Directory = JSON.parse(value);
|
|
|
|
|
2016-07-05 00:12:58 +08:00
|
|
|
|
|
|
|
directory.photos.forEach((photo:Photo) => {
|
|
|
|
photo.metadata.creationDate = new Date(<any>photo.metadata.creationDate);
|
|
|
|
});
|
|
|
|
|
2016-06-26 17:08:05 +08:00
|
|
|
directory.photos.forEach((photo:Photo) => {
|
|
|
|
photo.directory = directory;
|
|
|
|
});
|
|
|
|
|
|
|
|
return directory;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public setDirectory(directory:Directory):void {
|
2016-06-26 21:54:10 +08:00
|
|
|
if (Config.Client.enableCache == false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-26 17:08:05 +08:00
|
|
|
localStorage.setItem(Utils.concatUrls(directory.path, directory.name), JSON.stringify(directory));
|
|
|
|
|
|
|
|
directory.directories.forEach((dir:Directory) => {
|
|
|
|
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 21:54:10 +08:00
|
|
|
/**
|
|
|
|
* Update photo state at cache too (Eg.: thumbnail rendered)
|
|
|
|
* @param photo
|
|
|
|
*/
|
|
|
|
public photoUpdated(photo:Photo):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) {
|
|
|
|
let directory:Directory = 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 17:08:05 +08:00
|
|
|
|
|
|
|
}
|