2020-07-05 01:02:57 +02:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2020-07-16 01:20:08 +02:00
|
|
|
"os"
|
2020-07-05 01:02:57 +02:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-07-16 01:20:08 +02:00
|
|
|
"time"
|
|
|
|
|
2020-08-07 03:50:35 +02:00
|
|
|
"github.com/jordanknott/taskcafe/internal/db"
|
|
|
|
"github.com/jordanknott/taskcafe/internal/frontend"
|
2020-09-11 20:57:02 +02:00
|
|
|
"github.com/jordanknott/taskcafe/internal/utils"
|
2020-07-05 01:02:57 +02:00
|
|
|
)
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// Frontend serves the index.html file
|
2020-08-07 03:50:35 +02:00
|
|
|
func (h *TaskcafeHandler) Frontend(w http.ResponseWriter, r *http.Request) {
|
2020-07-16 01:20:08 +02:00
|
|
|
f, err := frontend.Frontend.Open("index.h")
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
log.Warning("does not exist")
|
|
|
|
} else if err != nil {
|
|
|
|
log.WithError(err).Error("frontend")
|
|
|
|
}
|
|
|
|
http.ServeContent(w, r, "index.html", time.Now(), f)
|
|
|
|
}
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// ProfileImageUpload handles a user uploading a new avatar profile image
|
2020-08-07 03:50:35 +02:00
|
|
|
func (h *TaskcafeHandler) ProfileImageUpload(w http.ResponseWriter, r *http.Request) {
|
2020-07-05 01:02:57 +02:00
|
|
|
log.Info("preparing to upload file")
|
2020-09-11 20:57:02 +02:00
|
|
|
userID, ok := r.Context().Value(utils.UserIDKey).(uuid.UUID)
|
2020-07-05 01:02:57 +02:00
|
|
|
if !ok {
|
|
|
|
log.Error("not a valid uuid")
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse our multipart form, 10 << 20 specifies a maximum
|
|
|
|
// upload of 10 MB files.
|
|
|
|
r.ParseMultipartForm(10 << 20)
|
|
|
|
|
|
|
|
file, handler, err := r.FormFile("file")
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("issue while uploading file")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
log.WithFields(log.Fields{"filename": handler.Filename, "size": handler.Size, "header": handler.Header}).Info("file metadata")
|
|
|
|
|
|
|
|
fileBytes, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("while reading file")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile("uploads/"+handler.Filename, fileBytes, 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("while reading file")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-12 08:23:48 +02:00
|
|
|
h.repo.UpdateUserAccountProfileAvatarURL(r.Context(), db.UpdateUserAccountProfileAvatarURLParams{UserID: userID, ProfileAvatarUrl: sql.NullString{String: "/uploads/" + handler.Filename, Valid: true}})
|
2020-07-05 01:02:57 +02:00
|
|
|
// return that we have successfully uploaded our file!
|
|
|
|
log.Info("file uploaded")
|
2020-09-12 08:23:48 +02:00
|
|
|
json.NewEncoder(w).Encode(AvatarUploadResponseData{URL: "/uploads/" + handler.Filename, UserID: userID.String()})
|
2020-07-05 01:02:57 +02:00
|
|
|
|
|
|
|
}
|