From ace8d0ddee3496705dc0a28451ea346f58b3e30c Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sat, 3 Mar 2018 16:03:18 +0100 Subject: [PATCH] Implemented avatar uploads --- main.go | 4 ---- pages/upload/{upload.go => avatar.go} | 15 +++++++-------- scripts/Actions/Upload.ts | 14 +++++++++++--- 3 files changed, 18 insertions(+), 15 deletions(-) rename pages/upload/{upload.go => avatar.go} (61%) diff --git a/main.go b/main.go index 473c4d06..4c686026 100644 --- a/main.go +++ b/main.go @@ -3,10 +3,6 @@ package main import ( "strings" - _ "image/gif" - _ "image/jpeg" - _ "image/png" - "github.com/aerogo/aero" "github.com/aerogo/session-store-nano" "github.com/animenotifier/arn" diff --git a/pages/upload/upload.go b/pages/upload/avatar.go similarity index 61% rename from pages/upload/upload.go rename to pages/upload/avatar.go index c41ecc47..2bc61f06 100644 --- a/pages/upload/upload.go +++ b/pages/upload/avatar.go @@ -1,9 +1,6 @@ package upload import ( - "bytes" - "fmt" - "image" "net/http" "github.com/aerogo/aero" @@ -18,20 +15,22 @@ func Avatar(ctx *aero.Context) string { return ctx.Error(http.StatusUnauthorized, "Not logged in", nil) } + // Retrieve file from post body data, err := ctx.Request().Body().Bytes() if err != nil { return ctx.Error(http.StatusInternalServerError, "Reading request body failed", err) } - // Decode - img, format, err := image.Decode(bytes.NewReader(data)) + // Set avatar file + err = user.SetAvatarBytes(data) if err != nil { - return ctx.Error(http.StatusBadRequest, "Invalid image format", err) + return ctx.Error(http.StatusInternalServerError, "Invalid image format", err) } - fmt.Println("Avatar received!", len(data), format, img.Bounds().Dx(), img.Bounds().Dy(), user.Nick) - // ioutil.WriteFile("avatar") + // Save avatar information + user.Save() + return "ok" } diff --git a/scripts/Actions/Upload.ts b/scripts/Actions/Upload.ts index bf5efffd..7d747f85 100644 --- a/scripts/Actions/Upload.ts +++ b/scripts/Actions/Upload.ts @@ -10,7 +10,7 @@ export function selectFile(arn: AnimeNotifier, button: HTMLButtonElement) { let file = input.files[0] previewImage(file, preview) - uploadFile(file, "/api/upload/avatar") + uploadFile(file, "/api/upload/avatar", arn) } input.click() @@ -33,11 +33,13 @@ function previewImage(file: File, preview: HTMLImageElement) { } // Upload file -function uploadFile(file: File, endpoint: string) { +function uploadFile(file: File, endpoint: string, arn: AnimeNotifier) { let reader = new FileReader() reader.onloadend = async () => { - await fetch(endpoint, { + arn.statusMessage.showInfo("Uploading avatar...") + + let response = await fetch(endpoint, { method: "POST", credentials: "include", headers: { @@ -45,6 +47,12 @@ function uploadFile(file: File, endpoint: string) { }, body: reader.result }) + + if(response.ok) { + arn.statusMessage.showInfo("Successfully uploaded your new avatar.") + } else { + arn.statusMessage.showError("Failed uploading your new avatar.") + } } reader.readAsArrayBuffer(file)