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/gallery.service.ts

163 lines
5.2 KiB
TypeScript
Raw Normal View History

2018-03-30 15:30:30 -04:00
import {Injectable} from '@angular/core';
import {NetworkService} from '../../model/network/network.service';
import {ContentWrapper} from '../../../../common/entities/ConentWrapper';
import {DirectoryDTO, DirectoryDTOUtils} from '../../../../common/entities/DirectoryDTO';
2018-03-30 15:30:30 -04:00
import {GalleryCacheService} from './cache.gallery.service';
2018-05-22 20:27:07 -04:00
import {BehaviorSubject} from 'rxjs';
import {Config} from '../../../../common/config/public/Config';
2018-03-30 15:30:30 -04:00
import {ShareService} from './share.service';
import {NavigationService} from '../../model/navigation.service';
import {SortingMethods} from '../../../../common/entities/SortingMethods';
import {QueryParams} from '../../../../common/QueryParams';
import {PG2ConfMap} from '../../../../common/PG2ConfMap';
import {SearchQueryDTO} from '../../../../common/entities/SearchQueryDTO';
2021-04-06 11:32:31 +02:00
import {ErrorCodes} from '../../../../common/entities/Error';
2018-05-28 14:03:12 -04:00
2016-03-20 16:54:30 +01:00
@Injectable()
2016-05-09 17:04:56 +02:00
export class GalleryService {
2021-04-06 11:32:31 +02:00
public content: BehaviorSubject<ContentWrapperWithError>;
2018-05-28 14:03:12 -04:00
public sorting: BehaviorSubject<SortingMethods>;
2019-02-22 23:39:01 +01:00
lastRequest: { directory: string } = {
directory: null
};
private lastDirectory: DirectoryDTO;
private searchId: any;
private ongoingSearch: SearchQueryDTO = null;
2017-07-03 19:17:49 +02:00
constructor(private networkService: NetworkService,
private galleryCacheService: GalleryCacheService,
private shareService: ShareService,
private navigationService: NavigationService) {
2021-04-06 11:32:31 +02:00
this.content = new BehaviorSubject<ContentWrapperWithError>(new ContentWrapperWithError());
this.sorting = new BehaviorSubject<SortingMethods>(Config.Client.Other.defaultPhotoSortingMethod);
}
getDefaultSorting(directory: DirectoryDTO): SortingMethods {
if (directory && directory.metaFile) {
for (const file in PG2ConfMap.sorting) {
if (directory.metaFile.some(f => f.name === file)) {
return (PG2ConfMap.sorting as any)[file];
}
}
}
return Config.Client.Other.defaultPhotoSortingMethod;
}
2018-05-28 14:03:12 -04:00
setSorting(sorting: SortingMethods): void {
this.sorting.next(sorting);
2018-12-19 23:14:33 +01:00
if (this.content.value.directory) {
if (sorting !== this.getDefaultSorting(this.content.value.directory)) {
2018-12-19 23:14:33 +01:00
this.galleryCacheService.setSorting(this.content.value.directory, sorting);
} else {
this.galleryCacheService.removeSorting(this.content.value.directory);
}
}
}
2021-04-06 11:32:31 +02:00
setContent(content: ContentWrapperWithError): void {
2018-12-19 23:14:33 +01:00
this.content.next(content);
if (content.directory) {
const sort = this.galleryCacheService.getSorting(content.directory);
if (sort !== null) {
this.sorting.next(sort);
} else {
this.sorting.next(this.getDefaultSorting(content.directory));
2018-12-19 23:14:33 +01:00
}
}
2018-05-28 14:03:12 -04:00
}
public async loadDirectory(directoryName: string): Promise<void> {
2021-04-06 11:32:31 +02:00
const content = new ContentWrapperWithError();
2017-06-21 13:47:21 +02:00
content.directory = this.galleryCacheService.getDirectory(directoryName);
content.searchResult = null;
2018-11-30 15:36:42 +01:00
2018-12-19 23:14:33 +01:00
this.setContent(content);
this.lastRequest.directory = directoryName;
2018-11-28 23:49:33 +01:00
const params: { [key: string]: any } = {};
if (Config.Client.Sharing.enabled === true) {
if (this.shareService.isSharing()) {
params[QueryParams.gallery.sharingKey_query] = this.shareService.getSharingKey();
2017-07-03 19:17:49 +02:00
}
}
2017-07-08 00:18:24 +02:00
2017-07-21 19:14:22 +02:00
if (content.directory && content.directory.lastModified && content.directory.lastScanned &&
!content.directory.isPartial) {
2018-12-09 12:02:02 +01:00
params[QueryParams.gallery.knownLastModified] = content.directory.lastModified;
params[QueryParams.gallery.knownLastScanned] = content.directory.lastScanned;
2017-07-03 19:17:49 +02:00
}
try {
2021-04-06 11:32:31 +02:00
const cw = await this.networkService.getJson<ContentWrapperWithError>('/gallery/content/' + directoryName, params);
2017-07-19 20:47:09 +02:00
2018-05-26 21:44:32 -04:00
if (!cw || cw.notModified === true) {
return;
}
2018-05-26 21:44:32 -04:00
this.galleryCacheService.setDirectory(cw.directory); // save it before adding references
2017-03-18 00:11:53 +01:00
2018-05-26 21:44:32 -04:00
if (this.lastRequest.directory !== directoryName) {
return;
}
DirectoryDTOUtils.unpackDirectory(cw.directory as DirectoryDTO);
2017-06-21 13:47:21 +02:00
this.lastDirectory = (cw.directory as DirectoryDTO);
2018-12-19 23:14:33 +01:00
this.setContent(cw);
} catch (e) {
2018-11-30 15:36:42 +01:00
console.error(e);
this.navigationService.toGallery().catch(console.error);
}
}
public async search(query: SearchQueryDTO): Promise<void> {
2017-07-30 09:06:12 +02:00
if (this.searchId != null) {
clearTimeout(this.searchId);
}
2016-05-16 11:03:11 +02:00
this.ongoingSearch = query;
2021-04-06 11:32:31 +02:00
this.setContent(new ContentWrapperWithError());
const cw = new ContentWrapperWithError();
cw.searchResult = this.galleryCacheService.getSearch(query);
2017-07-30 09:06:12 +02:00
if (cw.searchResult == null) {
2021-04-06 11:32:31 +02:00
try {
cw.searchResult = (await this.networkService.getJson<ContentWrapper>('/search/' + query)).searchResult;
this.galleryCacheService.setSearch(query, cw.searchResult);
} catch (e) {
if (e.code === ErrorCodes.LocationLookUp_ERROR) {
cw.error = 'Cannot find location: ' + e.message;
} else {
throw e;
}
}
2017-07-30 09:06:12 +02:00
}
if (this.ongoingSearch !== query) {
return;
}
2016-05-09 21:43:52 +02:00
2018-12-19 23:14:33 +01:00
this.setContent(cw);
2017-07-03 19:17:49 +02:00
}
2016-05-09 21:43:52 +02:00
2016-03-20 16:54:30 +01:00
isSearchResult(): boolean {
return !!this.content.value.searchResult;
}
2016-03-20 16:54:30 +01:00
}
2021-04-06 11:32:31 +02:00
export class ContentWrapperWithError extends ContentWrapper {
public error: string;
}