package Auth
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Database"
|
|
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Models"
|
|
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Util"
|
|
)
|
|
|
|
// AddProfileImage adds a profile image
|
|
func AddProfileImage(w http.ResponseWriter, r *http.Request) {
|
|
var (
|
|
user Models.User
|
|
attachment Models.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)
|
|
fileName, err = Util.WriteFile(decodedFile)
|
|
attachment.FilePath = fileName
|
|
|
|
user.Attachment = attachment
|
|
|
|
err = Database.UpdateUser(user.ID.String(), &user)
|
|
if err != nil {
|
|
http.Error(w, "Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|