1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/frontend/app/gallery/cache.gallery.service.ts

164 lines
5.1 KiB
TypeScript
Raw Normal View History

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-09 18:03:17 +08:00
import {DirectoryDTO} 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";
2017-07-30 15:06:12 +08:00
import {AutoCompleteItem, SearchTypes} from "../../../common/entities/AutoCompleteItem";
import {SearchResultDTO} from "../../../common/entities/SearchResultDTO";
2017-07-30 05:39:06 +08:00
2017-07-30 15:06:12 +08:00
interface CacheItem<T> {
2017-07-30 05:39:06 +08:00
timestamp: number;
2017-07-30 15:06:12 +08:00
item: T;
2017-07-30 05:39:06 +08:00
}
2016-06-26 17:08:05 +08:00
@Injectable()
export class GalleryCacheService {
2017-07-30 05:39:06 +08:00
private static CONTENT_PREFIX = "content:";
2017-07-30 15:06:12 +08:00
private static AUTO_COMPLETE_PREFIX = "autocomplete:";
private static INSTANT_SEARCH_PREFIX = "instant_search:";
private static SEARCH_PREFIX = "search:";
private static SEARCH_TYPE_PREFIX = ":type:";
2017-07-30 05:39:06 +08:00
public getAutoComplete(text: string): Array<AutoCompleteItem> {
const key = GalleryCacheService.AUTO_COMPLETE_PREFIX + text;
const tmp = localStorage.getItem(key);
if (tmp != null) {
2017-07-30 15:06:12 +08:00
const value: CacheItem<Array<AutoCompleteItem>> = JSON.parse(tmp);
2017-07-30 05:39:06 +08:00
if (value.timestamp < Date.now() - Config.Client.Search.autocompleteCacheTimeout) {
localStorage.removeItem(key);
return null;
}
2017-07-30 15:06:12 +08:00
return value.item;
2017-07-30 05:39:06 +08:00
}
return null;
}
2017-07-30 15:06:12 +08:00
public setAutoComplete(text: string, items: Array<AutoCompleteItem>): void {
const tmp: CacheItem<Array<AutoCompleteItem>> = {
2017-07-30 05:39:06 +08:00
timestamp: Date.now(),
2017-07-30 15:06:12 +08:00
item: items
2017-07-30 05:39:06 +08:00
};
localStorage.setItem(GalleryCacheService.AUTO_COMPLETE_PREFIX + text, JSON.stringify(tmp));
}
2016-06-26 17:08:05 +08:00
2017-07-30 15:06:12 +08:00
public getInstantSearch(text: string): SearchResultDTO {
const key = GalleryCacheService.INSTANT_SEARCH_PREFIX + text;
const tmp = localStorage.getItem(key);
if (tmp != null) {
const value: CacheItem<SearchResultDTO> = JSON.parse(tmp);
if (value.timestamp < Date.now() - Config.Client.Search.instantSearchCacheTimeout) {
localStorage.removeItem(key);
return null;
}
return value.item;
}
return null;
}
public setInstantSearch(text: string, searchResult: SearchResultDTO): void {
const tmp: CacheItem<SearchResultDTO> = {
timestamp: Date.now(),
item: searchResult
};
localStorage.setItem(GalleryCacheService.INSTANT_SEARCH_PREFIX + text, JSON.stringify(tmp));
}
public getSearch(text: string, type?: SearchTypes): SearchResultDTO {
let key = GalleryCacheService.SEARCH_PREFIX + text;
if (typeof type != "undefined") {
key += GalleryCacheService.SEARCH_TYPE_PREFIX + type;
}
const tmp = localStorage.getItem(key);
if (tmp != null) {
const value: CacheItem<SearchResultDTO> = JSON.parse(tmp);
if (value.timestamp < Date.now() - Config.Client.Search.searchCacheTimeout) {
localStorage.removeItem(key);
return null;
}
return value.item;
}
return null;
}
public setSearch(text: string, type: SearchTypes, searchResult: SearchResultDTO): void {
const tmp: CacheItem<SearchResultDTO> = {
timestamp: Date.now(),
item: searchResult
};
let key = GalleryCacheService.SEARCH_PREFIX + text;
if (typeof type != "undefined") {
key += GalleryCacheService.SEARCH_TYPE_PREFIX + type;
}
localStorage.setItem(key, JSON.stringify(tmp));
}
public getDirectory(directoryName: string): DirectoryDTO {
if (Config.Client.enableCache == false) {
return null;
}
2017-07-30 05:39:06 +08:00
let value = localStorage.getItem(GalleryCacheService.CONTENT_PREFIX + Utils.concatUrls(directoryName));
if (value != null) {
let directory: DirectoryDTO = JSON.parse(value);
2016-06-26 17:08:05 +08:00
2017-07-09 18:03:17 +08:00
DirectoryDTO.addReferences(directory);
return directory;
2016-06-26 17:08:05 +08:00
}
return null;
}
2016-06-26 17:08:05 +08:00
public setDirectory(directory: DirectoryDTO): void {
if (Config.Client.enableCache == false) {
return;
}
2016-12-27 06:36:38 +08:00
2017-07-30 05:39:06 +08:00
const key = GalleryCacheService.CONTENT_PREFIX + Utils.concatUrls(directory.path, directory.name);
2017-07-22 01:14:22 +08:00
if (directory.isPartial == true && localStorage.getItem(key)) {
return;
}
localStorage.setItem(key, JSON.stringify(directory));
2016-06-26 17:08:05 +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
}
2016-06-26 17:08:05 +08:00
/**
* 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;
}
});
}
2016-06-26 17:08:05 +08:00
}
2016-06-26 17:08:05 +08:00
}