1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2025-01-14 14:43:17 +08:00
pigallery2/src/frontend/app/ui/gallery/share.service.ts

118 lines
3.4 KiB
TypeScript
Raw Normal View History

2018-03-30 15:30:30 -04:00
import {Injectable} from '@angular/core';
import {NetworkService} from '../../model/network/network.service';
import {CreateSharingDTO, SharingDTO} from '../../../../common/entities/SharingDTO';
2018-03-30 15:30:30 -04:00
import {Router, RoutesRecognized} from '@angular/router';
2020-01-07 22:17:54 +01:00
import {BehaviorSubject} from 'rxjs';
import {distinctUntilChanged, filter} from 'rxjs/operators';
import {QueryParams} from '../../../../common/QueryParams';
import {UserDTO} from '../../../../common/entities/UserDTO';
2017-07-03 19:17:49 +02:00
@Injectable()
export class ShareService {
2018-11-28 23:49:33 +01:00
param: string = null;
queryParam: string = null;
sharingKey: string = null;
2017-07-03 19:17:49 +02:00
inited = false;
public ReadyPR: Promise<void>;
2020-01-07 22:17:54 +01:00
public sharingSubject: BehaviorSubject<SharingDTO> = new BehaviorSubject(null);
public currentSharing = this.sharingSubject
.asObservable().pipe(filter(s => s !== null)).pipe(distinctUntilChanged());
2017-07-03 19:17:49 +02:00
2020-01-07 22:17:54 +01:00
private resolve: () => void;
2017-07-03 19:17:49 +02:00
2019-02-22 23:39:01 +01:00
constructor(private networkService: NetworkService,
private router: Router) {
2018-11-28 23:49:33 +01:00
this.ReadyPR = new Promise((resolve: () => void) => {
if (this.inited === true) {
2017-07-03 19:17:49 +02:00
return resolve();
}
this.resolve = resolve;
});
2020-01-07 22:17:54 +01:00
this.router.events.subscribe(async val => {
2017-07-03 19:17:49 +02:00
if (val instanceof RoutesRecognized) {
2020-01-07 22:17:54 +01:00
this.param = val.state.root.firstChild.params[QueryParams.gallery.sharingKey_params] || null;
this.queryParam = val.state.root.firstChild.queryParams[QueryParams.gallery.sharingKey_query] || null;
2019-02-22 23:39:01 +01:00
const changed = this.sharingKey !== (this.param || this.queryParam);
2017-07-09 12:03:17 +02:00
if (changed) {
2019-02-22 23:39:01 +01:00
this.sharingKey = this.param || this.queryParam || this.sharingKey;
2020-01-07 22:17:54 +01:00
await this.getSharing();
2017-07-09 12:03:17 +02:00
}
2017-07-03 19:17:49 +02:00
if (this.resolve) {
this.resolve();
2019-02-22 23:39:01 +01:00
this.resolve = null;
2017-07-03 19:17:49 +02:00
this.inited = true;
}
}
});
2019-02-22 23:39:01 +01:00
}
2020-01-07 22:17:54 +01:00
onNewUser = async (user: UserDTO) => {
if (user && !!user.usedSharingKey) {
if (user.usedSharingKey !== this.sharingKey ||
this.sharingSubject.value == null) {
this.sharingKey = user.usedSharingKey;
await this.getSharing();
2019-02-22 23:39:01 +01:00
}
2020-01-07 22:17:54 +01:00
if (this.resolve) {
this.resolve();
this.resolve = null;
this.inited = true;
}
}
};
2017-07-03 19:17:49 +02:00
public wait(): Promise<void> {
2019-02-22 23:39:01 +01:00
if (this.inited) {
return Promise.resolve();
}
2017-07-03 19:17:49 +02:00
return this.ReadyPR;
}
public createSharing(dir: string, includeSubFolders: boolean, valid: number): Promise<SharingDTO> {
2019-02-22 23:39:01 +01:00
return this.networkService.postJson('/share/' + dir, {
createSharing: {
includeSubfolders: includeSubFolders,
valid
} as CreateSharingDTO
2017-07-03 19:17:49 +02:00
});
}
public updateSharing(dir: string, sharingId: number, includeSubFolders: boolean, password: string, valid: number): Promise<SharingDTO> {
2019-02-22 23:39:01 +01:00
return this.networkService.putJson('/share/' + dir, {
updateSharing: {
2017-07-03 19:17:49 +02:00
id: sharingId,
includeSubfolders: includeSubFolders,
valid,
password
} as CreateSharingDTO
2017-07-03 19:17:49 +02:00
});
}
public getSharingKey(): string {
2017-07-03 19:17:49 +02:00
return this.sharingKey;
}
public isSharing(): boolean {
return this.sharingKey != null;
}
2017-07-09 12:03:17 +02:00
2020-01-07 22:17:54 +01:00
private async getSharing(): Promise<void> {
try {
this.sharingSubject.next(null);
const sharing = await this.networkService.getJson<SharingDTO>('/share/' + this.getSharingKey());
this.sharingSubject.next(sharing);
} catch (e) {
console.error(e);
}
2017-07-09 12:03:17 +02:00
}
2017-07-03 19:17:49 +02:00
}