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