image deletion works

This commit is contained in:
Iris Lightshard 2023-07-04 23:54:43 -06:00
parent b125b3c3ec
commit f797337074
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
2 changed files with 46 additions and 13 deletions

View file

@ -273,11 +273,10 @@ func apiListImages(next http.Handler, uploads string, uploadType string, udb aut
} }
} }
} }
w.WriteHeader(422) w.WriteHeader(422)
next.ServeHTTP(w, req) next.ServeHTTP(w, req)
return return
} }
return http.HandlerFunc(handlerFunc) 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 { 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) { handlerFunc := func(w http.ResponseWriter, req *http.Request) {
// put the path together // put the path together
urlParams := req.Context().Value("params").(map[string]string) urlParams := req.Context().Value("params").(map[string]string)
tableName := urlParams["Slug"] tableName := urlParams["table"]
tableKey := models.TableKey{ tableKey := models.TableKey{
Name: tableName, Name: tableName,
Passcode: req.FormValue("passcode"), Passcode: req.FormValue("passcode"),
@ -308,12 +307,24 @@ urlParams := req.Context().Value("params").(map[string]string)
if ok { if ok {
if dbAdapter.CheckTable(tableKey) { 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 // otherwise, return an error
w.WriteHeader(500)
next.ServeHTTP(w, req)
} }
return http.HandlerFunc(handlerFunc) return http.HandlerFunc(handlerFunc)

View file

@ -33,7 +33,7 @@ async function getTable(name, pass) {
infoHtml += "<ul>"; infoHtml += "<ul>";
for (const i of imgs) { for (const i of imgs) {
const parts = i.split("/"); 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>"; infoHtml += "</ul>";
} else { } else {
@ -86,9 +86,31 @@ async function uploadMapImg() {
} }
} }
async function deleteMapImg() { async function deleteImg(url) {
try { 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}`);
} }
} }