mirror of
https://github.com/xuthus83/pigallery2.git
synced 2024-11-03 21:04:03 +08:00
107 lines
3.1 KiB
TypeScript
107 lines
3.1 KiB
TypeScript
import {Injectable} from "@angular/core";
|
|
import {PhotoDTO} from "../../../common/entities/PhotoDTO";
|
|
import {DirectoryDTO} from "../../../common/entities/DirectoryDTO";
|
|
import {Utils} from "../../../common/Utils";
|
|
import {Config} from "../../../common/config/public/Config";
|
|
import {AutoCompleteItem} from "../../../common/entities/AutoCompleteItem";
|
|
|
|
interface AutoCompleteCacheItem {
|
|
timestamp: number;
|
|
items: Array<AutoCompleteItem>;
|
|
}
|
|
|
|
@Injectable()
|
|
export class GalleryCacheService {
|
|
|
|
private static CONTENT_PREFIX = "content:";
|
|
private static AUTO_COMPLETE_PREFIX = "content:";
|
|
|
|
|
|
public getAutoComplete(text: string): Array<AutoCompleteItem> {
|
|
const key = GalleryCacheService.AUTO_COMPLETE_PREFIX + text;
|
|
const tmp = localStorage.getItem(key);
|
|
if (tmp != null) {
|
|
const value: AutoCompleteCacheItem = JSON.parse(tmp);
|
|
if (value.timestamp < Date.now() - Config.Client.Search.autocompleteCacheTimeout) {
|
|
localStorage.removeItem(key);
|
|
return null;
|
|
}
|
|
return value.items;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public setAutoComplete(text, items: Array<AutoCompleteItem>): void {
|
|
const tmp: AutoCompleteCacheItem = {
|
|
timestamp: Date.now(),
|
|
items: items
|
|
};
|
|
localStorage.setItem(GalleryCacheService.AUTO_COMPLETE_PREFIX + text, JSON.stringify(tmp));
|
|
}
|
|
|
|
public getDirectory(directoryName: string): DirectoryDTO {
|
|
if (Config.Client.enableCache == false) {
|
|
return null;
|
|
}
|
|
let value = localStorage.getItem(GalleryCacheService.CONTENT_PREFIX + Utils.concatUrls(directoryName));
|
|
if (value != null) {
|
|
let directory: DirectoryDTO = JSON.parse(value);
|
|
|
|
DirectoryDTO.addReferences(directory);
|
|
return directory;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public setDirectory(directory: DirectoryDTO): void {
|
|
if (Config.Client.enableCache == false) {
|
|
return;
|
|
}
|
|
|
|
const key = GalleryCacheService.CONTENT_PREFIX + Utils.concatUrls(directory.path, directory.name);
|
|
if (directory.isPartial == true && localStorage.getItem(key)) {
|
|
return;
|
|
}
|
|
|
|
localStorage.setItem(key, JSON.stringify(directory));
|
|
|
|
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));
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
/**
|
|
* Update photo state at cache too (Eg.: thumbnail rendered)
|
|
* @param photo
|
|
*/
|
|
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) {
|
|
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;
|
|
}
|
|
});
|
|
}
|
|
|
|
}
|
|
|
|
}
|