1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2025-01-14 14:43:17 +08:00
pigallery2/src/frontend/app/ui/gallery/cache.gallery.service.ts

276 lines
8.3 KiB
TypeScript
Raw Normal View History

2018-03-30 15:30:30 -04:00
import {Injectable} from '@angular/core';
import {DirectoryDTO, DirectoryDTOUtils} from '../../../../common/entities/DirectoryDTO';
import {Utils} from '../../../../common/Utils';
import {Config} from '../../../../common/config/public/Config';
2021-03-21 09:46:25 +01:00
import {IAutoCompleteItem} from '../../../../common/entities/AutoCompleteItem';
import {SearchResultDTO} from '../../../../common/entities/SearchResultDTO';
import {MediaBaseDTO} from '../../../../common/entities/MediaDTO';
import {SortingMethods} from '../../../../common/entities/SortingMethods';
import {VersionService} from '../../model/version.service';
2021-03-21 09:46:25 +01:00
import {SearchQueryDTO, SearchQueryTypes} from '../../../../common/entities/SearchQueryDTO';
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-12-19 23:14:33 +01:00
private static readonly CONTENT_PREFIX = 'content:';
private static readonly AUTO_COMPLETE_PREFIX = 'autocomplete:';
private static readonly INSTANT_SEARCH_PREFIX = 'instant_search:';
private static readonly SEARCH_PREFIX = 'search:';
private static readonly SORTING_PREFIX = 'sorting:';
private static readonly VERSION = 'version';
constructor(private versionService: VersionService) {
const onNewVersion = (ver: string) => {
if (ver !== null &&
localStorage.getItem(GalleryCacheService.VERSION) !== ver) {
2019-12-12 19:11:49 +01:00
GalleryCacheService.deleteCache();
localStorage.setItem(GalleryCacheService.VERSION, ver);
}
};
this.versionService.version.subscribe(onNewVersion);
onNewVersion(this.versionService.version.value);
}
2017-07-29 23:39:06 +02:00
2019-12-12 19:11:49 +01:00
private static loadCacheItem(key: string): SearchResultDTO {
const tmp = localStorage.getItem(key);
if (tmp != null) {
const value: CacheItem<SearchResultDTO> = JSON.parse(tmp);
if (value.timestamp < Date.now() - Config.Client.Search.searchCacheTimeout) {
2019-12-12 19:11:49 +01:00
localStorage.removeItem(key);
return null;
}
return value.item;
}
return null;
}
private static deleteCache(): void {
2019-12-12 19:11:49 +01:00
try {
const toRemove = [];
for (let i = 0; i < localStorage.length; i++) {
if (localStorage.key(i).startsWith(GalleryCacheService.CONTENT_PREFIX) ||
localStorage.key(i).startsWith(GalleryCacheService.SEARCH_PREFIX) ||
localStorage.key(i).startsWith(GalleryCacheService.INSTANT_SEARCH_PREFIX) ||
localStorage.key(i).startsWith(GalleryCacheService.AUTO_COMPLETE_PREFIX)
) {
toRemove.push(localStorage.key(i));
}
}
for (const item of toRemove) {
localStorage.removeItem(item);
2019-12-12 19:11:49 +01:00
}
} catch (e) {
}
}
2018-12-19 23:14:33 +01:00
public getSorting(dir: DirectoryDTO): SortingMethods {
const key = GalleryCacheService.SORTING_PREFIX + dir.path + '/' + dir.name;
const tmp = localStorage.getItem(key);
if (tmp != null) {
return parseInt(tmp, 10);
}
return null;
}
public removeSorting(dir: DirectoryDTO): void {
2018-12-19 23:14:33 +01:00
try {
const key = GalleryCacheService.SORTING_PREFIX + dir.path + '/' + dir.name;
localStorage.removeItem(key);
} catch (e) {
this.reset();
console.error(e);
}
}
public setSorting(dir: DirectoryDTO, sorting: SortingMethods): SortingMethods {
try {
const key = GalleryCacheService.SORTING_PREFIX + dir.path + '/' + dir.name;
localStorage.setItem(key, sorting.toString());
} catch (e) {
this.reset();
console.error(e);
}
return null;
}
2021-03-21 09:46:25 +01:00
public getAutoComplete(text: string, type: SearchQueryTypes): IAutoCompleteItem[] {
if (Config.Client.Other.enableCache === false) {
return null;
}
2021-03-21 09:46:25 +01:00
const key = GalleryCacheService.AUTO_COMPLETE_PREFIX + text + (type ? '_' + type : '');
2017-07-29 23:39:06 +02:00
const tmp = localStorage.getItem(key);
if (tmp != null) {
const value: CacheItem<IAutoCompleteItem[]> = JSON.parse(tmp);
if (value.timestamp < Date.now() - Config.Client.Search.AutoComplete.cacheTimeout) {
2017-07-29 23:39:06 +02:00
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;
}
2021-03-21 09:46:25 +01:00
public setAutoComplete(text: string, type: SearchQueryTypes, items: Array<IAutoCompleteItem>): void {
if (Config.Client.Other.enableCache === false) {
return;
}
2021-03-21 09:46:25 +01:00
const key = GalleryCacheService.AUTO_COMPLETE_PREFIX + text + (type ? '_' + type : '');
const tmp: CacheItem<Array<IAutoCompleteItem>> = {
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 20:57:16 +01:00
try {
2021-03-21 09:46:25 +01:00
localStorage.setItem(key, JSON.stringify(tmp));
2018-12-02 20:57:16 +01:00
} 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 {
if (Config.Client.Other.enableCache === false) {
return null;
}
2017-07-30 09:06:12 +02:00
const key = GalleryCacheService.INSTANT_SEARCH_PREFIX + text;
2019-12-12 19:11:49 +01:00
return GalleryCacheService.loadCacheItem(key);
2017-07-30 09:06:12 +02:00
}
public setInstantSearch(text: string, searchResult: SearchResultDTO): void {
if (Config.Client.Other.enableCache === false) {
return;
}
2017-07-30 09:06:12 +02:00
const tmp: CacheItem<SearchResultDTO> = {
timestamp: Date.now(),
item: searchResult
};
2018-12-02 20:57:16 +01: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(query: SearchQueryDTO): SearchResultDTO {
if (Config.Client.Other.enableCache === false) {
return null;
}
const key = GalleryCacheService.SEARCH_PREFIX + JSON.stringify(query);
2019-12-12 19:11:49 +01:00
return GalleryCacheService.loadCacheItem(key);
2017-07-30 09:06:12 +02:00
}
public setSearch(query: SearchQueryDTO, searchResult: SearchResultDTO): void {
if (Config.Client.Other.enableCache === false) {
return;
}
2017-07-30 09:06:12 +02:00
const tmp: CacheItem<SearchResultDTO> = {
timestamp: Date.now(),
item: searchResult
};
const key = GalleryCacheService.SEARCH_PREFIX + JSON.stringify(query);
2018-12-02 20:57:16 +01:00
try {
localStorage.setItem(key, JSON.stringify(tmp));
} catch (e) {
this.reset();
console.error(e);
}
2017-07-30 09:06:12 +02:00
}
public getDirectory(directoryName: string): DirectoryDTO {
if (Config.Client.Other.enableCache === false) {
return null;
}
2018-11-18 22:23:25 +01: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
DirectoryDTOUtils.unpackDirectory(directory);
2018-11-18 22:23:25 +01:00
return directory;
}
} catch (e) {
2016-06-26 11:08:05 +02:00
}
return null;
}
2016-06-26 11:08:05 +02:00
public setDirectory(directory: DirectoryDTO): void {
if (Config.Client.Other.enableCache === false) {
return;
}
2016-12-26 23:36:38 +01:00
2017-07-29 23:39:06 +02:00
const key = GalleryCacheService.CONTENT_PREFIX + Utils.concatUrls(directory.path, directory.name);
if (directory.isPartial === true && localStorage.getItem(key)) {
2017-07-21 19:14:22 +02:00
return;
}
2018-12-02 20:57:16 +01:00
try {
// try to fit it
2018-12-02 20:57:16 +01:00
localStorage.setItem(key, JSON.stringify(directory));
directory.directories.forEach((dir: DirectoryDTO) => {
const subKey = GalleryCacheService.CONTENT_PREFIX + Utils.concatUrls(dir.path, dir.name);
if (localStorage.getItem(subKey) == null) { // don't override existing
localStorage.setItem(subKey, JSON.stringify(dir));
}
});
2018-12-02 20:57:16 +01:00
} catch (e) {
this.reset();
console.error(e);
}
2016-06-26 11:08:05 +02:00
}
2016-06-26 11:08:05 +02:00
/**
2018-11-04 19:28:32 +01:00
* Update media state at cache too (Eg.: thumbnail rendered)
* @param media: MediaBaseDTO
*/
2021-04-01 21:48:38 +02:00
public mediaUpdated(media: MediaBaseDTO): void {
if (Config.Client.Other.enableCache === false) {
return;
}
try {
const directoryName = Utils.concatUrls(media.directory.path, media.directory.name);
const value = localStorage.getItem(directoryName);
if (value != null) {
const directory: DirectoryDTO = JSON.parse(value);
directory.media.forEach((p) => {
if (p.name === media.name) {
// update data
p.metadata = media.metadata;
p.readyThumbnails = media.readyThumbnails;
// save changes
localStorage.setItem(directoryName, JSON.stringify(directory));
return;
}
});
}
} catch (e) {
this.reset();
console.error(e);
}
2016-06-26 11:08:05 +02:00
}
private reset(): void {
try {
2020-01-07 22:17:54 +01:00
const currentUserStr = localStorage.getItem('currentUser');
localStorage.clear();
2020-01-07 22:17:54 +01:00
localStorage.setItem('currentUser', currentUserStr);
localStorage.setItem(GalleryCacheService.VERSION, this.versionService.version.value);
} catch (e) {
}
}
2016-06-26 11:08:05 +02:00
}