Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | 1x 1x 1x 1x 1x 1x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 1x 1x | import { Overlay, OverlayRef } from '@angular/cdk/overlay';
import { TemplatePortal } from '@angular/cdk/portal';
import { HttpErrorResponse } from '@angular/common/http';
import { ChangeDetectorRef, Component, ElementRef, TemplateRef, ViewChild, ViewContainerRef } from '@angular/core';
import { ReactiveFormsModule, UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms';
import { sortBy, uniq } from 'lodash-es';
import { DateTime } from 'luxon';
import { catchError, throwError } from 'rxjs';
import { BackupListComponent } from '../../../component/backup/backup-list/backup-list.component';
import { LoadingComponent } from '../../../component/loading/loading.component';
import { BackupOptions } from '../../../model/backup';
import { BackupRef, BackupService } from '../../../service/api/backup.service';
import { OriginService } from '../../../service/api/origin.service';
import { BookmarkService } from '../../../service/bookmark.service';
import { ModService } from '../../../service/mod.service';
import { Store } from '../../../store/store';
import { scrollToFirstInvalid } from '../../../util/form';
import { ORIGIN_REGEX } from '../../../util/format';
import { printError } from '../../../util/http';
@Component({
selector: 'app-settings-backup-page',
templateUrl: './backup.component.html',
styleUrls: ['./backup.component.scss'],
host: { 'class': 'backup' },
imports: [ReactiveFormsModule, LoadingComponent, BackupListComponent]
})
export class SettingsBackupPage {
@ViewChild('backupButton')
backupButton!: ElementRef<HTMLButtonElement>;
@ViewChild('backupOptions')
backupOptionsTemplate!: TemplateRef<any>;
originForm: UntypedFormGroup;
backupOptionsForm: UntypedFormGroup;
list?: BackupRef[];
uploading = false;
serverError: string[] = [];
backupOrigins: string[] = this.store.origins.list;
backupOptionsRef?: OverlayRef;
constructor(
private mod: ModService,
public store: Store,
private backups: BackupService,
private bookmarks: BookmarkService,
private origins: OriginService,
private fb: UntypedFormBuilder,
private overlay: Overlay,
private viewContainerRef: ViewContainerRef,
private cd: ChangeDetectorRef,
) {
mod.setTitle($localize`Settings: Backup & Restore`);
this.fetchBackups();
this.originForm = fb.group({
origin: [this.origin, [Validators.pattern(ORIGIN_REGEX)]],
olderThan: [DateTime.now().toISO()],
});
this.backupOptionsForm = fb.group({
cache: [false],
ref: [true],
ext: [true],
user: [true],
plugin: [false],
template: [false],
newerThan: [''],
});
this.origins.list()
.subscribe(origins => {
this.backupOrigins = uniq([...this.store.origins.list, ...origins]);
this.cd.markForCheck();
});
}
get origin() {
return this.store.view.origin || this.store.account.origin;
}
selectOrigin(origin: string) {
if (origin === this.origin) return;
this.fetchBackups(origin);
this.bookmarks.origin = origin;
}
fetchBackups(origin?: string) {
delete this.list;
this.backups.list(origin === undefined ? this.origin : origin)
.subscribe(list => this.list = sortBy(list, 'id').reverse());
}
showBackupOptions() {
if (this.backupOptionsRef) return;
const positionStrategy = this.overlay.position()
.flexibleConnectedTo(this.backupButton!)
.withPositions([{
originX: 'start',
originY: 'bottom',
overlayX: 'start',
overlayY: 'top',
offsetY: 4,
}]);
this.backupOptionsRef = this.overlay.create({
hasBackdrop: true,
backdropClass: 'hide',
positionStrategy,
scrollStrategy: this.overlay.scrollStrategies.reposition()
});
this.backupOptionsRef.attach(new TemplatePortal(this.backupOptionsTemplate, this.viewContainerRef));
this.backupOptionsRef.backdropClick().subscribe(() => this.cancelBackup());
}
confirmBackup() {
const options: BackupOptions = {
cache: this.backupOptionsForm.value.cache,
ref: this.backupOptionsForm.value.ref,
ext: this.backupOptionsForm.value.ext,
user: this.backupOptionsForm.value.user,
plugin: this.backupOptionsForm.value.plugin,
template: this.backupOptionsForm.value.template,
newerThan: this.backupOptionsForm.value.newerThan || undefined,
};
this.closeBackupOptions();
this.backup(options);
}
cancelBackup() {
this.closeBackupOptions();
}
closeBackupOptions() {
this.backupOptionsRef?.detach();
this.backupOptionsRef?.dispose();
this.backupOptionsRef = undefined;
}
backup(options: BackupOptions) {
this.serverError = [];
this.backups.create(this.origin, options).pipe(
catchError((res: HttpErrorResponse) => {
this.serverError = printError(res);
return throwError(() => res);
}),
).subscribe(id => {
this.list ||= [];
this.list.unshift({ id: '_' + id });
});
}
upload(files?: FileList) {
this.serverError = [];
if (!files || !files.length) return;
this.uploading = true;
const file = files[0]!;
this.backups.upload(this.origin, file).pipe(
catchError((res: HttpErrorResponse) => {
this.serverError = printError(res);
this.uploading = false;
return throwError(() => res);
}),
).subscribe(() => {
this.uploading = false;
this.list ||= [];
this.list.unshift({ id: files[0].name });
});
}
regen() {
this.serverError = [];
if (!confirm($localize`Are you sure you want totally regenerate metadata${this.origin ? ' in ' + this.origin : ''}?`)) return;
this.backups.regen(this.origin).pipe(
catchError((res: HttpErrorResponse) => {
this.serverError = printError(res);
return throwError(() => res);
}),
).subscribe();
}
deleteOrigin() {
this.serverError = [];
this.originForm.markAllAsTouched();
if (!this.originForm.valid) {
scrollToFirstInvalid();
return;
}
const confirmation = prompt($localize`Are you sure you want totally delete everything in ${this.origin || 'default'}?\n\nEnter the origin to confirm:`);
if (confirmation === null) return;
if (confirmation !== (this.origin || 'default')) {
alert($localize`Origin did not match ${this.origin || 'default'}, aborting.`)
return;
}
const olderThan = DateTime.fromISO(this.originForm.value.olderThan);
this.origins.delete(this.origin, olderThan).pipe(
catchError((res: HttpErrorResponse) => {
this.serverError = printError(res);
return throwError(() => res);
}),
).subscribe();
}
}
|