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
This commit is contained in:
Matt
2025-08-16 17:08:24 -04:00
committed by GitHub
parent 1439e20d93
commit d41f313cff

View File

@@ -5,6 +5,15 @@ import (
"context" "context"
"crypto/md5" "crypto/md5"
"fmt" "fmt"
"image"
"io"
"io/fs"
"net/http"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/evanoberholster/imagemeta" "github.com/evanoberholster/imagemeta"
"github.com/gen2brain/avif" "github.com/gen2brain/avif"
"github.com/gen2brain/heic" "github.com/gen2brain/heic"
@@ -16,13 +25,6 @@ import (
"github.com/sysadminsmedia/homebox/backend/pkgs/utils" "github.com/sysadminsmedia/homebox/backend/pkgs/utils"
"github.com/zeebo/blake3" "github.com/zeebo/blake3"
"golang.org/x/image/draw" "golang.org/x/image/draw"
"image"
"io"
"io/fs"
"net/http"
"path/filepath"
"strings"
"time"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/sysadminsmedia/homebox/backend/internal/data/ent" "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 { 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:///./") { if strings.HasPrefix(r.storage.ConnString, "file:///./") {
dir, err := filepath.Abs(strings.TrimPrefix(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 { if err != nil {
log.Err(err).Msg("failed to get absolute path for attachment directory") log.Err(err).Msg("failed to get absolute path for attachment directory")
return r.storage.ConnString 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 return r.storage.ConnString
} }