From c63fbd4ccc5e7a11c4ce15fe7d19a3daf4e5c45e Mon Sep 17 00:00:00 2001 From: JoramWilander Date: Fri, 17 Jul 2015 15:55:06 -0400 Subject: [PATCH] add proper url encoding for filenames --- api/file.go | 5 ++++- utils/urlencode.go | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 utils/urlencode.go diff --git a/api/file.go b/api/file.go index 4fead46271..2abaca7091 100644 --- a/api/file.go +++ b/api/file.go @@ -103,7 +103,9 @@ func uploadFile(c *Context, w http.ResponseWriter, r *http.Request) { imageDataList = append(imageDataList, buf.Bytes()) } - fileUrl := "/" + channelId + "/" + c.Session.UserId + "/" + uid + "/" + url.QueryEscape(files[i].Filename) + encName := utils.UrlEncode(files[i].Filename) + + fileUrl := "/" + channelId + "/" + c.Session.UserId + "/" + uid + "/" + encName resStruct.Filenames = append(resStruct.Filenames, fileUrl) } @@ -264,6 +266,7 @@ func asyncGetFile(path string, fileData chan []byte) { go func() { data, getErr := readFile(path) if getErr != nil { + l4g.Error(getErr) fileData <- nil } else { fileData <- data diff --git a/utils/urlencode.go b/utils/urlencode.go new file mode 100644 index 0000000000..63a8f7880d --- /dev/null +++ b/utils/urlencode.go @@ -0,0 +1,19 @@ +// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved. +// See License.txt for license information. + +package utils + +import ( + "net/url" + "strings" +) + +func UrlEncode(str string) string { + strs := strings.Split(str, " ") + + for i, s := range strs { + strs[i] = url.QueryEscape(s) + } + + return strings.Join(strs, "%20") +}