diff --git a/cmd/webapi/register-web-ui.go b/cmd/webapi/register-web-ui.go index 352374e..9cb0af9 100644 --- a/cmd/webapi/register-web-ui.go +++ b/cmd/webapi/register-web-ui.go @@ -4,10 +4,11 @@ package main import ( "fmt" - "github.com/notherealmarco/WASAPhoto/webui" "io/fs" "net/http" "strings" + + "github.com/notherealmarco/WASAPhoto/webui" ) func registerWebUI(hdl http.Handler) (http.Handler, error) { @@ -20,6 +21,10 @@ func registerWebUI(hdl http.Handler) (http.Handler, error) { if strings.HasPrefix(r.RequestURI, "/dashboard/") { http.StripPrefix("/dashboard/", http.FileServer(http.FS(distDirectory))).ServeHTTP(w, r) return + } else if r.RequestURI == "/" { + // Redirect to dashboard + http.Redirect(w, r, "/dashboard/", http.StatusTemporaryRedirect) + return } hdl.ServeHTTP(w, r) }), nil diff --git a/service/api/photos.go b/service/api/photos.go index 98e5ddd..bd935fe 100644 --- a/service/api/photos.go +++ b/service/api/photos.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "strconv" + "strings" "github.com/julienschmidt/httprouter" "github.com/notherealmarco/WASAPhoto/service/api/authorization" @@ -39,23 +40,39 @@ func (rt *_router) PostPhoto(w http.ResponseWriter, r *http.Request, ps httprout return } - file, err := os.Create(path) + /*file, err := os.Create(path) if err != nil { helpers.SendInternalError(err, "Error creating file", w, rt.baseLogger) helpers.RollbackOrLogError(transaction, rt.baseLogger) return + }*/ + + bytes, err := io.ReadAll(r.Body) + + if err != nil { + helpers.SendInternalError(err, "Error checking the file", w, rt.baseLogger) + helpers.RollbackOrLogError(transaction, rt.baseLogger) + return } - if _, err = io.Copy(file, r.Body); err != nil { + mimeType := http.DetectContentType(bytes) + + if !strings.HasPrefix(mimeType, "image/") { + helpers.SendStatus(http.StatusBadRequest, w, mimeType+" file is not a valid image", rt.baseLogger) + helpers.RollbackOrLogError(transaction, rt.baseLogger) + return + } + + if err = os.WriteFile(path, bytes, 0644); err != nil { helpers.SendInternalError(err, "Error writing the file", w, rt.baseLogger) helpers.RollbackOrLogError(transaction, rt.baseLogger) return } - if err = file.Close(); err != nil { + /*if err = file.Close(); err != nil { helpers.SendInternalError(err, "Error closing file", w, rt.baseLogger) helpers.RollbackOrLogError(transaction, rt.baseLogger) - } + }*/ err = transaction.Commit() diff --git a/webui/src/components/UserCard.vue b/webui/src/components/UserCard.vue index ac4fe54..e053709 100644 --- a/webui/src/components/UserCard.vue +++ b/webui/src/components/UserCard.vue @@ -20,9 +20,17 @@ export default { show_username_form: false, newUsername: "", upload_file: null, + + modalTitle: "", + modalMsg: "", } }, methods: { + playModal(title, msg) { + this.modalTitle = title + this.modalMsg = msg + this.$refs.openModal.click() + }, logout() { localStorage.removeItem("token"); sessionStorage.removeItem("token"); @@ -37,7 +45,7 @@ export default { this.user_followed = true this.$emit('updateInfo') }) - .catch(error => alert(error.toString())); + .catch(error => this.playModal("Error", error.toString())); }, unfollow() { this.$axios.delete("/users/" + this.user_id + "/followers/" + getCurrentSession()) @@ -45,7 +53,7 @@ export default { this.user_followed = false this.$emit('updateInfo') }) - .catch(error => alert(error.toString())); + .catch(error => this.playModal("Error", error.toString())); }, ban() { this.$axios.put("/users/" + getCurrentSession() + "/bans/" + this.user_id) @@ -53,7 +61,7 @@ export default { this.user_banned = true this.$emit('updateInfo') }) - .catch(error => alert(error.toString())); + .catch(error => this.playModal("Error", error.toString())); }, unban() { this.$axios.delete("/users/" + getCurrentSession() + "/bans/" + this.user_id) @@ -61,7 +69,7 @@ export default { this.user_banned = false this.$emit('updateInfo') }) - .catch(error => alert(error.toString())); + .catch(error => this.playModal("Error", error.toString())); }, load_file(e) { let files = e.target.files || e.dataTransfer.files; @@ -74,7 +82,16 @@ export default { this.show_post_form = false this.$emit('updatePosts') }) - .catch(error => alert(error.toString())); + .catch(error => { + if (error.response.status != null && error.response.data != null) { + this.modalTitle = "Error" + this.modalMsg = error.response.data + this.$refs.openModal.click() + } else { + this.playModal("Error", error.toString()) + } + this.playModal("Error", error.toString()) + }); }, updateUsername() { this.$axios.put("/users/" + getCurrentSession() + "/username", {name: this.newUsername}) @@ -85,9 +102,11 @@ export default { }) .catch(error => { if (error.response.status == 409) { + this.modalTitle = "Error" + this.modalMsg = "The chosen username is already taken." this.$refs.openModal.click() } else { - alert(error.toString()) + this.playModal("Error", error.toString()) } }); }, @@ -97,6 +116,10 @@ export default { }