all repos — felt @ f7973370742f0cb864c52ce7824edee1f332007f

virtual tabletop for dungeons and dragons (and similar) using Go, MongoDB, and websockets

image deletion works
Iris Lightshard nilix@nilfm.cc
PGP Signature
-----BEGIN PGP SIGNATURE-----

iQIzBAABCAAdFiEEkFh6dA+k/6CXFXU4O3+8IhROY5gFAmSlBaMACgkQO3+8IhRO
Y5gN9g//Z6hdPKyxEXV+j+IdeIlBjo+DVVeBUxGUeahNzMo+bQkK4NDqZ1ND+OJZ
WWR07TM9DFa8dSPLHfi2RNvd0ihMkoOPmLkXqVrvZO0TK9qPja13OPJaaQnhlYzR
SPhixpLZkVubOK+5XQdw1BzL305UOhGQVTtcJdWyh6qpDj1vi8FuKUty0yvELF9C
2/RDQxDHA3CdVJRA89FrX2EeSZNlZlm9udjNnHvwPcJeUMT/Z0+XhfCagIv4fTD2
D9tjCEHl/i/4T2xkD8AD9bUGQTcVLKAuYBzQb1M0UlLEqdV87xid1u3sQJdxR7qn
oskg3vOUWA7z+dFTvDQ3Vj+agkXSPujVi/yQqV7fdvg3708vg/B6l3ryY/9cL7lB
T4gLioZ885iMVyt8/fG0lIISfg/21FgkdWVwY0fS3A3+UacJ8JbUZeIYiZSUpt2f
yaB5u06YwxRuGUm7iop7jBsMqf439sZCLzhi0eDX7I6Eqw+5plCD8TREYHvpYRwt
xmtq7VTlkPIhVPmKQoN9J5oAuilFPYsAdedZSuunIMq49qA3qxhyTvdBPY1dO32+
FsJElioK8aumEtYAha0pmmipISc9Ms+5DyDH/SJ4z49JN3PXIexNqAOmOsOKXxVW
ur0TY89O7W0X9grgI3V32zg1iwv+XA8TKQskBn6sB0hTuwIgI2o=
=cH9w
-----END PGP SIGNATURE-----
commit

f7973370742f0cb864c52ce7824edee1f332007f

parent

b125b3c3ec48c60e783345994b3a1a8ac58f098c

2 files changed, 46 insertions(+), 13 deletions(-)

jump to
M admin/admin.goadmin/admin.go

@@ -273,11 +273,10 @@ return

} } } - w.WriteHeader(422) - next.ServeHTTP(w, req) - return + w.WriteHeader(422) + next.ServeHTTP(w, req) + return } - return http.HandlerFunc(handlerFunc) }

@@ -285,8 +284,8 @@

func apiDeleteImage(next http.Handler, uploads string, uploadType string, udb auth.UserStore, dbAdapter mongodb.DbAdapter) http.Handler { handlerFunc := func(w http.ResponseWriter, req *http.Request) { // put the path together -urlParams := req.Context().Value("params").(map[string]string) - tableName := urlParams["Slug"] + urlParams := req.Context().Value("params").(map[string]string) + tableName := urlParams["table"] tableKey := models.TableKey{ Name: tableName, Passcode: req.FormValue("passcode"),

@@ -308,12 +307,24 @@ }

if ok { if dbAdapter.CheckTable(tableKey) { - // if the file exists, delete it and return the deleted path - } - } + // if the file exists, delete it and return 201 + filename := urlParams["file"] + fullPath := filepath.Join(uploads, tableName, uploadType, filename) + s, err := os.Stat(fullPath) + if err == nil && !s.IsDir() { + err = os.Remove(fullPath) + if err == nil { + w.WriteHeader(201) + next.ServeHTTP(w, req) + return + } + } + } + } } - // otherwise, return an error + w.WriteHeader(500) + next.ServeHTTP(w, req) } return http.HandlerFunc(handlerFunc)
M static/admin.jsstatic/admin.js

@@ -33,7 +33,7 @@ const imgs = await mapImgs.json();

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 += `<li>${parts[parts.length - 1]} <a href="${i}">view</a> <button onclick="sendMapImg('${i}');">Set</button> <button onclick="deleteImg('${i}')">Delete</button></li>\n`; } infoHtml += "</ul>"; } else {

@@ -86,9 +86,31 @@ setErr(`${err.name}: ${err.message}`);

} } -async function deleteMapImg() { +async function deleteImg(url) { try { - } catch { + if (url.startsWith("/uploads/")) { + const parts = url.split("/"); + parts.shift(); + const table = parts[1]; + const imgType = parts[2] + const file = parts[3]; + + const headers = new Headers(); + headers.set('Authorization', 'Bearer ' + adminToken.access_token); + const res = await fetch(`/admin/api/upload/${table}/${imgType}/${file}?passcode=${tableKey.passcode}`, { + headers: headers, + method: "DELETE", + }); + + if (res.ok) { + // refresh UI + getTable(tableKey.name, tableKey.passcode); + } else { + throw new Error ("Something went wrong deleting the image..."); + } + } + } catch (err) { + setErr(`${err.name}: ${err.message}`); } }