package Auth
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
|
|
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Util"
|
|
)
|
|
|
|
// AddProfileImage adds a profile image
|
|
func AddProfileImage(w http.ResponseWriter, r *http.Request) {
|
|
var (
|
|
user Database.User
|
|
attachment Database.Attachment
|
|
decodedFile []byte
|
|
fileName string
|
|
err error
|
|
)
|
|
|
|
// Ignore error here, as middleware should handle auth
|
|
user, _ = CheckCookieCurrentUser(w, r)
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&attachment)
|
|
if err != nil {
|
|
http.Error(w, "Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if attachment.Data == "" {
|
|
http.Error(w, "Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
decodedFile, err = base64.StdEncoding.DecodeString(attachment.Data)
|
|
if err != nil {
|
|
http.Error(w, "Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
fileName, err = Util.WriteFile(decodedFile)
|
|
if err != nil {
|
|
http.Error(w, "Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
attachment.FilePath = fileName
|
|
|
|
user.Attachment = attachment
|
|
|
|
err = (&user).UpdateUser()
|
|
if err != nil {
|
|
http.Error(w, "Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|