Skip to content

Commit

Permalink
unstable: handlers: gallery: added reworked gallery support
Browse files Browse the repository at this point in the history
  • Loading branch information
pandadiestro committed Nov 27, 2024
1 parent 2677fc6 commit 4701e1f
Show file tree
Hide file tree
Showing 11 changed files with 290 additions and 127 deletions.
1 change: 0 additions & 1 deletion stiller-backend/db/create.sql
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ create table metatemplatefile (
create table template (
id integer unique primary key autoincrement not null,
tier integer not null references tier (id),
thumbnail integer not null references file (id),
templatefile integer not null references metatemplatefile(id),
title text not null,
description text not null
Expand Down
31 changes: 5 additions & 26 deletions stiller-backend/internal/handlers/get_gallery.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package handlers

import (
"archive/tar"
"bufio"
"net/http"
"os"
"stiller/pkg/dbutils"
"stiller/pkg/fsop"
"stiller/pkg/jwt"
"stiller/pkg/loggers"
"stiller/pkg/netwrappers"
Expand Down Expand Up @@ -49,28 +45,11 @@ func GetGallery(w http.ResponseWriter, r *http.Request, params httprouter.Params
exec_err := sqlitex.ExecuteTransient(new_dbconn, get_galleries.String(), &sqlitex.ExecOptions{
ResultFunc: func(stmt *sqlite.Stmt) error {
gall_template := int(stmt.GetInt64("template"))
gall_id := int(stmt.GetInt64("id"))

template_path := fsop.GetTemplatePath(gall_template)
file, open_err := os.Open(template_path)
if loggers.RequestLog(open_err, "", http.StatusInternalServerError, &w) {
return open_err
}

defer file.Close()

tar_reader := tar.NewReader(file)
_, read_err := tar_reader.Next()
if loggers.RequestLog(read_err, "", http.StatusInternalServerError, &w) {
return read_err
}

buftar := bufio.NewReader(tar_reader)


new_gallery_slots, newgall_slots_err := dbutils.GetGallerySlots(gall_id, buftar)
if loggers.RequestLog(newgall_slots_err, "", http.StatusInternalServerError, &w) {
return newgall_slots_err
gall_id := int(stmt.GetInt64("id"))
gallery_data, slots_err := dbutils.GetGalleryData(gall_id)
if slots_err != nil {
return slots_err
}

gallery := dbutils.StillerGallery{
Expand All @@ -80,7 +59,7 @@ func GetGallery(w http.ResponseWriter, r *http.Request, params httprouter.Params
Description: stmt.GetText("description"),
TemplateId: gall_template,
Slug: stmt.GetText("slug"),
Slots: new_gallery_slots,
Data: *gallery_data,
}

galleries = append(galleries, gallery)
Expand Down
9 changes: 4 additions & 5 deletions stiller-backend/internal/handlers/get_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func GetTemplate(w http.ResponseWriter, r *http.Request, params httprouter.Param

user_id := user_decoded.UserId
tier_stmt := sqlf.
Select("tier").
From("user").
Where("id = ?", user_id)
Select("tier").
From("user").
Where("id = ?", user_id)

dbconn, dbconn_err := dbutils.NewConn()
if loggers.RequestLog(dbconn_err, "", http.StatusInternalServerError, &w) {
Expand Down Expand Up @@ -64,7 +64,7 @@ func GetTemplate(w http.ResponseWriter, r *http.Request, params httprouter.Param

gettemplates_stmt := sqlf.
Select("*").
From("template").
From("template").
Where("tier <= ?", tier)

templates := make([]dbutils.StillerTemplate, 0, 2)
Expand All @@ -74,7 +74,6 @@ func GetTemplate(w http.ResponseWriter, r *http.Request, params httprouter.Param
new_template := dbutils.StillerTemplate{
Id: int(stmt.GetInt64("id")),
TierId: int(stmt.GetInt64("tier")),
ThumbnailId: int(stmt.GetInt64("thumbnail")),
TemplateId: int(stmt.GetInt64("template")),
Title: stmt.GetText("title"),
Description: stmt.GetText("description"),
Expand Down
42 changes: 42 additions & 0 deletions stiller-backend/internal/handlers/get_template.info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package handlers

import (
"net/http"
"stiller/pkg/jwt"
"stiller/pkg/loggers"
"stiller/pkg/netwrappers"
"stiller/pkg/templates"
"strconv"

"github.com/julienschmidt/httprouter"
)

func GetTemplateInfo(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
if netwrappers.CORS(w, r) {
return
}

user_token := r.Header.Get("token")
_, token_decode_err := jwt.Decode(user_token)
if loggers.RequestLog(token_decode_err, "", http.StatusUnauthorized, &w) {
return
}

template_id_str := params.ByName("template_id")
template_id, template_id_err := strconv.Atoi(template_id_str)
if loggers.RequestLog(template_id_err, "", http.StatusBadRequest, &w) {
return
}

metatemplate_field := params.ByName("field")
switch metatemplate_field {
case "data", "scene", "thumbnail", "topview":
templates.WriteTemplateSubfile(w, metatemplate_field, template_id)

default:
loggers.RequestLog(nil, "not a valid field", http.StatusNotFound, &w)
return
}
}


38 changes: 29 additions & 9 deletions stiller-backend/internal/handlers/post_auth.newuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,15 @@ func PostAuthNewuser(w http.ResponseWriter, r *http.Request, _ httprouter.Params
Passwd string `json:"pwd"`
}

rpayload := ReqPayload{}
unmarshal_err := jsonexp.UnmarshalRead(r.Body, &rpayload, jsonexp.DefaultOptionsV2())
type ResPayload struct {
Token string `json:"token"`
UserData dbutils.StillerUser `json:"userdata"`
}

req_payload := ReqPayload{}
res_payload := ResPayload{}

unmarshal_err := jsonexp.UnmarshalRead(r.Body, &req_payload, jsonexp.DefaultOptionsV2())
if loggers.RequestLog(unmarshal_err, "", http.StatusBadRequest, &w) {
return
}
Expand Down Expand Up @@ -142,11 +149,11 @@ func PostAuthNewuser(w http.ResponseWriter, r *http.Request, _ httprouter.Params

query_stmt := sqlf.Select(
"newuser(?, ?, ?, ?, ?)",
rpayload.TierId,
rpayload.Username,
rpayload.Username,
rpayload.Mail,
rpayload.Passwd,
req_payload.TierId,
req_payload.Username,
req_payload.Username,
req_payload.Mail,
req_payload.Passwd,
)

defer query_stmt.Close()
Expand All @@ -165,6 +172,11 @@ func PostAuthNewuser(w http.ResponseWriter, r *http.Request, _ httprouter.Params
Args: query_stmt.Args(),
})

if new_tk.UserId == -1 {
loggers.RequestLog(nil, "no user was created", http.StatusInternalServerError, &w)
return
}

if loggers.RequestLog(exec_err, "", http.StatusBadRequest, &w) {
return
}
Expand All @@ -175,13 +187,21 @@ func PostAuthNewuser(w http.ResponseWriter, r *http.Request, _ httprouter.Params
return
}

new_user, newuser_err := dbutils.GetUserById(new_tk.UserId, new_dbconn)
new_user.Bpasswd = ""
if loggers.RequestLog(newuser_err, "", http.StatusInternalServerError, &w) {
return
}

sign_encoded, sign_err := new_tk.Encode()
if loggers.RequestLog(sign_err, "", http.StatusInternalServerError, &w) {
return
}

w.WriteHeader(http.StatusOK)
w.Write(sign_encoded)
res_payload.UserData = new_user
res_payload.Token = string(sign_encoded)

jsonexp.MarshalWrite(w, res_payload, jsonexp.DefaultOptionsV2())
}


Expand Down
72 changes: 29 additions & 43 deletions stiller-backend/internal/handlers/post_gallery.new.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package handlers

import (
"archive/tar"
"bufio"
"net/http"
"os"
"stiller/pkg/dbutils"
"stiller/pkg/fsop"
"stiller/pkg/jwt"
"stiller/pkg/loggers"
"stiller/pkg/netwrappers"
Expand Down Expand Up @@ -35,6 +31,8 @@ func PostGalleryNew(w http.ResponseWriter, r *http.Request, params httprouter.Pa
type ResPayload dbutils.StillerGallery

req_payload := ReqPayload{}
res_payload := ResPayload{}

unmarshal_err := jsonexp.UnmarshalRead(r.Body, &req_payload, jsonexp.DefaultOptionsV2())
if loggers.RequestLog(unmarshal_err, "", http.StatusBadRequest, &w) {
return
Expand Down Expand Up @@ -86,63 +84,51 @@ func PostGalleryNew(w http.ResponseWriter, r *http.Request, params httprouter.Pa
return
}

path := fsop.GetTemplatePath(req_payload.Template)
file, open_err := os.Open(path)
if loggers.RequestLog(open_err, "", http.StatusInternalServerError, &w) {
return
}

defer file.Close()

tar_reader := tar.NewReader(file)
_, read_err := tar_reader.Next()
if loggers.RequestLog(read_err, "", http.StatusInternalServerError, &w) {
template_data, template_data_err := templates.GetTemplateData(req_payload.Template)
if loggers.RequestLog(template_data_err, "", http.StatusInternalServerError, &w) {
return
}

buftar := bufio.NewReader(tar_reader)
gallery_slots := make(
[]dbutils.StillerGallerySlot,
0,
len(template_data.Slots),
)

templatefile_slots := []templates.MetatemplateSlot{}
slotunmarshal_exp := jsonexp.UnmarshalRead(buftar, &templatefile_slots, jsonexp.DefaultOptionsV2())
if loggers.RequestLog(slotunmarshal_exp, "", http.StatusInternalServerError, &w) {
return
}

insert_slots_stmt := sqlf.
new_gallery_slots_stmt := sqlf.
InsertInto("galleryslot")

for index := range templatefile_slots {
insert_slots_stmt.NewRow().

for index := range template_data.Slots {
new_gallery_slots_stmt.
NewRow().
Set("gallery", newgallery_id).
Set("slotid", templatefile_slots[index].Ref)
Set("slotid", template_data.Slots[index].Ref)

gallery_slots = append(gallery_slots, dbutils.StillerGallerySlot{
MetatemplateSlot: template_data.Slots[index],
})
}

insert_slots_err := sqlitex.ExecuteTransient(dbconn, insert_slots_stmt.String(), &sqlitex.ExecOptions{
Args: insert_slots_stmt.Args(),
insert_slots_err := sqlitex.ExecuteTransient(dbconn, new_gallery_slots_stmt.String(), &sqlitex.ExecOptions{
Args: new_gallery_slots_stmt.Args(),
})

if loggers.RequestLog(insert_slots_err, "", http.StatusInternalServerError, &w) {
if loggers.RequestLog(insert_slots_err, "", http.StatusBadRequest, &w) {
return
}

gallery_slots := make([]dbutils.StillerGallerySlot, 0, len(templatefile_slots))
for index := range templatefile_slots {
gallery_slots = append(gallery_slots, dbutils.StillerGallerySlot{
Ref: templatefile_slots[index].Ref,
Type: templatefile_slots[index].Type,
Props: templatefile_slots[index].Props,
Vertices: templatefile_slots[index].Vertices,
})
}

res_payload := dbutils.StillerGallery{
res_payload = ResPayload{
Id: newgallery_id,
Title: req_payload.Title,
Description: req_payload.Description,
OwnerId: user_id,
TemplateId: req_payload.Template,
Slug: req_payload.Slug,
Slots: gallery_slots,
Title: req_payload.Title,
Description: req_payload.Description,
Data: dbutils.StillerGalleryData{
Origin: template_data.Origin,
Slots: gallery_slots,
},
}

marshal_err := jsonexp.MarshalWrite(w, res_payload, jsonexp.DefaultOptionsV2())
Expand Down
7 changes: 0 additions & 7 deletions stiller-backend/internal/handlers/post_template.new.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ func PostTemplateNew(w http.ResponseWriter, r *http.Request, params httprouter.P

type ReqPayload dbutils.StillerTemplate

type TemplateType struct {
Tier dbutils.StillerTier `form:"tier"`
Title string `form:"title"`
Description string `form:"description"`
}

user_token := r.Header.Get("token")
user_tk, token_decode_err := jwt.Decode(user_token)
if loggers.RequestLog(token_decode_err, "", http.StatusUnauthorized, &w) {
Expand Down Expand Up @@ -153,7 +147,6 @@ func PostTemplateNew(w http.ResponseWriter, r *http.Request, params httprouter.P
InsertInto("template").
NewRow().
Set("tier", req_payload.TierId).
Set("thumbnail", req_payload.ThumbnailId).
Set("title", req_payload.Title).
Set("description", req_payload.Description).
Set("templatefile", templatefile_id).
Expand Down
15 changes: 10 additions & 5 deletions stiller-backend/internal/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ var routes = [...]individualHandler{
method: http.MethodGet,
handlefunc: handlers.GetTemplate,
},
{
path: "/template/info/:template_id/:field",
method: http.MethodGet,
handlefunc: handlers.GetTemplateInfo,
},
{
path: "/template/new",
method: http.MethodPost,
Expand All @@ -29,16 +34,16 @@ var routes = [...]individualHandler{
method: http.MethodGet,
handlefunc: handlers.GetGallery,
},
{
path: "/gallery/edit",
method: http.MethodPatch,
handlefunc: handlers.PatchGalleryEdit,
},
{
path: "/gallery/new",
method: http.MethodPost,
handlefunc: handlers.PostGalleryNew,
},
{
path: "/gallery/edit",
method: http.MethodPatch,
handlefunc: handlers.PatchGalleryEdit,
},
{
path: "/gallery/slot",
method: http.MethodPatch,
Expand Down
Loading

0 comments on commit 4701e1f

Please sign in to comment.