2018-03-30 21:30:30 +02:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {DirectoryDTO} from '../../../common/entities/DirectoryDTO';
|
|
|
|
import {Utils} from '../../../common/Utils';
|
|
|
|
import {Config} from '../../../common/config/public/Config';
|
|
|
|
import {AutoCompleteItem, SearchTypes} from '../../../common/entities/AutoCompleteItem';
|
|
|
|
import {SearchResultDTO} from '../../../common/entities/SearchResultDTO';
|
2018-11-04 20:28:32 +02:00
|
|
|
import {MediaDTO} from '../../../common/entities/MediaDTO';
|
2018-11-18 23:56:05 +02:00
|
|
|
import {DataStructureVersion} from '../../../common/DataStructureVersion';
|
2017-07-29 23:39:06 +02:00
|
|
|
|
2017-07-30 09:06:12 +02:00
|
|
|
interface CacheItem<T> {
|
2017-07-29 23:39:06 +02:00
|
|
|
timestamp: number;
|
2017-07-30 09:06:12 +02:00
|
|
|
item: T;
|
2017-07-29 23:39:06 +02:00
|
|
|
}
|
2016-06-26 11:08:05 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class GalleryCacheService {
|
|
|
|
|
2018-03-30 21:30:30 +02:00
|
|
|
private static CONTENT_PREFIX = 'content:';
|
|
|
|
private static AUTO_COMPLETE_PREFIX = 'autocomplete:';
|
|
|
|
private static INSTANT_SEARCH_PREFIX = 'instant_search:';
|
|
|
|
private static SEARCH_PREFIX = 'search:';
|
|
|
|
private static SEARCH_TYPE_PREFIX = ':type:';
|
2018-11-18 23:56:05 +02:00
|
|
|
private static VERSION = 'version';
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
const version = parseInt(localStorage.getItem(GalleryCacheService.VERSION), 10) || 0;
|
|
|
|
if (version !== DataStructureVersion) {
|
|
|
|
localStorage.clear();
|
|
|
|
localStorage.setItem(GalleryCacheService.VERSION, DataStructureVersion.toString());
|
|
|
|
}
|
2018-12-02 21:57:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private reset() {
|
|
|
|
try {
|
|
|
|
localStorage.clear();
|
|
|
|
localStorage.setItem(GalleryCacheService.VERSION, DataStructureVersion.toString());
|
|
|
|
} catch (e) {
|
2018-11-18 23:56:05 +02:00
|
|
|
|
2018-12-02 21:57:16 +02:00
|
|
|
}
|
2018-11-18 23:56:05 +02:00
|
|
|
}
|
2017-07-29 23:39:06 +02:00
|
|
|
|
|
|
|
|
2018-12-02 13:22:20 +02:00
|
|
|
public getAutoComplete(text: string): AutoCompleteItem[] {
|
2017-07-29 23:39:06 +02:00
|
|
|
const key = GalleryCacheService.AUTO_COMPLETE_PREFIX + text;
|
|
|
|
const tmp = localStorage.getItem(key);
|
|
|
|
if (tmp != null) {
|
2018-12-02 13:22:20 +02:00
|
|
|
const value: CacheItem<AutoCompleteItem[]> = JSON.parse(tmp);
|
2017-07-29 23:39:06 +02:00
|
|
|
if (value.timestamp < Date.now() - Config.Client.Search.autocompleteCacheTimeout) {
|
|
|
|
localStorage.removeItem(key);
|
|
|
|
return null;
|
|
|
|
}
|
2017-07-30 09:06:12 +02:00
|
|
|
return value.item;
|
2017-07-29 23:39:06 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-07-30 09:06:12 +02:00
|
|
|
public setAutoComplete(text: string, items: Array<AutoCompleteItem>): void {
|
|
|
|
const tmp: CacheItem<Array<AutoCompleteItem>> = {
|
2017-07-29 23:39:06 +02:00
|
|
|
timestamp: Date.now(),
|
2017-07-30 09:06:12 +02:00
|
|
|
item: items
|
2017-07-29 23:39:06 +02:00
|
|
|
};
|
2018-12-02 21:57:16 +02:00
|
|
|
try {
|
|
|
|
localStorage.setItem(GalleryCacheService.AUTO_COMPLETE_PREFIX + text, JSON.stringify(tmp));
|
|
|
|
} catch (e) {
|
|
|
|
this.reset();
|
|
|
|
console.error(e);
|
|
|
|
}
|
2017-07-29 23:39:06 +02:00
|
|
|
}
|
2016-06-26 11:08:05 +02:00
|
|
|
|
2017-07-30 09:06:12 +02: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
|
|
|
|
};
|
2018-12-02 21:57:16 +02:00
|
|
|
try {
|
|
|
|
localStorage.setItem(GalleryCacheService.INSTANT_SEARCH_PREFIX + text, JSON.stringify(tmp));
|
|
|
|
} catch (e) {
|
|
|
|
this.reset();
|
|
|
|
console.error(e);
|
|
|
|
}
|
2017-07-30 09:06:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public getSearch(text: string, type?: SearchTypes): SearchResultDTO {
|
|
|
|
let key = GalleryCacheService.SEARCH_PREFIX + text;
|
2018-12-04 22:43:07 +02:00
|
|
|
if (typeof type !== 'undefined' && type !== null) {
|
2017-07-30 09:06:12 +02:00
|
|
|
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;
|
2018-12-04 22:43:07 +02:00
|
|
|
if (typeof type !== 'undefined' && type !== null) {
|
2017-07-30 09:06:12 +02:00
|
|
|
key += GalleryCacheService.SEARCH_TYPE_PREFIX + type;
|
|
|
|
}
|
2018-12-02 21:57:16 +02:00
|
|
|
try {
|
|
|
|
localStorage.setItem(key, JSON.stringify(tmp));
|
|
|
|
} catch (e) {
|
|
|
|
this.reset();
|
|
|
|
console.error(e);
|
|
|
|
}
|
2017-07-30 09:06:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
public getDirectory(directoryName: string): DirectoryDTO {
|
2018-11-02 17:24:37 +02:00
|
|
|
if (Config.Client.Other.enableCache === false) {
|
2017-06-10 22:32:56 +02:00
|
|
|
return null;
|
|
|
|
}
|
2018-11-18 23:23:25 +02:00
|
|
|
try {
|
|
|
|
const value = localStorage.getItem(GalleryCacheService.CONTENT_PREFIX + Utils.concatUrls(directoryName));
|
|
|
|
if (value != null) {
|
|
|
|
const directory: DirectoryDTO = JSON.parse(value);
|
2016-06-26 11:08:05 +02:00
|
|
|
|
2018-11-18 23:23:25 +02:00
|
|
|
DirectoryDTO.addReferences(directory);
|
|
|
|
return directory;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2016-06-26 11:08:05 +02:00
|
|
|
}
|
2017-06-10 22:32:56 +02:00
|
|
|
return null;
|
|
|
|
}
|
2016-06-26 11:08:05 +02:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
public setDirectory(directory: DirectoryDTO): void {
|
2018-11-02 17:24:37 +02:00
|
|
|
if (Config.Client.Other.enableCache === false) {
|
2017-06-10 22:32:56 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-12-27 00:36:38 +02:00
|
|
|
|
2017-07-29 23:39:06 +02:00
|
|
|
const key = GalleryCacheService.CONTENT_PREFIX + Utils.concatUrls(directory.path, directory.name);
|
2018-05-12 18:19:51 +02:00
|
|
|
if (directory.isPartial === true && localStorage.getItem(key)) {
|
2017-07-21 19:14:22 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-02 21:57:16 +02:00
|
|
|
try {
|
|
|
|
localStorage.setItem(key, JSON.stringify(directory));
|
|
|
|
} catch (e) {
|
|
|
|
this.reset();
|
|
|
|
console.error(e);
|
|
|
|
}
|
2017-06-10 22:32:56 +02:00
|
|
|
directory.directories.forEach((dir: DirectoryDTO) => {
|
2017-12-20 01:20:37 +02:00
|
|
|
const sub_key = GalleryCacheService.CONTENT_PREFIX + Utils.concatUrls(dir.path, dir.name);
|
2018-05-12 18:19:51 +02:00
|
|
|
if (localStorage.getItem(sub_key) == null) { // don't override existing
|
2017-12-20 01:20:37 +02:00
|
|
|
localStorage.setItem(sub_key, JSON.stringify(dir));
|
2017-06-10 22:32:56 +02:00
|
|
|
}
|
|
|
|
});
|
2016-06-26 11:08:05 +02:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
}
|
2016-06-26 11:08:05 +02:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
/**
|
2018-11-04 20:28:32 +02:00
|
|
|
* Update media state at cache too (Eg.: thumbnail rendered)
|
|
|
|
* @param media
|
2017-06-10 22:32:56 +02:00
|
|
|
*/
|
2018-11-04 20:28:32 +02:00
|
|
|
public mediaUpdated(media: MediaDTO): void {
|
2016-06-26 15:54:10 +02:00
|
|
|
|
2018-11-02 17:24:37 +02:00
|
|
|
if (Config.Client.Other.enableCache === false) {
|
2017-06-10 22:32:56 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-06-26 15:54:10 +02:00
|
|
|
|
2018-11-04 20:28:32 +02:00
|
|
|
const directoryName = Utils.concatUrls(media.directory.path, media.directory.name);
|
2018-05-12 18:19:51 +02:00
|
|
|
const value = localStorage.getItem(directoryName);
|
2017-06-10 22:32:56 +02:00
|
|
|
if (value != null) {
|
2018-05-12 18:19:51 +02:00
|
|
|
const directory: DirectoryDTO = JSON.parse(value);
|
2018-11-04 20:28:32 +02:00
|
|
|
directory.media.forEach((p) => {
|
|
|
|
if (p.name === media.name) {
|
2018-05-12 18:19:51 +02:00
|
|
|
// update data
|
2018-11-04 20:28:32 +02:00
|
|
|
p.metadata = media.metadata;
|
|
|
|
p.readyThumbnails = media.readyThumbnails;
|
2017-06-10 22:32:56 +02:00
|
|
|
|
2018-05-12 18:19:51 +02:00
|
|
|
// save changes
|
2017-06-10 22:32:56 +02:00
|
|
|
localStorage.setItem(directoryName, JSON.stringify(directory));
|
|
|
|
return;
|
2016-06-26 15:54:10 +02:00
|
|
|
}
|
2017-06-10 22:32:56 +02:00
|
|
|
});
|
2016-06-26 15:54:10 +02:00
|
|
|
}
|
2016-06-26 11:08:05 +02:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
}
|
|
|
|
|
2016-06-26 11:08:05 +02:00
|
|
|
}
|