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

116 lines
3.5 KiB
TypeScript
Raw Normal View History

2017-07-09 18:03:17 +08:00
import {Component, OnDestroy, OnInit, ViewChild} from "@angular/core";
2017-07-04 01:17:49 +08:00
import {Utils} from "../../../../common/Utils";
import {ShareService} from "../share.service";
import {GalleryService} from "../gallery.service";
import {ContentWrapper} from "../../../../common/entities/ConentWrapper";
import {SharingDTO} from "../../../../common/entities/SharingDTO";
2017-07-09 18:03:17 +08:00
import {ModalDirective} from "ngx-bootstrap/modal";
import {Config} from "../../../../common/config/public/Config";
import {NotificationService} from "../../model/notification.service";
2017-07-20 02:47:09 +08:00
import {DirectoryDTO} from "../../../../common/entities/DirectoryDTO";
2018-03-30 08:30:23 +08:00
import {I18n} from "@ngx-translate/i18n-polyfill";
2017-07-09 18:03:17 +08:00
2017-07-04 01:17:49 +08:00
@Component({
selector: 'gallery-share',
templateUrl: './share.gallery.component.html',
styleUrls: ['./share.gallery.component.css'],
})
export class GalleryShareComponent implements OnInit, OnDestroy {
2017-07-09 18:03:17 +08:00
@ViewChild('shareModal') public childModal: ModalDirective;
2017-07-04 01:17:49 +08:00
enabled: boolean = true;
url: string = "";
input = {
includeSubfolders: true,
valid: {
amount: 30,
type: ValidityTypes.Days
2017-07-10 04:36:25 +08:00
},
password: ""
2017-07-04 01:17:49 +08:00
};
currentDir: string = "";
2017-07-28 06:04:19 +08:00
sharing: SharingDTO = null;
2017-07-04 01:17:49 +08:00
contentSubscription = null;
2017-07-09 18:03:17 +08:00
passwordProtection = false;
2018-02-04 08:50:42 +08:00
ValidityTypes: any;
2017-07-04 01:17:49 +08:00
2017-07-09 18:03:17 +08:00
constructor(private _sharingService: ShareService,
public _galleryService: GalleryService,
2018-03-30 08:30:23 +08:00
private _notification: NotificationService,
public i18n: I18n) {
2018-02-04 08:50:42 +08:00
this.ValidityTypes = ValidityTypes;
2017-07-04 01:17:49 +08:00
}
ngOnInit() {
this.contentSubscription = this._galleryService.content.subscribe((content: ContentWrapper) => {
this.enabled = !!content.directory;
if (!this.enabled) {
return;
}
2017-07-20 02:47:09 +08:00
this.currentDir = Utils.concatUrls((<DirectoryDTO>content.directory).path, (<DirectoryDTO>content.directory).name);
2017-07-04 01:17:49 +08:00
});
2017-07-09 18:03:17 +08:00
this.passwordProtection = Config.Client.Sharing.passwordProtected;
2017-07-04 01:17:49 +08:00
}
ngOnDestroy() {
if (this.contentSubscription !== null) {
this.contentSubscription.unsubscribe();
}
}
calcValidity() {
switch (parseInt(this.input.valid.type.toString())) {
case ValidityTypes.Minutes:
return this.input.valid.amount * 1000 * 60;
case ValidityTypes.Hours:
return this.input.valid.amount * 1000 * 60 * 60;
case ValidityTypes.Days:
return this.input.valid.amount * 1000 * 60 * 60 * 24;
case ValidityTypes.Months:
return this.input.valid.amount * 1000 * 60 * 60 * 24 * 30;
}
2018-03-30 08:30:23 +08:00
throw new Error("unknown type: " + this.input.valid.type);
2017-07-04 01:17:49 +08:00
}
async update() {
2017-07-28 06:04:19 +08:00
if (this.sharing == null) {
return;
}
2017-07-04 01:17:49 +08:00
this.url = "loading..";
2017-07-10 04:36:25 +08:00
this.sharing = await this._sharingService.updateSharing(this.currentDir, this.sharing.id, this.input.includeSubfolders, this.input.password, this.calcValidity());
2017-07-09 18:03:17 +08:00
this.url = Config.Client.publicUrl + "/share/" + this.sharing.sharingKey
2017-07-04 01:17:49 +08:00
}
async get() {
this.url = "loading..";
2017-07-09 18:03:17 +08:00
this.sharing = await this._sharingService.createSharing(this.currentDir, this.input.includeSubfolders, this.calcValidity());
this.url = Config.Client.publicUrl + "/share/" + this.sharing.sharingKey
}
async showModal() {
await this.get();
2018-02-04 08:50:42 +08:00
this.input.password = "";
2017-07-09 18:03:17 +08:00
this.childModal.show();
2018-02-04 08:50:42 +08:00
document.body.style.paddingRight = "0px";
2017-07-09 18:03:17 +08:00
}
onCopy() {
2018-03-30 08:30:23 +08:00
this._notification.success(this.i18n("Url has been copied to clipboard"));
2017-07-04 01:17:49 +08:00
}
2017-07-30 15:32:24 +08:00
public hideModal() {
2017-07-30 05:39:06 +08:00
this.childModal.hide();
this.sharing = null;
}
2017-07-04 01:17:49 +08:00
}
2017-07-30 05:39:06 +08:00
export enum ValidityTypes {
2017-07-04 01:17:49 +08:00
Minutes = 0, Hours = 1, Days = 2, Months = 3
}