autobrr/internal/http/release.go
soup f774831d76
feat(releases): delete older than x (#924)
* feat: delete releases older than x

* check timestamp

* incomplete front end changes

commiting changes from codespace to not lose them

* change to dropdown with options

* using int comparisons to avoid nightmares

* Revert "using int comparisons to avoid nightmares"

This reverts commit dc55966a73e9f6ad79ed28c3a3e0dbe0e35448a6.

* suggestions by stacksmash76

come back to discord @stacksmash76

* Curves - a touch of warmth in our pixel realm

* replace inline css with tailwind

* remove unnecessary comment

* align label with dropdown
changed first paragraph to something more sensible

* change font weight for duration label

* padding changes

* nitpicky

* merged divs where possible

* small adjustments for light theme

* attempt to fix for postgres

* refactor: split into component and add confirmation modal

also restyle component

* fix: go fmt

---------

Co-authored-by: ze0s <43699394+zze0s@users.noreply.github.com>
2023-05-21 18:39:28 +02:00

261 lines
6.5 KiB
Go

// Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
package http
import (
"context"
"net/http"
"net/url"
"strconv"
"github.com/autobrr/autobrr/internal/domain"
"github.com/go-chi/chi/v5"
)
type releaseService interface {
Find(ctx context.Context, query domain.ReleaseQueryParams) (res []*domain.Release, nextCursor int64, count int64, err error)
FindRecent(ctx context.Context) (res []*domain.Release, err error)
GetIndexerOptions(ctx context.Context) ([]string, error)
Stats(ctx context.Context) (*domain.ReleaseStats, error)
Delete(ctx context.Context) error
DeleteOlder(ctx context.Context, duration int) error
Retry(ctx context.Context, req *domain.ReleaseActionRetryReq) error
}
type releaseHandler struct {
encoder encoder
service releaseService
}
func newReleaseHandler(encoder encoder, service releaseService) *releaseHandler {
return &releaseHandler{
encoder: encoder,
service: service,
}
}
func (h releaseHandler) Routes(r chi.Router) {
r.Get("/", h.findReleases)
r.Get("/recent", h.findRecentReleases)
r.Get("/stats", h.getStats)
r.Get("/indexers", h.getIndexerOptions)
r.Delete("/all", h.deleteReleases)
r.Delete("/older-than/{duration}", h.deleteOlder)
r.Route("/{releaseId}", func(r chi.Router) {
r.Post("/actions/{actionStatusId}/retry", h.retryAction)
})
}
func (h releaseHandler) findReleases(w http.ResponseWriter, r *http.Request) {
limitP := r.URL.Query().Get("limit")
limit, err := strconv.Atoi(limitP)
if err != nil && limitP != "" {
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]interface{}{
"code": "BAD_REQUEST_PARAMS",
"message": "limit parameter is invalid",
})
return
}
if limit == 0 {
limit = 20
}
offsetP := r.URL.Query().Get("offset")
offset, err := strconv.Atoi(offsetP)
if err != nil && offsetP != "" {
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]interface{}{
"code": "BAD_REQUEST_PARAMS",
"message": "offset parameter is invalid",
})
return
}
cursorP := r.URL.Query().Get("cursor")
cursor := 0
if cursorP != "" {
cursor, err = strconv.Atoi(cursorP)
if err != nil && cursorP != "" {
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]interface{}{
"code": "BAD_REQUEST_PARAMS",
"message": "cursor parameter is invalid",
})
}
return
}
u, err := url.Parse(r.URL.String())
if err != nil {
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]interface{}{
"code": "BAD_REQUEST_PARAMS",
"message": "indexer parameter is invalid",
})
return
}
vals := u.Query()
indexer := vals["indexer"]
pushStatus := r.URL.Query().Get("push_status")
search := r.URL.Query().Get("q")
query := domain.ReleaseQueryParams{
Limit: uint64(limit),
Offset: uint64(offset),
Cursor: uint64(cursor),
Sort: nil,
Filters: struct {
Indexers []string
PushStatus string
}{Indexers: indexer, PushStatus: pushStatus},
Search: search,
}
releases, nextCursor, count, err := h.service.Find(r.Context(), query)
if err != nil {
h.encoder.StatusResponse(w, http.StatusInternalServerError, map[string]interface{}{
"code": "INTERNAL_SERVER_ERROR",
"message": err.Error(),
})
return
}
ret := struct {
Data []*domain.Release `json:"data"`
NextCursor int64 `json:"next_cursor"`
Count int64 `json:"count"`
}{
Data: releases,
NextCursor: nextCursor,
Count: count,
}
h.encoder.StatusResponse(w, http.StatusOK, ret)
}
func (h releaseHandler) findRecentReleases(w http.ResponseWriter, r *http.Request) {
releases, err := h.service.FindRecent(r.Context())
if err != nil {
h.encoder.StatusResponse(w, http.StatusInternalServerError, map[string]interface{}{
"code": "INTERNAL_SERVER_ERROR",
"message": err.Error(),
})
return
}
ret := struct {
Data []*domain.Release `json:"data"`
}{
Data: releases,
}
h.encoder.StatusResponse(w, http.StatusOK, ret)
}
func (h releaseHandler) getIndexerOptions(w http.ResponseWriter, r *http.Request) {
stats, err := h.service.GetIndexerOptions(r.Context())
if err != nil {
h.encoder.StatusResponse(w, http.StatusInternalServerError, map[string]interface{}{
"code": "INTERNAL_SERVER_ERROR",
"message": err.Error(),
})
return
}
h.encoder.StatusResponse(w, http.StatusOK, stats)
}
func (h releaseHandler) getStats(w http.ResponseWriter, r *http.Request) {
stats, err := h.service.Stats(r.Context())
if err != nil {
h.encoder.StatusResponse(w, http.StatusInternalServerError, map[string]interface{}{
"code": "INTERNAL_SERVER_ERROR",
"message": err.Error(),
})
return
}
h.encoder.StatusResponse(w, http.StatusOK, stats)
}
func (h releaseHandler) deleteReleases(w http.ResponseWriter, r *http.Request) {
err := h.service.Delete(r.Context())
if err != nil {
h.encoder.StatusResponse(w, http.StatusInternalServerError, map[string]interface{}{
"code": "INTERNAL_SERVER_ERROR",
"message": err.Error(),
})
return
}
h.encoder.NoContent(w)
}
func (h releaseHandler) deleteOlder(w http.ResponseWriter, r *http.Request) {
durationStr := chi.URLParam(r, "duration")
duration, err := strconv.Atoi(durationStr)
if err != nil {
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]interface{}{
"code": "BAD_REQUEST_PARAMS",
"message": "Invalid duration",
})
return
}
if err := h.service.DeleteOlder(r.Context(), duration); err != nil {
h.encoder.StatusResponse(w, http.StatusInternalServerError, map[string]interface{}{
"code": "INTERNAL_SERVER_ERROR",
"message": err.Error(),
})
return
}
h.encoder.NoContent(w)
}
func (h releaseHandler) retryAction(w http.ResponseWriter, r *http.Request) {
var (
req *domain.ReleaseActionRetryReq
err error
)
releaseIdParam := chi.URLParam(r, "releaseId")
if releaseIdParam == "" {
h.encoder.StatusError(w, http.StatusBadRequest, err)
return
}
releaseId, err := strconv.Atoi(releaseIdParam)
if err != nil {
h.encoder.StatusError(w, http.StatusBadRequest, err)
return
}
actionStatusIdParam := chi.URLParam(r, "actionStatusId")
if actionStatusIdParam == "" {
h.encoder.StatusError(w, http.StatusBadRequest, err)
return
}
actionStatusId, err := strconv.Atoi(actionStatusIdParam)
if err != nil {
h.encoder.StatusError(w, http.StatusBadRequest, err)
return
}
req = &domain.ReleaseActionRetryReq{
ReleaseId: releaseId,
ActionStatusId: actionStatusId,
}
if err := h.service.Retry(r.Context(), req); err != nil {
h.encoder.Error(w, err)
return
}
h.encoder.NoContent(w)
}