image deletion works
This commit is contained in:
parent
b125b3c3ec
commit
f797337074
2 changed files with 46 additions and 13 deletions
|
@ -278,15 +278,14 @@ func apiListImages(next http.Handler, uploads string, uploadType string, udb aut
|
|||
return
|
||||
}
|
||||
|
||||
|
||||
return http.HandlerFunc(handlerFunc)
|
||||
}
|
||||
|
||||
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)
|
||||
|
|
|
@ -33,7 +33,7 @@ async function getTable(name, pass) {
|
|||
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 @@ 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}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue