From 0d50bcfee6d1025f368e17fd6e98dbfa6e1ff36f Mon Sep 17 00:00:00 2001 From: Hein Date: Tue, 24 Mar 2026 09:02:17 +0200 Subject: [PATCH] fix(provider): enhance file opening logic with alternate path. Handling broken cases to be compatible with Bitech clients * Implemented alternate path handling for file retrieval * Improved error messaging for file not found scenarios --- pkg/server/staticweb/providers/embed.go | 16 +++++++++++++++- pkg/server/staticweb/providers/local.go | 17 ++++++++++++++++- pkg/server/staticweb/providers/zip.go | 17 ++++++++++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/pkg/server/staticweb/providers/embed.go b/pkg/server/staticweb/providers/embed.go index 651cc41..fbcdcab 100644 --- a/pkg/server/staticweb/providers/embed.go +++ b/pkg/server/staticweb/providers/embed.go @@ -98,6 +98,7 @@ func (p *EmbedFSProvider) Open(name string) (fs.File, error) { // Apply prefix stripping by prepending the prefix to the requested path actualPath := name + alternatePath := "" if p.stripPrefix != "" { // Clean the paths to handle leading/trailing slashes prefix := strings.Trim(p.stripPrefix, "/") @@ -105,12 +106,25 @@ func (p *EmbedFSProvider) Open(name string) (fs.File, error) { if prefix != "" { actualPath = path.Join(prefix, cleanName) + alternatePath = cleanName } else { actualPath = cleanName } } + // First try the actual path with prefix + if file, err := p.fs.Open(actualPath); err == nil { + return file, nil + } - return p.fs.Open(actualPath) + // If alternate path is different, try it as well + if alternatePath != "" && alternatePath != actualPath { + if file, err := p.fs.Open(alternatePath); err == nil { + return file, nil + } + } + + // If both attempts fail, return the error from the first attempt + return nil, fmt.Errorf("file not found: %s", name) } // Close releases any resources held by the provider. diff --git a/pkg/server/staticweb/providers/local.go b/pkg/server/staticweb/providers/local.go index 24f718f..d2662a5 100644 --- a/pkg/server/staticweb/providers/local.go +++ b/pkg/server/staticweb/providers/local.go @@ -53,6 +53,7 @@ func (p *LocalFSProvider) Open(name string) (fs.File, error) { // Apply prefix stripping by prepending the prefix to the requested path actualPath := name + alternatePath := "" if p.stripPrefix != "" { // Clean the paths to handle leading/trailing slashes prefix := strings.Trim(p.stripPrefix, "/") @@ -60,12 +61,26 @@ func (p *LocalFSProvider) Open(name string) (fs.File, error) { if prefix != "" { actualPath = path.Join(prefix, cleanName) + alternatePath = cleanName } else { actualPath = cleanName } } - return p.fs.Open(actualPath) + // First try the actual path with prefix + if file, err := p.fs.Open(actualPath); err == nil { + return file, nil + } + + // If alternate path is different, try it as well + if alternatePath != "" && alternatePath != actualPath { + if file, err := p.fs.Open(alternatePath); err == nil { + return file, nil + } + } + + // If both attempts fail, return the error from the first attempt + return nil, fmt.Errorf("file not found: %s", name) } // Close releases any resources held by the provider. diff --git a/pkg/server/staticweb/providers/zip.go b/pkg/server/staticweb/providers/zip.go index 363448c..37e2a77 100644 --- a/pkg/server/staticweb/providers/zip.go +++ b/pkg/server/staticweb/providers/zip.go @@ -56,6 +56,7 @@ func (p *ZipFSProvider) Open(name string) (fs.File, error) { // Apply prefix stripping by prepending the prefix to the requested path actualPath := name + alternatePath := "" if p.stripPrefix != "" { // Clean the paths to handle leading/trailing slashes prefix := strings.Trim(p.stripPrefix, "/") @@ -63,12 +64,26 @@ func (p *ZipFSProvider) Open(name string) (fs.File, error) { if prefix != "" { actualPath = path.Join(prefix, cleanName) + alternatePath = cleanName } else { actualPath = cleanName } } - return p.zipFS.Open(actualPath) + // First try the actual path with prefix + if file, err := p.zipFS.Open(actualPath); err == nil { + return file, nil + } + + // If alternate path is different, try it as well + if alternatePath != "" && alternatePath != actualPath { + if file, err := p.zipFS.Open(alternatePath); err == nil { + return file, nil + } + } + + // If both attempts fail, return the error from the first attempt + return nil, fmt.Errorf("file not found: %s", name) } // Close releases resources held by the zip reader.