Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
pandadiestro committed Nov 18, 2024
1 parent 0ad0b50 commit e2f003d
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 46 deletions.
20 changes: 10 additions & 10 deletions stiller-backend/db/create.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ create table avatar (
create table gallery (
id integer unique primary key autoincrement not null,
owner integer not null references user (id),
-- ******slug (pedir al crear galería)
-- ******template_fk (pedir al crear galería)
title text not null,
template integer not null references template (id),
slug text unique not null,
title text,
description text,
deploy_stage integer not null,
url text unique not null
Expand All @@ -51,7 +51,7 @@ 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),
res integer not null references file (id),
templatefile integer not null references file (id),
title text not null,
description text not null
);
Expand Down Expand Up @@ -84,28 +84,28 @@ create table template (

create table galleryslot (
gallery integer not null references gallery (id),
slot_ref integer not null, -- inmutable
slotref integer not null,
res integer references file (id),
title text not null,
description text not null
title text,
description text
);

insert into
filetype
values
(0, 'image'),
(0, 'image'),
(1, 'video'),
(2, 'object3d');

insert into
tier
values
(0, 'free'),
(0, 'free'),
(1, 'van gogh'),
(2, 'picasso');

insert into
user (tier, displayname, username, mail, bpasswd)
values
(0, 'admin', 'admin', '', 'skibidi pana');
(0, 'admin', 'admin', '[email protected]', 'skibidi pana');

25 changes: 18 additions & 7 deletions stiller-backend/internal/dbutils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,24 @@ type StillerTemplateBlock struct {
}

type StillerTemplate struct {
Id int `json:"id"`
TierId int `json:"tier"`
ThumbnailId int `json:"thumbnail"`
ResId int `json:"res"`
Title string `json:"title"`
Description string `json:"description"`
Blocks []StillerTemplateBlock `json:"blocks"`
Id int `json:"id"`
TierId int `json:"tier"`
ThumbnailId int `json:"thumbnail"`
TemplateId int `json:"templatefile"`
Title string `json:"title"`
Description string `json:"description"`
}

type SlotType uint8
const (
D2 SlotType = iota
D3
)

type StillerSlot struct {
Ref int `json:"ref"`
Pos []float64 `json:"pos"`
Type SlotType `json:"type"`
}


12 changes: 11 additions & 1 deletion stiller-backend/internal/dbutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@ import (
)

func newPool() *sqlitex.Pool {
pool, err := sqlitex.NewPool(stiller.StillerConfig.DBPath, sqlitex.PoolOptions{})
pool, err := sqlitex.NewPool(stiller.StillerConfig.DBPath, sqlitex.PoolOptions{
PrepareConn: func(conn *sqlite.Conn) error {
stmt, _, err := conn.PrepareTransient("PRAGMA foreign_keys = ON;")
_, row_ret_err := stmt.Step()
if row_ret_err != nil {
return row_ret_err
}

return err
},
})
if err != nil {
log.Panicln(err)
}
Expand Down
143 changes: 143 additions & 0 deletions stiller-backend/internal/handlers/gallery/addgallery/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package addgallery

import (
"archive/tar"
"bufio"
"net/http"
"os"
"stiller/internal/dbutils"
"stiller/internal/fsutils"
"stiller/internal/handlers/handleutils"
"stiller/internal/jwtutils"

jsonexp "github.com/go-json-experiment/json"
"github.com/julienschmidt/httprouter"
"github.com/leporo/sqlf"
"zombiezen.com/go/sqlite"
"zombiezen.com/go/sqlite/sqlitex"
)


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

type ReqPayload struct {
Template int `json:"template"`
Slug string `json:"slug"`
Title string `json:"title"`
Description string `json:"description"`
}

req_payload := ReqPayload{}
unmarshal_err := jsonexp.UnmarshalRead(r.Body, &req_payload, jsonexp.DefaultOptionsV2())
if handleutils.RequestLog(unmarshal_err, "", http.StatusBadRequest, &w) {
return
}

user_token := r.Header.Get("token")
user_tk, token_decode_err := jwtutils.Decode(user_token)
if handleutils.RequestLog(token_decode_err, "", http.StatusUnauthorized, &w) {
return
}

user_id := user_tk.UserId

newgallery_stmt := sqlf.InsertInto("gallery").
NewRow().
Set("owner", user_id).
Set("template", req_payload.Template).
Set("slug", req_payload.Slug).
Set("title", req_payload.Title).
Set("description", req_payload.Description).
Returning("id")

newgallery_query, newgallery_args := newgallery_stmt.String(), newgallery_stmt.Args()

dbconn, dbconn_err := dbutils.NewConn()
if handleutils.RequestLog(dbconn_err, "", http.StatusInternalServerError, &w) {
return
}

defer dbutils.CloseConn(dbconn)

newgallery_id := int(-1)
exec_err := sqlitex.ExecuteTransient(dbconn, newgallery_query, &sqlitex.ExecOptions{
ResultFunc: func(stmt *sqlite.Stmt) error {
newgallery_id = int(stmt.GetInt64("id"))
return nil
},

Args: newgallery_args,
})

if handleutils.RequestLog(exec_err, "", http.StatusInternalServerError, &w) {
return
}

if newgallery_id == -1 {
handleutils.GenericLog(nil, "no new template was created")
w.WriteHeader(http.StatusInternalServerError)
return
}

templpath_stmt := sqlf.
Select("path").
From("file").
Where("id = ?", req_payload.Template)

metatemplate_path := ""
templpath_query, temptemplpath_args := templpath_stmt.String(), templpath_stmt.Args()
sqlitex.ExecuteTransient(dbconn, templpath_query, &sqlitex.ExecOptions{
ResultFunc: func(stmt *sqlite.Stmt) error {
metatemplate_path = stmt.GetText("path")
return nil
},
Args: temptemplpath_args,
})

if len(metatemplate_path) == 0 {
handleutils.GenericLog(nil, "no valid template, empty path")
w.WriteHeader(http.StatusInternalServerError)
return
}

flag, flag_err := fsutils.FileExists(metatemplate_path)
if handleutils.RequestLog(flag_err, "", http.StatusInternalServerError, &w){
return
}

if !flag {
handleutils.GenericLog(nil, "metatemplate not found")
w.WriteHeader(http.StatusInternalServerError)
return
}

metatempl_file, open_err := os.Open(metatemplate_path)
if handleutils.RequestLog(open_err, "", http.StatusInternalServerError, &w) {
return
}

bufread := bufio.NewReader(metatempl_file)

tarball := tar.NewReader(bufread)
slots := []dbutils.StillerSlot{}
slotread_err := jsonexp.UnmarshalRead(tarball, &slots, jsonexp.DefaultOptionsV2())
if handleutils.RequestLog(slotread_err, "", http.StatusInternalServerError, &w) {
return
}

newslots_stmt := sqlf.InsertInto("galleryslot")
for index := range slots {
newslots_stmt.NewRow().
Set("gallery", newgallery_id).
Set("slotref", slots[index].Ref)
}

newslots_query, newslots_args := newslots_stmt.String(), newslots_stmt.Args()
sqlitex.ExecuteTransient(dbconn, newslots_query, &sqlitex.ExecOptions{
Args: newslots_args,
})
}

30 changes: 2 additions & 28 deletions stiller-backend/internal/handlers/gallery/addtemplate/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,12 @@ func NetHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params
return
}


