fix escape

This commit is contained in:
Mukhtar Akere
2025-04-17 16:31:39 +01:00
parent 1983e27124
commit 8c78da3f69
5 changed files with 18 additions and 13 deletions

View File

@@ -1,2 +1,12 @@
package utils
import "strings"
func EscapePath(path string) string {
// escape %
escapedPath := strings.ReplaceAll(path, "%", "%25")
// add others
return escapedPath
}

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"github.com/sirrobot01/decypharr/internal/config"
"github.com/sirrobot01/decypharr/internal/request"
"github.com/sirrobot01/decypharr/internal/utils"
"github.com/sirrobot01/decypharr/pkg/debrid/types"
"io"
"net/http"
@@ -23,8 +24,8 @@ type fileInfo struct {
isDir bool
}
func (fi *fileInfo) Name() string { return url.PathEscape(fi.name) }
func (fi *fileInfo) RawName() string { return fi.name }
func (fi *fileInfo) Name() string { return utils.EscapePath(fi.name) }
func (fi *fileInfo) RawName() string { return url.PathEscape(fi.name) }
func (fi *fileInfo) Size() int64 { return fi.size }
func (fi *fileInfo) Mode() os.FileMode { return fi.mode }
func (fi *fileInfo) ModTime() time.Time { return fi.modTime }

View File

@@ -1,6 +1,7 @@
package webdav
import (
"github.com/sirrobot01/decypharr/internal/utils"
"net/url"
"os"
"time"
@@ -15,8 +16,8 @@ type FileInfo struct {
isDir bool
}
func (fi *FileInfo) Name() string { return url.PathEscape(fi.name) }
func (fi *FileInfo) RawName() string { return fi.name }
func (fi *FileInfo) Name() string { return utils.EscapePath(fi.name) } // uses minimal escaping
func (fi *FileInfo) RawName() string { return url.PathEscape(fi.name) } // uses full escaping
func (fi *FileInfo) Size() int64 { return fi.size }
func (fi *FileInfo) Mode() os.FileMode { return fi.mode }
func (fi *FileInfo) ModTime() time.Time { return fi.modTime }

View File

@@ -14,7 +14,6 @@ import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
path "path/filepath"
"slices"
@@ -104,12 +103,6 @@ func (h *Handler) getParentFiles() []os.FileInfo {
func (h *Handler) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (webdav.File, error) {
name = path.Clean("/" + name)
// unescape the path
name, err := url.PathUnescape(name)
if err != nil {
h.logger.Error().Err(err).Msg("Failed to unescape path")
return nil, err
}
rootDir := h.getRootPath()
metadataOnly := ctx.Value("metadataOnly") != nil

View File

@@ -113,9 +113,9 @@ const directoryTemplate = `
{{end}}
{{range $index, $file := .Children}}
<li>
<a href="{{(printf "%s/%s" $.Path $file.Name)}}">
<a href="{{(printf "%s/%s" $.Path $file.RawName )}}">
<span class="file-number">{{add $index 1}}.</span>
<span class="file-name">{{$file.RawName}}{{if $file.IsDir}}/{{end}}</span>
<span class="file-name">{{$file.Name}}{{if $file.IsDir}}/{{end}}</span>
<span class="file-info">
{{if not $file.IsDir}}
{{formatSize $file.Size}}