From 8f4eb7e9582b5057050d7177bd4a0e5aaacf5118 Mon Sep 17 00:00:00 2001 From: Derek Stevens Date: Wed, 5 Apr 2023 22:33:38 -0600 Subject: [PATCH] more map stuff - list images and start real setup for setting map image adminside --- admin/admin.go | 42 +++++++++++++++++++++++++++++++++++++----- models/models.go | 1 + static/admin.js | 21 ++++++++++++++++++++- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/admin/admin.go b/admin/admin.go index 67546a9..f17c8d0 100644 --- a/admin/admin.go +++ b/admin/admin.go @@ -119,6 +119,7 @@ func apiDestroyTable(next http.Handler, udb auth.UserStore, dbAdapter mongodb.Db err = json.NewDecoder(req.Body).Decode(&table) if err != nil { w.WriteHeader(400) + return } for j, t := range tables { @@ -149,7 +150,7 @@ func apiDestroyTable(next http.Handler, udb auth.UserStore, dbAdapter mongodb.Db return http.HandlerFunc(handlerFunc) } -func apiUploadMapImg(next http.Handler, udb auth.UserStore, dbAdapter mongodb.DbAdapter, uploads, uploadType string, uploadMax int) http.Handler { +func apiUploadImg(next http.Handler, udb auth.UserStore, dbAdapter mongodb.DbAdapter, uploads, uploadType string, uploadMax int) http.Handler { handlerFunc := func(w http.ResponseWriter, req *http.Request) { // get table from request body r, err := req.MultipartReader() @@ -212,16 +213,18 @@ func apiUploadMapImg(next http.Handler, udb auth.UserStore, dbAdapter mongodb.Db fmt.Println(err.Error()) w.WriteHeader(500) next.ServeHTTP(w, req) + return } dest.Write(fileData) dest.Close() err = dbAdapter.SetMapImageUrl(tableKey, "/uploads/"+tableKey.Name+"/"+uploadType+"/"+header.Filename) if err != nil { - fmt.Println(err.Error()) + w.WriteHeader(500) + next.ServeHTTP(w, req) + return } // respond with URL so UI can update - w.Header().Set("Content-Type", "application/json") AddContextValue(req, "location", "/uploads/"+tableKey.Name+"/"+uploadType+"/"+header.Filename) next.ServeHTTP(w, req) } @@ -229,6 +232,33 @@ func apiUploadMapImg(next http.Handler, udb auth.UserStore, dbAdapter mongodb.Db return http.HandlerFunc(handlerFunc) } +func apiListImages(next http.Handler, uploads string, uploadType string) http.Handler { + handlerFunc := func(w http.ResponseWriter, req *http.Request) { + urlParams := req.Context().Value("params").(map[string]string) + tableName := urlParams["Slug"] + destPath := filepath.Join(uploads, tableName, uploadType) + files, err := ioutil.ReadDir(destPath) + + if err != nil { + w.WriteHeader(500) + next.ServeHTTP(w, req) + return + } + + fileNames := []string{} + for _, file := range files { + if !file.IsDir() { + fileNames = append(fileNames, "/uploads/"+tableName+"/"+uploadType+"/"+file.Name()) + } + } + + AddContextValue(req, "files", fileNames) + next.ServeHTTP(w, req) + } + + return http.HandlerFunc(handlerFunc) +} + func CreateAdminInterface(udb auth.UserStore, dbAdapter mongodb.DbAdapter, uploads string, uploadMaxMB int) http.Handler { // create quartzgun router rtr := &router.Router{Fallback: *template.Must(template.ParseFiles("static/error.html"))} @@ -244,9 +274,11 @@ func CreateAdminInterface(udb auth.UserStore, dbAdapter mongodb.DbAdapter, uploa rtr.Delete(`/api/table/(?P\S+)`, Validate(apiDestroyTable(renderer.JSON("result"), udb, dbAdapter), udb, scopes)) // asset management - rtr.Post(`/api/upload/(?P\S+)/map/`, Validate(apiUploadMapImg(renderer.JSON("location"), udb, dbAdapter, uploads, "map", uploadMaxMB), udb, scopes)) + rtr.Post(`/api/upload/(?P\S+)/map/`, Validate(apiUploadImg(renderer.JSON("location"), udb, dbAdapter, uploads, "map", uploadMaxMB), udb, scopes)) + // GET /api/upload//map/ + rtr.Get(`/api/upload/(?P\S+)/map/`, Validate(apiListImages(renderer.JSON("files"), uploads, "map"), udb, scopes)) // DELETE /api/upload/
/map/ - rtr.Post(`/api/upload/(?P\S+)/token/`, Validate(apiUploadMapImg(renderer.JSON("location"), udb, dbAdapter, uploads, "token", uploadMaxMB), udb, scopes)) + rtr.Post(`/api/upload/(?P\S+)/token/`, Validate(apiUploadImg(renderer.JSON("location"), udb, dbAdapter, uploads, "token", uploadMaxMB), udb, scopes)) // DELETE /api/upload/
/token/ return http.HandlerFunc(rtr.ServeHTTP) diff --git a/models/models.go b/models/models.go index 8362286..d9aa989 100644 --- a/models/models.go +++ b/models/models.go @@ -36,6 +36,7 @@ type Table struct { } type TableMessage struct { + Auth *string `json:"auth"` Key *TableKey `json:"key"` DiceRoll *DiceRoll `json:"diceRoll"` Token *Token `json:"token"` diff --git a/static/admin.js b/static/admin.js index 3f633c6..de5df99 100644 --- a/static/admin.js +++ b/static/admin.js @@ -13,6 +13,11 @@ async function getTable(name, pass) { method: 'GET', headers: headers, }); + + const mapImgs = await fetch (`/admin/api/upload/${name}/map/`, { + method: 'GET', + headers: headers, + }); if (res.ok) { document.getElementById("input_table_name").value = name; @@ -21,8 +26,18 @@ async function getTable(name, pass) { infoHtml = "← table list
";
       infoHtml += await res.text();
       infoHtml += "
" - infoHtml += "Map Image" infoHtml += "" + infoHtml += "Map Image" + if (mapImgs.ok) { + const imgs = await mapImgs.json(); + console.dir(imgs); + infoHtml += "
    "; + for (const i of imgs) { + const parts = i.split("/"); + infoHtml += `
  • ${parts[parts.length - 1]} view
  • \n` + } + infoHtml += "
" + } adminZone.innerHTML = infoHtml; } else { @@ -33,6 +48,10 @@ async function getTable(name, pass) { } } +function sendMapImg(url) { + publish({mapImg: url, auth: adminToken.access_token}); +} + async function uploadMapImg() { try { var input = document.getElementById("map_img_upload");