Support for special characters in filename.

Previously, spaces or other special characters in filenames were borked
because of how the filenames were passed through to the ViewFile
function - with escaped strings (%020 instead of " "). Added a
QueryUnescape call, and slightly tweaked the router to use a filename
rather than accessing var everytime.
This commit is contained in:
Gabriel Simmer 2020-04-03 21:17:43 +01:00
parent 0902f8a429
commit 59c79fc4fe
No known key found for this signature in database
GPG key ID: 33BA4D83B160A0A9

View file

@ -6,6 +6,7 @@ import (
"github.com/gmemstr/nas/files"
"github.com/gorilla/mux"
"net/http"
"net/url"
"sort"
"strings"
)
@ -20,18 +21,25 @@ func HandleProvider() Handler {
if r.Method == "GET" {
fileList := provider.GetDirectory("")
if vars["file"] != "" {
fileType := provider.DetermineType(vars["file"])
filename, err := url.QueryUnescape(vars["file"])
if err != nil {
return &HTTPError{
Message: fmt.Sprintf("error determining filetype for %s\n", filename),
StatusCode: http.StatusInternalServerError,
}
}
fileType := provider.DetermineType(filename)
if fileType == "" {
return &HTTPError{
Message: fmt.Sprintf("error determining filetype for %s\n", vars["file"]),
Message: fmt.Sprintf("error determining filetype for %s\n", filename),
StatusCode: http.StatusInternalServerError,
}
}
if fileType == "file" {
provider.ViewFile(vars["file"], w)
provider.ViewFile(filename, w)
return nil
}
fileList = provider.GetDirectory(vars["file"])
fileList = provider.GetDirectory(filename)
}
data, err := json.Marshal(fileList)
if err != nil {