Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow dumping and restoring via two separate buttons in the UI #38

Merged
merged 1 commit into from
Nov 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"restic-browser/backend/open"
"restic-browser/backend/restic"

"github.com/leaanthony/sail/fs"
"github.com/pkg/errors"
"github.com/wailsapp/wails/v2/pkg/runtime"
"golang.org/x/text/encoding/unicode"
Expand Down Expand Up @@ -46,6 +48,21 @@ func (r *ResticBrowserApp) showError(title, message string) {
r.showMessage(title, message, "error")
}

func (r *ResticBrowserApp) showConfirmation(title, message string) bool {
messageOptions := runtime.MessageDialogOptions{
Type: "question",
Title: title,
Message: message,
Buttons: []string{"Yes", "No"},
}
result, err := runtime.MessageDialog(*r.context, messageOptions)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to show confirmation message: %s\n", err.Error())
return false
}
return (result == "Yes")
}

func (r *ResticBrowserApp) showMessage(title, message string, messageType runtime.DialogType) {
messageOptions := runtime.MessageDialogOptions{
Type: messageType,
Expand Down Expand Up @@ -274,6 +291,28 @@ func (r *ResticBrowserApp) RestoreFile(snapshotID string, file *restic.File) (st
if targetPath == "" {
return "", nil
}

targetFileOrFolderName := filepath.Join(targetPath, file.Path)
if file.Type == "dir" {
if fs.DirExists(targetFileOrFolderName) {
if !r.showConfirmation("Overwrite existing directory?",
fmt.Sprintf("The target directory '%s' already exists.\n\n"+
"Are you sure that you want to overwrite the existing directory?",
targetFileOrFolderName)) {
return "", fmt.Errorf("target directory '%s' already exists", targetFileOrFolderName)
}
}
} else {
if fs.FileExists(targetFileOrFolderName) {
if !r.showConfirmation("Overwrite existing file?",
fmt.Sprintf("The target file '%s' already exists.\n\n"+
"Are you sure that you want to overwrite the existing file?",
targetFileOrFolderName)) {
return "", fmt.Errorf("target directory '%s' already exists", targetFileOrFolderName)
}
}
}

targetFilePath, err := r.repo.RestoreFile(snapshot, file, targetPath)
if err != nil {
return "", err
Expand Down Expand Up @@ -303,8 +342,21 @@ func (r *ResticBrowserApp) DumpFile(snapshotID string, file *restic.File) (strin
if targetPath == "" {
return "", nil
}
var targetFilePath string
targetFilePath, err = r.repo.DumpFile(snapshot, file, targetPath)

targetFileName := filepath.Join(targetPath, file.Name)
if file.Type == "dir" {
targetFileName += ".zip"
}
if fs.FileExists(targetFileName) {
if !r.showConfirmation("Overwrite existing file?",
fmt.Sprintf("The target file '%s' already exists.\n\n"+
"Are you sure that you want to overwrite the existing file?",
targetFileName)) {
return "", fmt.Errorf("target file '%s' already exists", targetFileName)
}
}

targetFilePath, err := r.repo.DumpFile(snapshot, file, targetPath)
if err != nil {
return "", err
}
Expand Down
20 changes: 6 additions & 14 deletions backend/restic/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,12 @@ func (r *Repository) GetFiles(snapshot *Snapshot, path string) ([]*File, error)
}

func (r *Repository) RestoreFile(snapshot *Snapshot, file *File, targetPath string) (string, error) {
// check if the target already exists
targetFileOrFolderName := filepath.Join(targetPath, file.Name)
if file.Type == "dir" {
if fs.DirExists(targetFileOrFolderName) {
return "", fmt.Errorf("target directory '%s' already exists", targetFileOrFolderName)
}
} else {
if fs.FileExists(targetFileOrFolderName) {
return "", fmt.Errorf("target file '%s' already exists", targetFileOrFolderName)
}
}
// restore the file or foolder

_, stderr, code, _ := r.run("restore", snapshot.ID, "--target", targetPath, "--include", file.Path)
if code != 0 {
return "", fmt.Errorf(stderr)
}
return targetFileOrFolderName, nil
return filepath.Join(targetPath, file.Path), nil
}

func (r *Repository) DumpFile(snapshot *Snapshot, file *File, targetPath string) (string, error) {
Expand All @@ -131,7 +120,10 @@ func (r *Repository) DumpFile(snapshot *Snapshot, file *File, targetPath string)
targetFileName += ".zip"
}
if fs.FileExists(targetFileName) {
return "", fmt.Errorf("target file '%s' already exists", targetFileName)
err := os.Remove(targetFileName)
if err != nil {
return "", fmt.Errorf("failed to remove file '%s': %s", targetFileName, err)
}
}
// open target file
targetFile, err := os.Create(targetFileName)
Expand Down
23 changes: 17 additions & 6 deletions frontend/src/components/file-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,17 +274,26 @@ export class ResticBrowserFileList extends MobxLitElement {
_column: GridColumn<restic.File>,
model: GridItemModel<restic.File>
) {
const downloadButton = html`
const dumpButton = html`
<vaadin-button
.tabindex=${null}
title="Restore file/folder contents"
title="Dump file/folder contents as zip file"
theme="small secondary icon"
style="height: 1.5rem; margin: unset; padding: 0;"
@click=${() => this._restoreFile(model.item)}>
@click=${() => this._dumpFile(model.item)}>
<vaadin-icon icon="vaadin:download"></vaadin-icon>
</vaadin-button>
`;
if (model.item.type === "dir") {
const restoreButton = html`
<vaadin-button
.tabindex=${null}
title="Restore file/folder contents"
theme="small secondary icon"
style="height: 1.5rem; margin: unset; padding: 0;"
@click=${() => this._restoreFile(model.item)}>
<vaadin-icon icon="lumo:undo"></vaadin-icon>
</vaadin-button>
`;if (model.item.type === "dir") {
const setRootpathButton = html`
<vaadin-button
.tabindex=${null}
Expand All @@ -302,7 +311,8 @@ export class ResticBrowserFileList extends MobxLitElement {
} else {
render(html`
${setRootpathButton}
${downloadButton}
${dumpButton}
${restoreButton}
`, root);
}
} else {
Expand All @@ -315,7 +325,8 @@ export class ResticBrowserFileList extends MobxLitElement {
@click=${() => this._openFile(model.item)}>
<vaadin-icon icon="lumo:eye"></vaadin-icon>
</vaadin-button>
${downloadButton}
${dumpButton}
${restoreButton}
`, root)
}
}
Expand Down