Skip to content

Commit

Permalink
fix: check if "rc.Serve" is enabled in server options before download…
Browse files Browse the repository at this point in the history
…ing file
  • Loading branch information
ElonH committed Jun 10, 2020
1 parent 1f578f7 commit 6a484ca
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 12 deletions.
24 changes: 18 additions & 6 deletions src/app/pages/manager/fileMode/download-file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import {
DownloadFileFlow,
DownloadFileFlowInNode,
DownloadFileFlowParamsNode,
NestedGet,
} from '../../../@dataflow/rclone';
import { ConnectionService } from '../../connection.service';
import { ServerSettingService } from '../../settings/sever-setting/server-setting.service';

@Injectable({
providedIn: 'root',
Expand All @@ -26,17 +28,27 @@ export class DownloadFileService {
constructor(
private cmdService: ConnectionService,
private toastr: NbToastrService,
private fileSaverService: FileSaverService
private fileSaverService: FileSaverService,
private serverSettingService: ServerSettingService
) {
const outer = this;
this.download$ = new (class extends DownloadFileFlow {
public prerequest$: Observable<CombErr<DownloadFileFlowInNode>> = outer.trigger.pipe(
withLatestFrom(outer.cmdService.connection$.getOutput()),
withLatestFrom(
outer.cmdService.connection$.getOutput(),
outer.serverSettingService.options$.getOutput()
),
map(
([item, connectNode]): CombErr<DownloadFileFlowInNode> => [
{ ...connectNode[0], ...item },
connectNode[1],
]
([item, connectNode, serverSettingNode]): CombErr<DownloadFileFlowInNode> => {
if (serverSettingNode[1].length !== 0) return [{}, serverSettingNode[1]] as any;
if (NestedGet(serverSettingNode[0].options, 'rc', 'Serve'))
return [{ ...connectNode[0], ...item }, connectNode[1]];
else
return [
{},
[new Error('rc-serve is closed. (Tip: Set server option: rc.Serve as true)')],
] as any;
}
)
);
})();
Expand Down
102 changes: 96 additions & 6 deletions src/app/pages/manager/fileMode/file.detail.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,54 @@
import { Component, OnInit } from '@angular/core';
import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
import { NbToastrService } from '@nebular/theme';
import { overlayConfigFactory } from 'ngx-modialog-7';
// tslint:disable-next-line: no-submodule-imports
import { Modal, VEXModalContext } from 'ngx-modialog-7/plugins/vex';
import { Subject } from 'rxjs';
import { withLatestFrom } from 'rxjs/operators';
import { OperationsListExtendsFlowOutItemNode } from '../../../@dataflow/extra';
import { NestedGet } from '../../../@dataflow/rclone';
import { ServerSettingService } from '../../settings/sever-setting/server-setting.service';
import { DownloadFileService } from './download-file.service';

@Component({
selector: 'app-file-file-detail',
template: `
<div *ngIf="!currentDirDetail">
<div class="detail" *ngIf="!currentDirDetail">
<img [src]="'assets/icons/' + typeIcon" />
<h5>{{ name }}</h5>
<button nbButton (click)="download()" *ngIf="!isDir">download</button>
</div>
<ng-template #EnableServe let-dialogRef="dialogRef" let-ctx="dialogRef.context">
<div class="vex-dialog-message">
<h3>Are you want to enable "rc.Serve"?</h3>
<span style="padding: 1rem 0.5rem;">
Download featrue needs to enable server option "rc.Serve".
</span>
<p style="text-indent: 1rem;">
Nb: You can manually close it in "server setting" page after download completed.
</p>
</div>
<div class="vex-dialog-buttons">
<button
type="button"
class="vex-dialog-button vex-dialog-button-primary vex-first"
(click)="dialogRef.close(true)"
>
OK
</button>
<button
type="button"
class="vex-dialog-button vex-dialog-button-secondary vex-last"
(click)="dialogRef.close(false)"
>
Cancel
</button>
</div>
</ng-template>
`,
styles: [
`
div {
div.detail {
padding: 0 1.25rem;
display: flex;
flex-direction: column;
Expand All @@ -25,11 +60,20 @@ import { DownloadFileService } from './download-file.service';
h5 {
overflow-wrap: break-word;
}
.vex-dialog-message {
display: flex;
flex-direction: column;
}
`,
],
})
export class FileDetailComponent implements OnInit {
constructor(private downloadService: DownloadFileService) {}
constructor(
private downloadService: DownloadFileService,
private serverSettingService: ServerSettingService,
public modal: Modal,
private toastrService: NbToastrService
) {}

/** if user wasn't select any item, right sidebar can show current directory detail. */
currentDirDetail = true;
Expand All @@ -40,6 +84,10 @@ export class FileDetailComponent implements OnInit {
isDir = true;
typeIcon = '';

private downloadTrigger = new Subject<number>();

@ViewChild('EnableServe') public EnableServe: TemplateRef<any>;

itemNode(x: OperationsListExtendsFlowOutItemNode) {
this.remote = x.remote || '';
this.path = x.Path || '';
Expand All @@ -50,8 +98,50 @@ export class FileDetailComponent implements OnInit {
}

download() {
this.downloadService.post({ remote: this.remote, Path: this.path, Name: this.name });
this.downloadTrigger.next(1);
}

private realDownload() {
this.downloadService.post({
remote: this.remote,
Path: this.path,
Name: this.name,
});
}

ngOnInit() {}
ngOnInit() {
this.downloadTrigger
.pipe(withLatestFrom(this.serverSettingService.options$.getOutput()))
.subscribe(([, optionsNode]) => {
if (NestedGet(optionsNode[0].options, 'rc', 'Serve'))
this.modal
.confirm()
.className('flat-attack')
.message(`Are you sure want to download ${this.name} ?`)
.isBlocking(false)
.open()
.result.then(
x => {
if (x) this.realDownload();
},
() => {}
);
else {
this.modal
.open(
this.EnableServe,
overlayConfigFactory({ isBlocking: false, className: 'flat-attack' }, VEXModalContext)
)
.result.then(
x => {
if (x) {
this.serverSettingService.setOption({ rc: { Serve: true } });
this.toastrService.default('Download', 'Try to download again.');
}
},
() => {}
);
}
});
}
}

0 comments on commit 6a484ca

Please sign in to comment.