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

87 lines
2.4 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';
import {Router, RoutesRecognized} from '@angular/router';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
2017-07-04 01:17:49 +08:00
@Injectable()
export class ShareService {
2017-07-09 18:03:17 +08:00
public sharing: BehaviorSubject<SharingDTO>;
2017-07-04 01:17:49 +08:00
param = null;
queryParam = null;
sharingKey = null;
inited = false;
public ReadyPR: Promise<void>;
private resolve;
constructor(private _networkService: NetworkService, private router: Router) {
2017-07-09 18:03:17 +08:00
this.sharing = new BehaviorSubject(null);
2017-07-04 01:17:49 +08:00
this.ReadyPR = new Promise((resolve) => {
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-03-31 03:30:30 +08:00
this.param = val.state.root.firstChild.params['sharingKey'] || null;
this.queryParam = val.state.root.firstChild.queryParams['sk'] || null;
const changed = this.sharingKey !== this.param || this.queryParam;
2017-07-09 18:03:17 +08:00
if (changed) {
this.sharingKey = this.param || this.queryParam;
this.getSharing();
}
2017-07-04 01:17:49 +08:00
if (this.resolve) {
this.resolve();
this.inited = true;
}
}
});
}
public wait(): Promise<void> {
return this.ReadyPR;
}
2017-07-09 18:03:17 +08:00
public createSharing(dir: string, includeSubfolders: boolean, valid: number): Promise<SharingDTO> {
2018-03-31 03:30:30 +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> {
2018-03-31 03:30:30 +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> {
2018-03-31 03:30:30 +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
}