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';
|
2018-05-23 08:27:07 +08:00
|
|
|
import {BehaviorSubject} from 'rxjs';
|
2018-11-29 06:49:33 +08:00
|
|
|
import {QueryService} from '../model/query.service';
|
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
|
|
|
|
|
|
|
|
|
|
|
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) => {
|
2018-05-13 00:19:51 +08:00
|
|
|
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;
|
2018-11-29 06:49:33 +08:00
|
|
|
this.queryParam = val.state.root.firstChild.queryParams[QueryService.SHARING_KEY] || null;
|
2018-05-13 00:19:51 +08:00
|
|
|
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
|
|
|
}
|