more map stuff - list images and start real setup for setting map image adminside
This commit is contained in:
parent
16d58eb281
commit
8f4eb7e958
3 changed files with 58 additions and 6 deletions
|
@ -119,6 +119,7 @@ func apiDestroyTable(next http.Handler, udb auth.UserStore, dbAdapter mongodb.Db
|
||||||
err = json.NewDecoder(req.Body).Decode(&table)
|
err = json.NewDecoder(req.Body).Decode(&table)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(400)
|
w.WriteHeader(400)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for j, t := range tables {
|
for j, t := range tables {
|
||||||
|
@ -149,7 +150,7 @@ func apiDestroyTable(next http.Handler, udb auth.UserStore, dbAdapter mongodb.Db
|
||||||
return http.HandlerFunc(handlerFunc)
|
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) {
|
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
||||||
// get table from request body
|
// get table from request body
|
||||||
r, err := req.MultipartReader()
|
r, err := req.MultipartReader()
|
||||||
|
@ -212,16 +213,18 @@ func apiUploadMapImg(next http.Handler, udb auth.UserStore, dbAdapter mongodb.Db
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
next.ServeHTTP(w, req)
|
next.ServeHTTP(w, req)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
dest.Write(fileData)
|
dest.Write(fileData)
|
||||||
dest.Close()
|
dest.Close()
|
||||||
|
|
||||||
err = dbAdapter.SetMapImageUrl(tableKey, "/uploads/"+tableKey.Name+"/"+uploadType+"/"+header.Filename)
|
err = dbAdapter.SetMapImageUrl(tableKey, "/uploads/"+tableKey.Name+"/"+uploadType+"/"+header.Filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
w.WriteHeader(500)
|
||||||
|
next.ServeHTTP(w, req)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
// respond with URL so UI can update
|
// respond with URL so UI can update
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
AddContextValue(req, "location", "/uploads/"+tableKey.Name+"/"+uploadType+"/"+header.Filename)
|
AddContextValue(req, "location", "/uploads/"+tableKey.Name+"/"+uploadType+"/"+header.Filename)
|
||||||
next.ServeHTTP(w, req)
|
next.ServeHTTP(w, req)
|
||||||
}
|
}
|
||||||
|
@ -229,6 +232,33 @@ func apiUploadMapImg(next http.Handler, udb auth.UserStore, dbAdapter mongodb.Db
|
||||||
return http.HandlerFunc(handlerFunc)
|
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 {
|
func CreateAdminInterface(udb auth.UserStore, dbAdapter mongodb.DbAdapter, uploads string, uploadMaxMB int) http.Handler {
|
||||||
// create quartzgun router
|
// create quartzgun router
|
||||||
rtr := &router.Router{Fallback: *template.Must(template.ParseFiles("static/error.html"))}
|
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))
|
rtr.Delete(`/api/table/(?P<Slug>\S+)`, Validate(apiDestroyTable(renderer.JSON("result"), udb, dbAdapter), udb, scopes))
|
||||||
|
|
||||||
// asset management
|
// 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>
|
// 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>
|
// DELETE /api/upload/<table>/token/<token>
|
||||||
|
|
||||||
return http.HandlerFunc(rtr.ServeHTTP)
|
return http.HandlerFunc(rtr.ServeHTTP)
|
||||||
|
|
|
@ -36,6 +36,7 @@ type Table struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type TableMessage struct {
|
type TableMessage struct {
|
||||||
|
Auth *string `json:"auth"`
|
||||||
Key *TableKey `json:"key"`
|
Key *TableKey `json:"key"`
|
||||||
DiceRoll *DiceRoll `json:"diceRoll"`
|
DiceRoll *DiceRoll `json:"diceRoll"`
|
||||||
Token *Token `json:"token"`
|
Token *Token `json:"token"`
|
||||||
|
|
|
@ -14,6 +14,11 @@ async function getTable(name, pass) {
|
||||||
headers: headers,
|
headers: headers,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const mapImgs = await fetch (`/admin/api/upload/${name}/map/`, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: headers,
|
||||||
|
});
|
||||||
|
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
document.getElementById("input_table_name").value = name;
|
document.getElementById("input_table_name").value = name;
|
||||||
document.getElementById("input_table_pass").value = pass;
|
document.getElementById("input_table_pass").value = pass;
|
||||||
|
@ -21,8 +26,18 @@ async function getTable(name, pass) {
|
||||||
infoHtml = "<a href='#' onclick='getTables()'>← table list</a><br><pre>";
|
infoHtml = "<a href='#' onclick='getTables()'>← table list</a><br><pre>";
|
||||||
infoHtml += await res.text();
|
infoHtml += await res.text();
|
||||||
infoHtml += "</pre>"
|
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 += "<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;
|
adminZone.innerHTML = infoHtml;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -33,6 +48,10 @@ async function getTable(name, pass) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sendMapImg(url) {
|
||||||
|
publish({mapImg: url, auth: adminToken.access_token});
|
||||||
|
}
|
||||||
|
|
||||||
async function uploadMapImg() {
|
async function uploadMapImg() {
|
||||||
try {
|
try {
|
||||||
var input = document.getElementById("map_img_upload");
|
var input = document.getElementById("map_img_upload");
|
||||||
|
|
Loading…
Reference in a new issue