blocks := req_payload.Blocks
if len(blocks) == 0 {
handleutils.GenericLog(nil, "invalid 0 blocks template")
w.WriteHeader(http.StatusBadRequest)
return
}

newtempl_stmt := sqlf.
InsertInto("template").
NewRow().
Set("tier", req_payload.TierId).
Set("thumbnail", req_payload.ThumbnailId).
Set("res", req_payload.ResId).
Set("templatefile", req_payload.TemplateId).
Set("title", req_payload.Title).
Set("description", req_payload.Description).
Returning("id")
Expand All @@ -55,6 +47,7 @@ func NetHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params
newtemp_id = int(stmt.GetInt64("id"))
return nil
},

Args: newtempl_args,
})

Expand All @@ -68,25 +61,6 @@ func NetHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params
return
}

prep_stmt := sqlf.InsertInto("templateblock")
defer prep_stmt.Close()
for index := range blocks {
prep_stmt.NewRow().
Set("template", newtemp_id).
Set("posx", blocks[index].Posx).
Set("posy", blocks[index].Posy).
Set("direction", blocks[index].Direction)
}

blocks_execute_query, blocks_execute_args := prep_stmt.String(), prep_stmt.Args()
blocks_execute_err := sqlitex.ExecuteTransient(dbconn, blocks_execute_query, &sqlitex.ExecOptions{
Args: blocks_execute_args,
})

if handleutils.RequestLog(blocks_execute_err, "", http.StatusInternalServerError, &w) {
return
}

w.WriteHeader(http.StatusOK)
}

5 changes: 5 additions & 0 deletions stiller-backend/internal/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ type individualHandler struct {
}

var routes = [...]individualHandler{
{
path: "/gallery/new",
method: http.MethodPost,
handlefunc: addtemplate.NetHandler,
},
{
path: "/gallery/template",
method: http.MethodPost,
Expand Down

0 comments on commit e2f003d

Please sign in to comment.