mirror of
https://github.com/Theodor-Springmann-Stiftung/musenalm.git
synced 2026-02-04 02:25:30 +00:00
FIX: better filenames for the export
This commit is contained in:
@@ -3,9 +3,11 @@ package exports
|
|||||||
import (
|
import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/Theodor-Springmann-Stiftung/musenalm/dbmodels"
|
"github.com/Theodor-Springmann-Stiftung/musenalm/dbmodels"
|
||||||
"github.com/pocketbase/dbx"
|
"github.com/pocketbase/dbx"
|
||||||
@@ -80,7 +82,7 @@ func Run(app core.App, exportID string) error {
|
|||||||
return markFailed(app, record, err)
|
return markFailed(app, record, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
filename := exportID + ".zip"
|
filename := buildExportFilename("data", exportID)
|
||||||
tempPath := filepath.Join(exportDir, filename+".tmp")
|
tempPath := filepath.Join(exportDir, filename+".tmp")
|
||||||
finalPath := filepath.Join(exportDir, filename)
|
finalPath := filepath.Join(exportDir, filename)
|
||||||
|
|
||||||
@@ -267,7 +269,7 @@ func markFailed(app core.App, record *core.Record, err error) error {
|
|||||||
if dirErr == nil {
|
if dirErr == nil {
|
||||||
filename := record.GetString(dbmodels.EXPORT_FILENAME_FIELD)
|
filename := record.GetString(dbmodels.EXPORT_FILENAME_FIELD)
|
||||||
if filename == "" {
|
if filename == "" {
|
||||||
filename = record.Id + ".zip"
|
filename = buildExportFilename("data", record.Id)
|
||||||
}
|
}
|
||||||
filename = filepath.Base(filename)
|
filename = filepath.Base(filename)
|
||||||
_ = os.Remove(filepath.Join(exportDir, filename))
|
_ = os.Remove(filepath.Join(exportDir, filename))
|
||||||
@@ -300,3 +302,16 @@ func safeFilename(name string) string {
|
|||||||
}
|
}
|
||||||
return b.String()
|
return b.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func buildExportFilename(kind, exportID string) string {
|
||||||
|
date := time.Now().Format("2006-01-02")
|
||||||
|
label := "DATA"
|
||||||
|
if kind == "files" {
|
||||||
|
label = "FILES"
|
||||||
|
}
|
||||||
|
idPart := exportID
|
||||||
|
if len(exportID) > 8 {
|
||||||
|
idPart = exportID[:8]
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s-MUSENALM-%s-%s.zip", date, label, idPart)
|
||||||
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ func RunFiles(app core.App, exportID string) error {
|
|||||||
record.Set(dbmodels.EXPORT_PROGRESS_FIELD, 0)
|
record.Set(dbmodels.EXPORT_PROGRESS_FIELD, 0)
|
||||||
record.Set(dbmodels.EXPORT_CURRENT_TABLE_FIELD, "")
|
record.Set(dbmodels.EXPORT_CURRENT_TABLE_FIELD, "")
|
||||||
record.Set(dbmodels.EXPORT_ERROR_FIELD, "")
|
record.Set(dbmodels.EXPORT_ERROR_FIELD, "")
|
||||||
record.Set(dbmodels.EXPORT_FILENAME_FIELD, exportID+"-files.zip")
|
record.Set(dbmodels.EXPORT_FILENAME_FIELD, buildExportFilename("files", exportID))
|
||||||
if err := app.Save(record); err != nil {
|
if err := app.Save(record); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -49,7 +49,7 @@ func RunFiles(app core.App, exportID string) error {
|
|||||||
return markFailed(app, record, err)
|
return markFailed(app, record, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
filename := exportID + "-files.zip"
|
filename := buildExportFilename("files", exportID)
|
||||||
tempPath := filepath.Join(exportDir, filename+".tmp")
|
tempPath := filepath.Join(exportDir, filename+".tmp")
|
||||||
finalPath := filepath.Join(exportDir, filename)
|
finalPath := filepath.Join(exportDir, filename)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user