prime fun + user stash

This commit is contained in:
daru
2022-07-26 20:33:11 +02:00
parent 3771b1d2c4
commit 7fe4181c72
7 changed files with 113 additions and 25 deletions

55
ober.go
View File

@@ -28,6 +28,7 @@ func RunWebserv() {
r.GET("/api/chat/{id}", Headers(ChatGet))
r.GET("/api/log", Headers(LogGet))
r.GET("/api/user/{user?}", Headers(UserGet))
r.GET("/api/userdata/{user}/{data}", Headers(UserStashGet))
r.GET("/api/watch/{user?}", Headers(WatchGet))
r.GET("/api/watchext/{user?}", Headers(WatchExtendedGet))
r.POST("/api/appointment/{user}", Headers(AppointmentPost))
@@ -35,6 +36,7 @@ func RunWebserv() {
r.POST("/api/register", Headers(Register))
r.POST("/api/watch/{user}", Headers(WatchPost))
r.PATCH("/api/register", Headers(RegisterUpdate))
r.PUT("/api/userdata/{user}/{data}", Headers(UserStashPut))
r.DELETE("/api/appointment/{user}", Headers(AppointmentDelete))
r.DELETE("/api/register", Headers(UnRegister))
r.DELETE("/api/watch/{user}", Headers(WatchDelete))
@@ -327,6 +329,10 @@ func UserGet(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusOK)
}
func UserStashGet(ctx *fasthttp.RequestCtx) {
processUserStashReq(ctx, false)
}
func WatchGet(ctx *fasthttp.RequestCtx) {
animeUsers, err := watchGetLogic(ctx)
if err != nil {
@@ -585,6 +591,10 @@ func WatchPost(ctx *fasthttp.RequestCtx) {
processUpdateReq(ctx, true)
}
func UserStashPut(ctx *fasthttp.RequestCtx) {
processUserStashReq(ctx, true)
}
func AppointmentDelete(ctx *fasthttp.RequestCtx) {
processUpdateAppointmentReq(ctx, false)
}
@@ -875,6 +885,51 @@ func processUpdateAppointmentReq(ctx *fasthttp.RequestCtx, update bool) {
ctx.SetContentType("application/json; charset=utf-8")
}
func processUserStashReq(ctx *fasthttp.RequestCtx, update bool) {
auth := ctx.Request.Header.Peek("X-HUSO-AUTH")
dataPath := fmt.Sprintf("%s", ctx.UserValue("data"))
if auth == nil || string(auth) == "" || dataPath == "" {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
return
}
username := fmt.Sprintf("%s", ctx.UserValue("user"))
legit, _ := GheddoAuth(username, string(auth))
if !legit {
ctx.SetStatusCode(fasthttp.StatusUnauthorized)
return
}
if update {
compressed, err := CompressZstd(ctx.PostBody())
if err != nil {
addErrorToCtx(ctx, err)
return
}
err = DbSave(bucketStash, fmt.Sprintf("%s.%s", username, dataPath), compressed)
if err != nil {
addErrorToCtx(ctx, err)
return
}
ctx.SetStatusCode(fasthttp.StatusAccepted)
} else {
compressed, err := DbRead(bucketStash, []byte(fmt.Sprintf("%s.%s", username, dataPath)))
if err != nil {
addErrorToCtx(ctx, err)
return
}
decompressed, err := DecompressZstd(compressed)
if err != nil {
addErrorToCtx(ctx, err)
return
}
ctx.SetBody(decompressed)
ctx.SetStatusCode(fasthttp.StatusOK)
}
}
func writeResponseBody(ctx *fasthttp.RequestCtx, bytes []byte) error {
_, err := ctx.Write(bytes)
if err != nil {