Skip to content

Commit

Permalink
download single db-files as snapshot too
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Jul 23, 2024
1 parent 1d20ea8 commit a1704ca
Showing 1 changed file with 38 additions and 27 deletions.
65 changes: 38 additions & 27 deletions web/filebrowser_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,50 @@ import (
"strings"
)

func ignoreFileDownload(filename string) bool {
if strings.HasSuffix(filename, ".sqlite-shm") || strings.HasSuffix(filename, ".sqlite-wal") {
// sqlite wal and shared memory
return true
}
return false
}

func isSqliteDatabase(filename string) bool {
return strings.HasSuffix(filename, ".sqlite")
}

func createSqliteSnapshot(filename string) (string, error) {
f, err := os.CreateTemp(os.TempDir(), "backup.sqlite")
if err != nil {
return "", fmt.Errorf("create temp error: %v", err)
}

err = db.BackupSqlite3Database(context.Background(), filename, f.Name())
if err != nil {
return "", fmt.Errorf("backup error from '%s' to '%s': %v", filename, f.Name(), err)
}

return f.Name(), nil
}

func (a *Api) DownloadFile(w http.ResponseWriter, r *http.Request, claims *types.Claims) {
rel_filename, filename, err := a.get_sanitized_filename(r, "filename")
if err != nil {
SendError(w, 500, err.Error())
return
}

maintenance := a.app.MaintenanceMode.Load()
if isSqliteDatabase(filename) && !maintenance {
tmppath, err := createSqliteSnapshot(filename)
if err != nil {
SendError(w, 500, fmt.Sprintf("error creating snapshot of '%s': %v", filename, err))
return
}
defer os.Remove(tmppath)
filename = tmppath
}

f, err := os.Open(filename)
if err != nil {
SendError(w, 500, err.Error())
Expand All @@ -40,7 +77,7 @@ func (a *Api) DownloadFile(w http.ResponseWriter, r *http.Request, claims *types
w.Header().Set("Content-Type", contentType)

if r.URL.Query().Get("download") == "true" {
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", path.Base(filename)))
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", path.Base(rel_filename)))
}

_, err = w.Write(header[:header_size])
Expand All @@ -62,32 +99,6 @@ func (a *Api) DownloadFile(w http.ResponseWriter, r *http.Request, claims *types
}, r)
}

func ignoreFileDownload(filename string) bool {
if strings.HasSuffix(filename, ".sqlite-shm") || strings.HasSuffix(filename, ".sqlite-wal") {
// sqlite wal and shared memory
return true
}
return false
}

func isSqliteDatabase(filename string) bool {
return strings.HasSuffix(filename, ".sqlite")
}

func createSqliteSnapshot(filename string) (string, error) {
f, err := os.CreateTemp(os.TempDir(), "backup.sqlite")
if err != nil {
return "", fmt.Errorf("create temp error: %v", err)
}

err = db.BackupSqlite3Database(context.Background(), filename, f.Name())
if err != nil {
return "", fmt.Errorf("backup error from '%s' to '%s': %v", filename, f.Name(), err)
}

return f.Name(), nil
}

func (a *Api) DownloadZip(w http.ResponseWriter, r *http.Request, claims *types.Claims) {
maintenance := a.app.MaintenanceMode.Load()

Expand Down

0 comments on commit a1704ca

Please sign in to comment.