0ec28660ba
ci/woodpecker/tag/docker Pipeline was successful
Follow-up to #99. ## Why Evicting or deleting a local RPM removed the \`local_files\` row but left its \`rpm_metadata\` behind. Since generated repodata is built from \`rpm_metadata\`, \`primary.xml\` kept advertising a package that no longer exists, producing 404s for clients that tried to fetch it. ## Changes - Add \`PostDeleteHook\` and \`MetadataDeleter\` provider interfaces (symmetric to the existing \`PostUploadHook\`/\`MetadataStore\`), plus a \`DeleteRPMMetadata\` DB method. - Implement \`AfterDelete\` in the RPM provider to drop the metadata row for the deleted file. - Route both local delete paths — the new \`evictLocal\` and the existing files handler's \`remove\` — through a shared \`deleteLocalFile\` helper that removes the file then runs the provider's post-delete hook. Non-RPM providers have no hook, so nothing changes for them. - Cover the cleanup with a dockerised test. Reviewed-on: #100 Co-authored-by: Ben Vincent <ben@unkin.net> Co-committed-by: Ben Vincent <ben@unkin.net>
95 lines
2.4 KiB
Go
95 lines
2.4 KiB
Go
package v2
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"git.unkin.net/unkin/artifactapi/internal/database"
|
|
)
|
|
|
|
type ObjectsHandler struct {
|
|
db *database.DB
|
|
}
|
|
|
|
func NewObjectsHandler(db *database.DB) *ObjectsHandler {
|
|
return &ObjectsHandler{db: db}
|
|
}
|
|
|
|
func (h *ObjectsHandler) Routes() chi.Router {
|
|
r := chi.NewRouter()
|
|
r.Get("/", h.list)
|
|
r.Delete("/*", h.evict)
|
|
return r
|
|
}
|
|
|
|
// LocalRoutes lists and evicts objects for local repos, which live in the
|
|
// local_files table rather than the artifacts table used by remotes.
|
|
func (h *ObjectsHandler) LocalRoutes() chi.Router {
|
|
r := chi.NewRouter()
|
|
r.Get("/", h.listLocal)
|
|
r.Delete("/*", h.evictLocal)
|
|
return r
|
|
}
|
|
|
|
// pageBounds parses the shared page/per_page query params into a SQL limit and offset.
|
|
func pageBounds(r *http.Request) (limit, offset int) {
|
|
limit, _ = strconv.Atoi(r.URL.Query().Get("per_page"))
|
|
if limit <= 0 || limit > 5000 {
|
|
limit = 50
|
|
}
|
|
page, _ := strconv.Atoi(r.URL.Query().Get("page"))
|
|
if page <= 0 {
|
|
page = 1
|
|
}
|
|
return limit, (page - 1) * limit
|
|
}
|
|
|
|
func (h *ObjectsHandler) list(w http.ResponseWriter, r *http.Request) {
|
|
remoteName := chi.URLParam(r, "name")
|
|
limit, offset := pageBounds(r)
|
|
|
|
artifacts, err := h.db.ListArtifacts(r.Context(), remoteName, limit, offset)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, artifacts)
|
|
}
|
|
|
|
func (h *ObjectsHandler) listLocal(w http.ResponseWriter, r *http.Request) {
|
|
repoName := chi.URLParam(r, "name")
|
|
limit, offset := pageBounds(r)
|
|
|
|
artifacts, err := h.db.ListLocalArtifacts(r.Context(), repoName, limit, offset)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, artifacts)
|
|
}
|
|
|
|
func (h *ObjectsHandler) evictLocal(w http.ResponseWriter, r *http.Request) {
|
|
repoName := chi.URLParam(r, "name")
|
|
path := chi.URLParam(r, "*")
|
|
|
|
if err := deleteLocalFile(r.Context(), h.db, repoName, path); err != nil {
|
|
http.Error(w, fmt.Sprintf("evict failed: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (h *ObjectsHandler) evict(w http.ResponseWriter, r *http.Request) {
|
|
remoteName := chi.URLParam(r, "name")
|
|
path := chi.URLParam(r, "*")
|
|
|
|
if err := h.db.DeleteArtifact(r.Context(), remoteName, path); err != nil {
|
|
http.Error(w, fmt.Sprintf("evict failed: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|