more map stuff - list images and start real setup for setting map image adminside

This commit is contained in:
Iris Lightshard 2023-04-05 22:33:38 -06:00
parent 16d58eb281
commit 8f4eb7e958
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
3 changed files with 58 additions and 6 deletions

View file

@ -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<Slug>\S+)`, Validate(apiDestroyTable(renderer.JSON("result"), udb, dbAdapter), udb, scopes))
// asset management
rtr.Post(`/api/upload/(?P<Slug>\S+)/map/`, Validate(apiUploadMapImg(renderer.JSON("location"), udb, dbAdapter, uploads, "map", uploadMaxMB), udb, scopes))
rtr.Post(`/api/upload/(?P<Slug>\S+)/map/`, Validate(apiUploadImg(renderer.JSON("location"), udb, dbAdapter, uploads, "map", uploadMaxMB), udb, scopes))
// GET /api/upload/<table>/map/
rtr.Get(`/api/upload/(?P<Slug>\S+)/map/`, Validate(apiListImages(renderer.JSON("files"), uploads, "map"), udb, scopes))
// DELETE /api/upload/<table>/map/<map>
rtr.Post(`/api/upload/(?P<Slug>\S+)/token/`, Validate(apiUploadMapImg(renderer.JSON("location"), udb, dbAdapter, uploads, "token", uploadMaxMB), udb, scopes))
rtr.Post(`/api/upload/(?P<Slug>\S+)/token/`, Validate(apiUploadImg(renderer.JSON("location"), udb, dbAdapter, uploads, "token", uploadMaxMB), udb, scopes))
// DELETE /api/upload/<table>/token/<token>
return http.HandlerFunc(rtr.ServeHTTP)

View file

@ -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"`

View file

@ -14,6 +14,11 @@ async function getTable(name, pass) {
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;
document.getElementById("input_table_pass").value = pass;
@ -21,8 +26,18 @@ async function getTable(name, pass) {
infoHtml = "<a href='#' onclick='getTables()'>&larr; table list</a><br><pre>";
infoHtml += await res.text();
infoHtml += "</pre>"
infoHtml += "<input id='map_img_upload' type='file'>Map Image</input><button onclick='uploadMapImg()'>Upload</button>"
infoHtml += "<button onclick='destroyTable()'>Destroy</button>"
infoHtml += "<input id='map_img_upload' type='file'>Map Image</input><button onclick='uploadMapImg()'>Upload</button>"
if (mapImgs.ok) {
const imgs = await mapImgs.json();
console.dir(imgs);
infoHtml += "<ul>";
for (const i of imgs) {
const parts = i.split("/");
infoHtml += `<li>${parts[parts.length - 1]} <a href="${i}">view</a> <button onclick="sendMapImg(${i});">set</button> <button onclick="deleteMapImg(${i})">delete</button></li>\n`
}
infoHtml += "</ul>"
}
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");