1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/frontend/app/ui/gallery/share.service.ts

113 lines
3.1 KiB
TypeScript
Raw Normal View History

2018-03-31 03:30:30 +08:00
import {Injectable} from '@angular/core';
import {NetworkService} from '../../model/network/network.service';
import {CreateSharingDTO, SharingDTO} from '../../../../common/entities/SharingDTO';
2018-03-31 03:30:30 +08:00
import {Router, RoutesRecognized} from '@angular/router';
2019-02-23 06:39:01 +08:00
import {BehaviorSubject, Observable} from 'rxjs';
import {QueryParams} from '../../../../common/QueryParams';
import {UserDTO} from '../../../../common/entities/UserDTO';
2017-07-04 01:17:49 +08:00
@Injectable()
export class ShareService {
2017-07-09 18:03:17 +08:00
public sharing: BehaviorSubject<SharingDTO>;
2018-11-29 06:49:33 +08:00
param: string = null;
queryParam: string = null;
sharingKey: string = null;
2017-07-04 01:17:49 +08:00
inited = false;
public ReadyPR: Promise<void>;
2018-11-29 06:49:33 +08:00
private resolve: () => void;
2017-07-04 01:17:49 +08:00
2019-02-23 06:39:01 +08:00
constructor(private networkService: NetworkService,
private router: Router) {
2017-07-09 18:03:17 +08:00
this.sharing = new BehaviorSubject(null);
2018-11-29 06:49:33 +08:00
this.ReadyPR = new Promise((resolve: () => void) => {
if (this.inited === true) {
2017-07-04 01:17:49 +08:00
return resolve();
}
this.resolve = resolve;
});
this.router.events.subscribe(val => {
if (val instanceof RoutesRecognized) {
2018-11-30 22:36:42 +08:00
this.param = val.state.root.firstChild.params[QueryParams.gallery.sharingKey_long] || null;
this.queryParam = val.state.root.firstChild.queryParams[QueryParams.gallery.sharingKey_short] || null;
2019-02-23 06:39:01 +08:00
const changed = this.sharingKey !== (this.param || this.queryParam);
2017-07-09 18:03:17 +08:00
if (changed) {
2019-02-23 06:39:01 +08:00
this.sharingKey = this.param || this.queryParam || this.sharingKey;
2017-07-09 18:03:17 +08:00
this.getSharing();
}
2017-07-04 01:17:49 +08:00
if (this.resolve) {
this.resolve();
2019-02-23 06:39:01 +08:00
this.resolve = null;
2017-07-04 01:17:49 +08:00
this.inited = true;
}
}
});
2019-02-23 06:39:01 +08:00
}
public setUserObs(userOB: Observable<UserDTO>) {
userOB.subscribe((user) => {
if (user && !!user.usedSharingKey) {
if (user.usedSharingKey !== this.sharingKey) {
this.sharingKey = user.usedSharingKey;
this.getSharing();
}
if (this.resolve) {
this.resolve();
this.resolve = null;
this.inited = true;
}
}
});
2017-07-04 01:17:49 +08:00
}
public wait(): Promise<void> {
2019-02-23 06:39:01 +08:00
if (this.inited) {
return Promise.resolve();
}
2017-07-04 01:17:49 +08:00
return this.ReadyPR;
}
2017-07-09 18:03:17 +08:00
public createSharing(dir: string, includeSubfolders: boolean, valid: number): Promise<SharingDTO> {
2019-02-23 06:39:01 +08:00
return this.networkService.postJson('/share/' + dir, {
2017-07-04 01:17:49 +08:00
createSharing: <CreateSharingDTO>{
includeSubfolders: includeSubfolders,
valid: valid
}
});
}
2017-07-10 04:36:25 +08:00
public updateSharing(dir: string, sharingId: number, includeSubfolders: boolean, password: string, valid: number): Promise<SharingDTO> {
2019-02-23 06:39:01 +08:00
return this.networkService.putJson('/share/' + dir, {
2017-07-04 01:17:49 +08:00
updateSharing: <CreateSharingDTO>{
id: sharingId,
includeSubfolders: includeSubfolders,
2017-07-10 04:36:25 +08:00
valid: valid,
password: password
2017-07-04 01:17:49 +08:00
}
});
}
public getSharingKey() {
return this.sharingKey;
}
public isSharing(): boolean {
return this.sharingKey != null;
}
2017-07-09 18:03:17 +08:00
public async getSharing(): Promise<SharingDTO> {
2019-02-23 06:39:01 +08:00
const sharing = await this.networkService.getJson<SharingDTO>('/share/' + this.getSharingKey());
2017-07-09 18:03:17 +08:00
this.sharing.next(sharing);
return sharing;
}
2017-07-04 01:17:49 +08:00
}