feat: Allow deleting all __bad__ with a single button (#98)
This commit is contained in:
@@ -22,6 +22,8 @@ import (
|
||||
"github.com/sirrobot01/decypharr/pkg/version"
|
||||
)
|
||||
|
||||
const DeleteAllBadTorrentKey = "DELETE_ALL_BAD_TORRENTS"
|
||||
|
||||
type Handler struct {
|
||||
Name string
|
||||
logger zerolog.Logger
|
||||
@@ -308,7 +310,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
h.handlePropfind(w, r)
|
||||
return
|
||||
case "DELETE":
|
||||
if err := h.handleIDDelete(w, r); err == nil {
|
||||
if err := h.handleDelete(w, r); err == nil {
|
||||
return
|
||||
}
|
||||
// fallthrough to default
|
||||
@@ -387,21 +389,23 @@ func (h *Handler) serveDirectory(w http.ResponseWriter, r *http.Request, file we
|
||||
|
||||
// Prepare template data
|
||||
data := struct {
|
||||
Path string
|
||||
ParentPath string
|
||||
ShowParent bool
|
||||
Children []os.FileInfo
|
||||
URLBase string
|
||||
IsBadPath bool
|
||||
CanDelete bool
|
||||
Path string
|
||||
ParentPath string
|
||||
ShowParent bool
|
||||
Children []os.FileInfo
|
||||
URLBase string
|
||||
IsBadPath bool
|
||||
CanDelete bool
|
||||
DeleteAllBadTorrentKey string
|
||||
}{
|
||||
Path: cleanPath,
|
||||
ParentPath: parentPath,
|
||||
ShowParent: showParent,
|
||||
Children: children,
|
||||
URLBase: h.URLBase,
|
||||
IsBadPath: isBadPath,
|
||||
CanDelete: canDelete,
|
||||
Path: cleanPath,
|
||||
ParentPath: parentPath,
|
||||
ShowParent: showParent,
|
||||
Children: children,
|
||||
URLBase: h.URLBase,
|
||||
IsBadPath: isBadPath,
|
||||
CanDelete: canDelete,
|
||||
DeleteAllBadTorrentKey: DeleteAllBadTorrentKey,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
@@ -534,8 +538,8 @@ func (h *Handler) handleOptions(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
// handleDelete deletes a torrent from using id
|
||||
func (h *Handler) handleIDDelete(w http.ResponseWriter, r *http.Request) error {
|
||||
// handleDelete deletes a torrent by id, or all bad torrents if the id is DeleteAllBadTorrentKey
|
||||
func (h *Handler) handleDelete(w http.ResponseWriter, r *http.Request) error {
|
||||
cleanPath := path.Clean(r.URL.Path) // Remove any leading slashes
|
||||
|
||||
_, torrentId := path.Split(cleanPath)
|
||||
@@ -543,7 +547,15 @@ func (h *Handler) handleIDDelete(w http.ResponseWriter, r *http.Request) error {
|
||||
return os.ErrNotExist
|
||||
}
|
||||
|
||||
cachedTorrent := h.cache.GetTorrent(torrentId)
|
||||
if torrentId == DeleteAllBadTorrentKey {
|
||||
return h.handleDeleteAll(w)
|
||||
}
|
||||
|
||||
return h.handleDeleteById(w, torrentId)
|
||||
}
|
||||
|
||||
func (h *Handler) handleDeleteById(w http.ResponseWriter, tId string) error {
|
||||
cachedTorrent := h.cache.GetTorrent(tId)
|
||||
if cachedTorrent == nil {
|
||||
return os.ErrNotExist
|
||||
}
|
||||
@@ -552,3 +564,22 @@ func (h *Handler) handleIDDelete(w http.ResponseWriter, r *http.Request) error {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *Handler) handleDeleteAll(w http.ResponseWriter) error {
|
||||
badTorrents := h.cache.GetListing("__bad__")
|
||||
if len(badTorrents) == 0 {
|
||||
http.Error(w, "No bad torrents to delete", http.StatusNotFound)
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, fi := range badTorrents {
|
||||
tName := strings.TrimSpace(strings.SplitN(fi.Name(), "||", 2)[0])
|
||||
t := h.cache.GetTorrentByName(tName)
|
||||
if t != nil {
|
||||
h.cache.OnRemove(t.Id)
|
||||
}
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -106,6 +106,19 @@
|
||||
</li>
|
||||
{{- end}}
|
||||
{{$isBadPath := hasSuffix .Path "__bad__"}}
|
||||
{{- if and $isBadPath (gt (len .Children) 0) }}
|
||||
<li>
|
||||
<span class="file-number"> </span>
|
||||
<span class="file-name"> </span>
|
||||
<span class="file-info"> </span>
|
||||
<button
|
||||
class="delete-btn"
|
||||
id="delete-all-btn"
|
||||
data-name="{{.DeleteAllBadTorrentKey}}">
|
||||
Delete All
|
||||
</button>
|
||||
</li>
|
||||
{{- end}}
|
||||
{{- range $i, $file := .Children}}
|
||||
<li class="{{if $isBadPath}}disabled{{end}}">
|
||||
<a {{ if not $isBadPath}}href="{{urlpath (printf "%s/%s" $.Path $file.Name)}}"{{end}}>
|
||||
@@ -118,7 +131,7 @@
|
||||
</a>
|
||||
{{- if and $.CanDelete }}
|
||||
<button
|
||||
class="delete-btn"
|
||||
class="delete-btn delete-with-id-btn"
|
||||
data-name="{{$file.Name}}"
|
||||
data-path="{{printf "%s/%s" $.Path $file.ID}}">
|
||||
Delete
|
||||
@@ -128,7 +141,7 @@
|
||||
{{- end}}
|
||||
</ul>
|
||||
<script>
|
||||
document.querySelectorAll('.delete-btn').forEach(btn=>{
|
||||
document.querySelectorAll('.delete-with-id-btn').forEach(btn=>{
|
||||
btn.addEventListener('click', ()=>{
|
||||
let p = btn.getAttribute('data-path');
|
||||
let name = btn.getAttribute('data-name');
|
||||
@@ -137,6 +150,14 @@
|
||||
.then(_=>location.reload());
|
||||
});
|
||||
});
|
||||
|
||||
const deleteAllButton = document.getElementById('delete-all-btn');
|
||||
deleteAllButton.addEventListener('click', () => {
|
||||
let p = deleteAllButton.getAttribute('data-name');
|
||||
if (!confirm('Delete all entries marked Bad?')) return;
|
||||
fetch(p, { method: 'DELETE' })
|
||||
.then(_=>location.reload());
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user