From d41f313cff14299220634898ae55fe018fc5ac28 Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 16 Aug 2025 17:08:24 -0400 Subject: [PATCH] Fix Windows Paths (#917) * In theory this should fix the issue with Windows paths * Fix Windows path handling in file storage connections for non-default --- .../data/repo/repo_item_attachments.go | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/backend/internal/data/repo/repo_item_attachments.go b/backend/internal/data/repo/repo_item_attachments.go index 9791a855..362c7f2c 100644 --- a/backend/internal/data/repo/repo_item_attachments.go +++ b/backend/internal/data/repo/repo_item_attachments.go @@ -5,6 +5,15 @@ import ( "context" "crypto/md5" "fmt" + "image" + "io" + "io/fs" + "net/http" + "path/filepath" + "runtime" + "strings" + "time" + "github.com/evanoberholster/imagemeta" "github.com/gen2brain/avif" "github.com/gen2brain/heic" @@ -16,13 +25,6 @@ import ( "github.com/sysadminsmedia/homebox/backend/pkgs/utils" "github.com/zeebo/blake3" "golang.org/x/image/draw" - "image" - "io" - "io/fs" - "net/http" - "path/filepath" - "strings" - "time" "github.com/google/uuid" "github.com/sysadminsmedia/homebox/backend/internal/data/ent" @@ -100,13 +102,30 @@ func (r *AttachmentRepo) path(gid uuid.UUID, hash string) string { } func (r *AttachmentRepo) GetConnString() string { + // Handle the default case for file storage + // which is file:///./ meaning relative to the current working directory if strings.HasPrefix(r.storage.ConnString, "file:///./") { dir, err := filepath.Abs(strings.TrimPrefix(r.storage.ConnString, "file:///./")) + if runtime.GOOS == "windows" { + dir = fmt.Sprintf("/%s", dir) + } if err != nil { log.Err(err).Msg("failed to get absolute path for attachment directory") return r.storage.ConnString } - return fmt.Sprintf("file://%s?no_tmp_dir=true", dir) + return strings.ReplaceAll(fmt.Sprintf("file://%s?no_tmp_dir=true", dir), "\\", "/") + } else if strings.HasPrefix(r.storage.ConnString, "file://") { + // Handle the case for file storage with an absolute path + // Convert Windows paths to a format compatible with fileblob + // e.g. file:///C:/path/to/file becomes file:///C/path + dir := strings.TrimPrefix(strings.ReplaceAll(r.storage.ConnString, "\\", "/"), "file://") + if runtime.GOOS == "windows" { + // Remove the colon from the drive letter (in case the user adds it) + dir = strings.ReplaceAll(dir, ":", "") + // Ensure the path starts with a slash for Windows compatibility + dir = fmt.Sprintf("/%s", dir) + } + return fmt.Sprintf("file://%s", dir) } return r.storage.ConnString }