fix: url encode avatar filename when showing path

fixes #61
This commit is contained in:
Jordan Knott 2020-09-12 18:12:12 -05:00
parent 52c60abcd7
commit fe84f97f18

View File

@ -5,7 +5,9 @@ import (
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url"
"os" "os"
"strings"
"github.com/google/uuid" "github.com/google/uuid"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -48,22 +50,24 @@ func (h *TaskcafeHandler) ProfileImageUpload(w http.ResponseWriter, r *http.Requ
return return
} }
defer file.Close() defer file.Close()
log.WithFields(log.Fields{"filename": handler.Filename, "size": handler.Size, "header": handler.Header}).Info("file metadata") filename := strings.ReplaceAll(handler.Filename, " ", "-")
encodedFilename := url.QueryEscape(filename)
log.WithFields(log.Fields{"filename": encodedFilename, "size": handler.Size, "header": handler.Header}).Info("file metadata")
fileBytes, err := ioutil.ReadAll(file) fileBytes, err := ioutil.ReadAll(file)
if err != nil { if err != nil {
log.WithError(err).Error("while reading file") log.WithError(err).Error("while reading file")
return return
} }
err = ioutil.WriteFile("uploads/"+handler.Filename, fileBytes, 0644) err = ioutil.WriteFile("uploads/"+filename, fileBytes, 0644)
if err != nil { if err != nil {
log.WithError(err).Error("while reading file") log.WithError(err).Error("while reading file")
return return
} }
h.repo.UpdateUserAccountProfileAvatarURL(r.Context(), db.UpdateUserAccountProfileAvatarURLParams{UserID: userID, ProfileAvatarUrl: sql.NullString{String: "/uploads/" + handler.Filename, Valid: true}}) h.repo.UpdateUserAccountProfileAvatarURL(r.Context(), db.UpdateUserAccountProfileAvatarURLParams{UserID: userID, ProfileAvatarUrl: sql.NullString{String: "/uploads/" + encodedFilename, Valid: true}})
// return that we have successfully uploaded our file! // return that we have successfully uploaded our file!
log.Info("file uploaded") log.Info("file uploaded")
json.NewEncoder(w).Encode(AvatarUploadResponseData{URL: "/uploads/" + handler.Filename, UserID: userID.String()}) json.NewEncoder(w).Encode(AvatarUploadResponseData{URL: "/uploads/" + encodedFilename, UserID: userID.String()})
} }