Implemented avatar uploads
This commit is contained in:
parent
91f38a6846
commit
ace8d0ddee
4
main.go
4
main.go
@ -3,10 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
_ "image/gif"
|
|
||||||
_ "image/jpeg"
|
|
||||||
_ "image/png"
|
|
||||||
|
|
||||||
"github.com/aerogo/aero"
|
"github.com/aerogo/aero"
|
||||||
"github.com/aerogo/session-store-nano"
|
"github.com/aerogo/session-store-nano"
|
||||||
"github.com/animenotifier/arn"
|
"github.com/animenotifier/arn"
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
package upload
|
package upload
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"image"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/aerogo/aero"
|
"github.com/aerogo/aero"
|
||||||
@ -18,20 +15,22 @@ func Avatar(ctx *aero.Context) string {
|
|||||||
return ctx.Error(http.StatusUnauthorized, "Not logged in", nil)
|
return ctx.Error(http.StatusUnauthorized, "Not logged in", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Retrieve file from post body
|
||||||
data, err := ctx.Request().Body().Bytes()
|
data, err := ctx.Request().Body().Bytes()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ctx.Error(http.StatusInternalServerError, "Reading request body failed", err)
|
return ctx.Error(http.StatusInternalServerError, "Reading request body failed", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode
|
// Set avatar file
|
||||||
img, format, err := image.Decode(bytes.NewReader(data))
|
err = user.SetAvatarBytes(data)
|
||||||
|
|
||||||
if err != nil {
|
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)
|
// Save avatar information
|
||||||
// ioutil.WriteFile("avatar")
|
user.Save()
|
||||||
|
|
||||||
return "ok"
|
return "ok"
|
||||||
}
|
}
|
@ -10,7 +10,7 @@ export function selectFile(arn: AnimeNotifier, button: HTMLButtonElement) {
|
|||||||
let file = input.files[0]
|
let file = input.files[0]
|
||||||
|
|
||||||
previewImage(file, preview)
|
previewImage(file, preview)
|
||||||
uploadFile(file, "/api/upload/avatar")
|
uploadFile(file, "/api/upload/avatar", arn)
|
||||||
}
|
}
|
||||||
|
|
||||||
input.click()
|
input.click()
|
||||||
@ -33,11 +33,13 @@ function previewImage(file: File, preview: HTMLImageElement) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Upload file
|
// Upload file
|
||||||
function uploadFile(file: File, endpoint: string) {
|
function uploadFile(file: File, endpoint: string, arn: AnimeNotifier) {
|
||||||
let reader = new FileReader()
|
let reader = new FileReader()
|
||||||
|
|
||||||
reader.onloadend = async () => {
|
reader.onloadend = async () => {
|
||||||
await fetch(endpoint, {
|
arn.statusMessage.showInfo("Uploading avatar...")
|
||||||
|
|
||||||
|
let response = await fetch(endpoint, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
credentials: "include",
|
credentials: "include",
|
||||||
headers: {
|
headers: {
|
||||||
@ -45,6 +47,12 @@ function uploadFile(file: File, endpoint: string) {
|
|||||||
},
|
},
|
||||||
body: reader.result
|
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)
|
reader.readAsArrayBuffer(file)
|
||||||
|
Loading…
Reference in New Issue
Block a user