72 lines
2.2 KiB
Go
72 lines
2.2 KiB
Go
|
|
package handlers
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"net/http"
|
||
|
|
"strconv"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/flatrender/render-svc/internal/models"
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
"github.com/google/uuid"
|
||
|
|
)
|
||
|
|
|
||
|
|
// GET /v1/admin-exports — all exports across users (admin)
|
||
|
|
func (h *ExportHandler) AdminList(c *gin.Context) {
|
||
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||
|
|
if page < 1 {
|
||
|
|
page = 1
|
||
|
|
}
|
||
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "30"))
|
||
|
|
if pageSize < 1 || pageSize > 100 {
|
||
|
|
pageSize = 30
|
||
|
|
}
|
||
|
|
exports, total, err := h.store.ListAllExports(c.Request.Context(), page, pageSize)
|
||
|
|
if err != nil {
|
||
|
|
c.JSON(http.StatusInternalServerError, models.APIError{Code: "internal_error", Message: err.Error()})
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if exports == nil {
|
||
|
|
exports = []*models.Export{}
|
||
|
|
}
|
||
|
|
c.JSON(http.StatusOK, models.PagedResponse[*models.Export]{
|
||
|
|
Data: exports,
|
||
|
|
Meta: models.PaginationMeta{Page: page, PageSize: pageSize, Total: total},
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
// DELETE /v1/admin-exports/:export_id (admin)
|
||
|
|
func (h *ExportHandler) AdminDelete(c *gin.Context) {
|
||
|
|
exportID, err := uuid.Parse(c.Param("export_id"))
|
||
|
|
if err != nil {
|
||
|
|
c.JSON(http.StatusBadRequest, models.APIError{Code: "bad_request", Message: "invalid export_id"})
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if err := h.store.SoftDeleteExportAny(c.Request.Context(), exportID); err != nil {
|
||
|
|
c.JSON(http.StatusNotFound, models.APIError{Code: "not_found", Message: err.Error()})
|
||
|
|
return
|
||
|
|
}
|
||
|
|
c.Status(http.StatusNoContent)
|
||
|
|
}
|
||
|
|
|
||
|
|
// GET /v1/admin-exports/:export_id/download-url (admin)
|
||
|
|
func (h *ExportHandler) AdminDownloadURL(c *gin.Context) {
|
||
|
|
exportID, err := uuid.Parse(c.Param("export_id"))
|
||
|
|
if err != nil {
|
||
|
|
c.JSON(http.StatusBadRequest, models.APIError{Code: "bad_request", Message: "invalid export_id"})
|
||
|
|
return
|
||
|
|
}
|
||
|
|
exp, err := h.store.GetExportByIDAny(c.Request.Context(), exportID)
|
||
|
|
if err != nil {
|
||
|
|
c.JSON(http.StatusNotFound, models.APIError{Code: "not_found", Message: err.Error()})
|
||
|
|
return
|
||
|
|
}
|
||
|
|
expiry := 15 * time.Minute
|
||
|
|
url, err := h.minio.PresignedGetObject(context.Background(), h.bucket, exp.Path, expiry, nil)
|
||
|
|
if err != nil {
|
||
|
|
c.JSON(http.StatusInternalServerError, models.APIError{Code: "internal_error", Message: "could not generate download URL"})
|
||
|
|
return
|
||
|
|
}
|
||
|
|
c.JSON(http.StatusOK, gin.H{"url": url.String(), "expires_at": time.Now().Add(expiry)})
|
||
|
|
}
|