diff --git a/admin/admin.go b/admin/admin.go index 13526d4..e3fd616 100644 --- a/admin/admin.go +++ b/admin/admin.go @@ -273,11 +273,10 @@ func apiListImages(next http.Handler, uploads string, uploadType string, udb aut } } } - 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 apiListImages(next http.Handler, uploads string, uploadType string, udb aut 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 @@ urlParams := req.Context().Value("params").(map[string]string) 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) diff --git a/static/admin.js b/static/admin.js index 8f76290..04c87e6 100644 --- a/static/admin.js +++ b/static/admin.js @@ -33,7 +33,7 @@ async function getTable(name, pass) { infoHtml += ""; } else { @@ -86,9 +86,31 @@ async function uploadMapImg() { } } -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}`); } }