Added basic upload flow
This commit is contained in:
parent
e1bbcd2756
commit
b83b1b94a5
@ -53,6 +53,7 @@ import (
|
|||||||
"github.com/animenotifier/notify.moe/pages/statistics"
|
"github.com/animenotifier/notify.moe/pages/statistics"
|
||||||
"github.com/animenotifier/notify.moe/pages/terms"
|
"github.com/animenotifier/notify.moe/pages/terms"
|
||||||
"github.com/animenotifier/notify.moe/pages/threads"
|
"github.com/animenotifier/notify.moe/pages/threads"
|
||||||
|
"github.com/animenotifier/notify.moe/pages/upload"
|
||||||
"github.com/animenotifier/notify.moe/pages/user"
|
"github.com/animenotifier/notify.moe/pages/user"
|
||||||
"github.com/animenotifier/notify.moe/pages/users"
|
"github.com/animenotifier/notify.moe/pages/users"
|
||||||
)
|
)
|
||||||
@ -185,6 +186,9 @@ func Configure(app *aero.Application) {
|
|||||||
l.Page("/shop/history", shop.PurchaseHistory)
|
l.Page("/shop/history", shop.PurchaseHistory)
|
||||||
app.Post("/api/shop/buy/:item/:quantity", shop.BuyItem)
|
app.Post("/api/shop/buy/:item/:quantity", shop.BuyItem)
|
||||||
|
|
||||||
|
// Upload
|
||||||
|
app.Post("/api/upload/avatar", upload.Avatar)
|
||||||
|
|
||||||
// Admin
|
// Admin
|
||||||
l.Page("/admin", admin.Get)
|
l.Page("/admin", admin.Get)
|
||||||
l.Page("/admin/webdev", admin.WebDev)
|
l.Page("/admin/webdev", admin.WebDev)
|
||||||
|
43
pages/upload/upload.go
Normal file
43
pages/upload/upload.go
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package upload
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"image"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/aerogo/aero"
|
||||||
|
"github.com/animenotifier/notify.moe/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Avatar ...
|
||||||
|
func Avatar(ctx *aero.Context) string {
|
||||||
|
user := utils.GetUser(ctx)
|
||||||
|
|
||||||
|
if user == nil {
|
||||||
|
return ctx.Error(http.StatusUnauthorized, "Not logged in", nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := ctx.Request().Body().Bytes()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return ctx.Error(http.StatusInternalServerError, "Reading request body failed", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
format, err := guessImageFormat(bytes.NewReader(data))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return ctx.Error(http.StatusBadRequest, "Could not determine image file type", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Avatar received!", len(data), format, user.Nick)
|
||||||
|
// ioutil.WriteFile("avatar")
|
||||||
|
return "ok"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Guess image format from gif/jpeg/png/webp
|
||||||
|
func guessImageFormat(r io.Reader) (format string, err error) {
|
||||||
|
_, format, err = image.DecodeConfig(r)
|
||||||
|
return format, err
|
||||||
|
}
|
@ -7,20 +7,19 @@ export function selectFile(arn: AnimeNotifier, button: HTMLButtonElement) {
|
|||||||
input.setAttribute("type", "file")
|
input.setAttribute("type", "file")
|
||||||
|
|
||||||
input.onchange = () => {
|
input.onchange = () => {
|
||||||
previewImage(input, preview)
|
let file = input.files[0]
|
||||||
uploadImage(input, preview)
|
|
||||||
|
previewImage(file, preview)
|
||||||
|
uploadImage(file)
|
||||||
}
|
}
|
||||||
|
|
||||||
input.click()
|
input.click()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Preview image
|
// Preview image
|
||||||
function previewImage(input: HTMLInputElement, preview: HTMLImageElement) {
|
function previewImage(file: File, preview: HTMLImageElement) {
|
||||||
let file = input.files[0]
|
|
||||||
let reader = new FileReader()
|
let reader = new FileReader()
|
||||||
|
|
||||||
console.log(file.name, file.size, file.type)
|
|
||||||
|
|
||||||
reader.onloadend = () => {
|
reader.onloadend = () => {
|
||||||
preview.classList.remove("hidden")
|
preview.classList.remove("hidden")
|
||||||
preview.src = reader.result
|
preview.src = reader.result
|
||||||
@ -29,11 +28,26 @@ function previewImage(input: HTMLInputElement, preview: HTMLImageElement) {
|
|||||||
if(file) {
|
if(file) {
|
||||||
reader.readAsDataURL(file)
|
reader.readAsDataURL(file)
|
||||||
} else {
|
} else {
|
||||||
preview.src = ""
|
preview.classList.add("hidden")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upload image
|
// Upload image
|
||||||
function uploadImage(input: HTMLInputElement, preview: HTMLImageElement) {
|
function uploadImage(file: File) {
|
||||||
|
let reader = new FileReader()
|
||||||
|
|
||||||
|
reader.onloadend = async () => {
|
||||||
|
await fetch("/api/upload/avatar", {
|
||||||
|
method: "POST",
|
||||||
|
credentials: "include",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/octet-stream"
|
||||||
|
},
|
||||||
|
body: reader.result
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log("Avatar upload finished!")
|
||||||
|
}
|
||||||
|
|
||||||
|
reader.readAsBinaryString(file)
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user