1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-14 11:23:17 +02:00
pigallery2/frontend/app/gallery/gallery.service.ts

152 lines
4.7 KiB
TypeScript
Raw Normal View History

2018-03-30 21:30:30 +02:00
import {Injectable} from '@angular/core';
import {NetworkService} from '../model/network/network.service';
import {ContentWrapper} from '../../../common/entities/ConentWrapper';
import {DirectoryDTO} from '../../../common/entities/DirectoryDTO';
import {SearchTypes} from '../../../common/entities/AutoCompleteItem';
import {GalleryCacheService} from './cache.gallery.service';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
import {SharingDTO} from '../../../common/entities/SharingDTO';
import {Config} from '../../../common/config/public/Config';
import {ShareService} from './share.service';
2016-03-20 17:54:30 +02:00
@Injectable()
2016-05-09 17:04:56 +02:00
export class GalleryService {
2017-06-21 13:47:21 +02:00
public content: BehaviorSubject<ContentWrapper>;
private lastDirectory: DirectoryDTO;
private searchId: any;
2017-07-03 19:17:49 +02:00
constructor(private networkService: NetworkService,
private galleryCacheService: GalleryCacheService,
private _shareService: ShareService) {
2017-06-21 13:47:21 +02:00
this.content = new BehaviorSubject<ContentWrapper>(new ContentWrapper());
}
lastRequest: { directory: string } = {
directory: null
};
2017-07-03 19:17:49 +02:00
public async getDirectory(directoryName: string): Promise<ContentWrapper> {
2017-06-21 13:47:21 +02:00
const content = new ContentWrapper();
2017-06-21 13:47:21 +02:00
content.directory = this.galleryCacheService.getDirectory(directoryName);
content.searchResult = null;
this.content.next(content);
this.lastRequest.directory = directoryName;
2017-07-19 20:47:09 +02:00
const params = {};
if (Config.Client.Sharing.enabled === true) {
2017-07-03 19:17:49 +02:00
if (this._shareService.isSharing()) {
2017-07-19 20:47:09 +02:00
params['sk'] = 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) {
2017-07-19 20:47:09 +02:00
params['knownLastModified'] = content.directory.lastModified;
params['knownLastScanned'] = content.directory.lastScanned;
2017-07-03 19:17:49 +02:00
}
2017-07-19 20:47:09 +02:00
2018-03-30 21:30:30 +02:00
const cw = await this.networkService.getJson<ContentWrapper>('/gallery/content/' + directoryName, params);
2017-07-19 20:47:09 +02:00
if (!cw || cw.notModified === true) {
return;
}
this.galleryCacheService.setDirectory(cw.directory); // save it before adding references
if (this.lastRequest.directory !== directoryName) {
2017-07-03 19:17:49 +02:00
return;
}
2017-03-18 01:11:53 +02:00
2017-07-19 20:47:09 +02:00
DirectoryDTO.addReferences(<DirectoryDTO>cw.directory);
2016-05-16 11:03:11 +02:00
2017-06-21 13:47:21 +02:00
2017-07-19 20:47:09 +02:00
this.lastDirectory = <DirectoryDTO>cw.directory;
2017-07-03 19:17:49 +02:00
this.content.next(cw);
2017-06-21 13:47:21 +02:00
2017-07-03 19:17:49 +02:00
return cw;
2017-06-21 13:47:21 +02:00
}
2017-07-03 19:17:49 +02:00
public async search(text: string, type?: SearchTypes): Promise<ContentWrapper> {
2017-07-30 09:06:12 +02:00
if (this.searchId != null) {
clearTimeout(this.searchId);
}
if (text === null || text === '' || text.trim() === '.') {
2018-03-30 21:30:30 +02:00
return null;
}
2016-05-16 11:03:11 +02:00
2017-07-28 00:08:35 +02:00
this.content.next(new ContentWrapper());
2017-07-30 09:06:12 +02:00
const cw = new ContentWrapper();
cw.searchResult = this.galleryCacheService.getSearch(text, type);
if (cw.searchResult == null) {
const params = {};
if (typeof type !== 'undefined') {
2017-07-30 09:06:12 +02:00
params['type'] = type;
}
2018-03-30 21:30:30 +02:00
cw.searchResult = (await this.networkService.getJson<ContentWrapper>('/search/' + text, params)).searchResult;
2017-07-30 09:06:12 +02:00
this.galleryCacheService.setSearch(text, type, cw.searchResult);
}
2017-07-03 19:17:49 +02:00
this.content.next(cw);
return cw;
}
2017-07-03 19:17:49 +02:00
public async instantSearch(text: string): Promise<ContentWrapper> {
if (text === null || text === '' || text.trim() === '.') {
2017-07-30 09:06:12 +02:00
const content = new ContentWrapper(this.lastDirectory);
2017-06-21 13:47:21 +02:00
this.content.next(content);
2017-07-30 09:06:12 +02:00
if (this.searchId != null) {
clearTimeout(this.searchId);
}
if (!this.lastDirectory) {
2018-03-30 21:30:30 +02:00
this.getDirectory('/');
2017-07-30 09:06:12 +02:00
}
2018-03-30 21:30:30 +02:00
return null;
}
2016-05-09 21:43:52 +02:00
if (this.searchId != null) {
clearTimeout(this.searchId);
}
2017-07-30 09:06:12 +02:00
const cw = new ContentWrapper();
cw.directory = null;
cw.searchResult = this.galleryCacheService.getSearch(text);
if (cw.searchResult == null) {
// If result is not search cache, try to load load more
2017-07-30 09:06:12 +02:00
this.searchId = setTimeout(() => {
this.search(text);
this.searchId = null;
}, Config.Client.Search.InstantSearchTimeout);
cw.searchResult = this.galleryCacheService.getInstantSearch(text);
if (cw.searchResult == null) {
2018-03-30 21:30:30 +02:00
cw.searchResult = (await this.networkService.getJson<ContentWrapper>('/instant-search/' + text)).searchResult;
2017-07-30 09:06:12 +02:00
this.galleryCacheService.setInstantSearch(text, cw.searchResult);
}
}
2017-07-03 19:17:49 +02:00
this.content.next(cw);
2017-07-30 09:06:12 +02:00
// if instant search do not have a result, do not do a search
if (cw.searchResult.photos.length === 0 && cw.searchResult.directories.length === 0) {
2017-07-30 09:06:12 +02:00
if (this.searchId != null) {
clearTimeout(this.searchId);
}
}
2017-07-03 19:17:49 +02:00
return cw;
}
2016-05-09 21:43:52 +02:00
2017-07-03 19:17:49 +02:00
public async getSharing(sharingKey: string): Promise<SharingDTO> {
2018-03-30 21:30:30 +02:00
return this.networkService.getJson<SharingDTO>('/share/' + sharingKey);
}
2016-03-20 17:54:30 +02:00
}