2016-05-09 23:04:56 +08:00
|
|
|
import {Injectable} from "@angular/core";
|
2016-12-27 06:36:38 +08:00
|
|
|
import {NetworkService} from "../model/network/network.service";
|
2016-05-10 01:14:33 +08:00
|
|
|
import {ContentWrapper} from "../../../common/entities/ConentWrapper";
|
2017-07-21 05:00:49 +08:00
|
|
|
import {DirectoryDTO} from "../../../common/entities/DirectoryDTO";
|
2016-05-16 17:03:11 +08:00
|
|
|
import {SearchTypes} from "../../../common/entities/AutoCompleteItem";
|
2016-06-26 17:08:05 +08:00
|
|
|
import {GalleryCacheService} from "./cache.gallery.service";
|
2017-06-21 19:47:21 +08:00
|
|
|
import {BehaviorSubject} from "rxjs/BehaviorSubject";
|
2017-07-04 01:17:49 +08:00
|
|
|
import {SharingDTO} from "../../../common/entities/SharingDTO";
|
|
|
|
import {Config} from "../../../common/config/public/Config";
|
|
|
|
import {ShareService} from "./share.service";
|
2016-03-20 23:54:30 +08:00
|
|
|
|
|
|
|
@Injectable()
|
2016-05-09 23:04:56 +08:00
|
|
|
export class GalleryService {
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2017-06-21 19:47:21 +08:00
|
|
|
public content: BehaviorSubject<ContentWrapper>;
|
2017-06-11 04:32:56 +08:00
|
|
|
private lastDirectory: DirectoryDTO;
|
|
|
|
private searchId: any;
|
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
constructor(private networkService: NetworkService,
|
|
|
|
private galleryCacheService: GalleryCacheService,
|
|
|
|
private _shareService: ShareService) {
|
2017-06-21 19:47:21 +08:00
|
|
|
this.content = new BehaviorSubject<ContentWrapper>(new ContentWrapper());
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
lastRequest: { directory: string } = {
|
|
|
|
directory: null
|
|
|
|
};
|
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
public async getDirectory(directoryName: string): Promise<ContentWrapper> {
|
2017-06-21 19:47:21 +08:00
|
|
|
const content = new ContentWrapper();
|
2017-06-11 04:32:56 +08:00
|
|
|
|
2017-06-21 19:47:21 +08:00
|
|
|
content.directory = this.galleryCacheService.getDirectory(directoryName);
|
|
|
|
content.searchResult = null;
|
|
|
|
|
|
|
|
this.content.next(content);
|
2017-06-11 04:32:56 +08:00
|
|
|
this.lastRequest.directory = directoryName;
|
|
|
|
|
2017-07-20 02:47:09 +08:00
|
|
|
const params = {};
|
2017-07-04 01:17:49 +08:00
|
|
|
if (Config.Client.Sharing.enabled == true) {
|
|
|
|
if (this._shareService.isSharing()) {
|
2017-07-20 02:47:09 +08:00
|
|
|
params['sk'] = this._shareService.getSharingKey();
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|
|
|
|
}
|
2017-07-08 06:18:24 +08:00
|
|
|
|
2017-07-20 02:47:09 +08:00
|
|
|
if (content.directory && content.directory.lastModified && content.directory.lastScanned) {
|
|
|
|
params['knownLastModified'] = content.directory.lastModified;
|
|
|
|
params['knownLastScanned'] = content.directory.lastScanned;
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
|
2017-07-20 02:47:09 +08:00
|
|
|
|
|
|
|
const cw = await this.networkService.getJson<ContentWrapper>("/gallery/content/" + directoryName, params);
|
|
|
|
|
|
|
|
|
2017-07-21 05:00:49 +08:00
|
|
|
if (!cw || cw.notModified == true) {
|
2017-07-16 16:59:28 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-21 05:00:49 +08:00
|
|
|
this.galleryCacheService.setDirectory(cw.directory); //save it before adding references
|
2017-06-11 04:32:56 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
if (this.lastRequest.directory != directoryName) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-18 07:11:53 +08:00
|
|
|
|
2016-05-10 01:14:33 +08:00
|
|
|
|
2017-07-20 02:47:09 +08:00
|
|
|
DirectoryDTO.addReferences(<DirectoryDTO>cw.directory);
|
2016-05-16 17:03:11 +08:00
|
|
|
|
2017-06-21 19:47:21 +08:00
|
|
|
|
2017-07-20 02:47:09 +08:00
|
|
|
this.lastDirectory = <DirectoryDTO>cw.directory;
|
2017-07-04 01:17:49 +08:00
|
|
|
this.content.next(cw);
|
2017-06-21 19:47:21 +08:00
|
|
|
|
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
return cw;
|
2017-06-21 19:47:21 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: cache
|
2017-07-04 01:17:49 +08:00
|
|
|
public async search(text: string, type?: SearchTypes): Promise<ContentWrapper> {
|
2017-06-11 04:32:56 +08:00
|
|
|
clearTimeout(this.searchId);
|
2017-07-08 06:18:24 +08:00
|
|
|
if (text === null || text === '' || text.trim() == ".") {
|
2017-07-04 01:17:49 +08:00
|
|
|
return null
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-05-16 17:03:11 +08:00
|
|
|
|
2017-07-20 02:47:09 +08:00
|
|
|
const cw: ContentWrapper = await this.networkService.getJson<ContentWrapper>("/search/" + text, {type: type});
|
2017-07-04 01:17:49 +08:00
|
|
|
this.content.next(cw);
|
|
|
|
return cw;
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: cache (together with normal search)
|
2017-07-04 01:17:49 +08:00
|
|
|
public async instantSearch(text: string): Promise<ContentWrapper> {
|
2017-07-08 06:18:24 +08:00
|
|
|
if (text === null || text === '' || text.trim() == ".") {
|
2017-06-21 19:47:21 +08:00
|
|
|
const content = new ContentWrapper();
|
|
|
|
content.directory = this.lastDirectory;
|
|
|
|
content.searchResult = null;
|
|
|
|
this.content.next(content);
|
2017-06-11 04:32:56 +08:00
|
|
|
clearTimeout(this.searchId);
|
2017-07-04 01:17:49 +08:00
|
|
|
return null
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-05-10 03:43:52 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
if (this.searchId != null) {
|
|
|
|
clearTimeout(this.searchId);
|
2016-05-10 03:43:52 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
|
|
|
this.searchId = setTimeout(() => {
|
|
|
|
this.search(text);
|
|
|
|
this.searchId = null;
|
|
|
|
}, 3000); //TODO: set timeout to config
|
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
const cw = await this.networkService.getJson<ContentWrapper>("/instant-search/" + text);
|
|
|
|
this.content.next(cw);
|
|
|
|
return cw;
|
|
|
|
|
|
|
|
}
|
2016-05-10 03:43:52 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
public async getSharing(sharingKey: string): Promise<SharingDTO> {
|
|
|
|
return this.networkService.getJson<SharingDTO>("/share/" + sharingKey);
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-03-20 23:54:30 +08:00
|
|
|
|
|
|
|
}
|