Files
ds-galo/backend/internal/photos/handlers.go
T
iu aa396bdb85 feat: photo rename and viewer delete with confirmation
Backend:
- add PATCH /api/photos/:id to rename original_filename (uploader/owner/admin)
- emit photo.updated SSE event

Frontend:
- inline filename editor in the right panel
- viewer: back button (arrow_back) on the left, delete on the right
- delete confirmed via a modal dialog; filmstrip updates after deletion
2026-10-10 18:50:16 +03:00

130 lines
3.1 KiB
Go

package photos
import (
"io"
"net/http"
"strconv"
"time"
"github.com/gin-gonic/gin"
"shared-album/internal/auth"
"shared-album/internal/httpx"
)
const maxUploadSize = 50 << 20
type Handler struct {
svc *Service
}
func NewHandler(svc *Service) *Handler { return &Handler{svc: svc} }
func (h *Handler) List(c *gin.Context) {
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "50"))
var since *time.Time
if raw := c.Query("since"); raw != "" {
if t, err := time.Parse(time.RFC3339, raw); err == nil {
since = &t
} else if ms, err := strconv.ParseInt(raw, 10, 64); err == nil {
t := time.UnixMilli(ms)
since = &t
}
}
list, next, err := h.svc.List(c.Request.Context(), c.Param("id"), auth.UserID(c), c.Query("cursor"), since, limit)
if err != nil {
httpx.RespondError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{"photos": list, "next_cursor": next})
}
func (h *Handler) Upload(c *gin.Context) {
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxUploadSize)
form, err := c.MultipartForm()
if err != nil {
httpx.RespondError(c, httpx.BadRequest("invalid_request", "expected multipart form"))
return
}
files := form.File["files"]
if len(files) == 0 {
files = form.File["file"]
}
if len(files) == 0 {
httpx.RespondError(c, httpx.BadRequest("no_files", "no files provided"))
return
}
created := make([]any, 0, len(files))
for _, fh := range files {
f, err := fh.Open()
if err != nil {
httpx.RespondError(c, err)
return
}
data, err := io.ReadAll(io.LimitReader(f, maxUploadSize))
f.Close()
if err != nil {
httpx.RespondError(c, err)
return
}
p, err := h.svc.Upload(c.Request.Context(), UploadInput{
AlbumID: c.Param("id"),
UserID: auth.UserID(c),
Filename: fh.Filename,
MimeType: fh.Header.Get("Content-Type"),
Data: data,
})
if err != nil {
httpx.RespondError(c, err)
return
}
created = append(created, p)
}
c.JSON(http.StatusCreated, gin.H{"photos": created})
}
func (h *Handler) Get(c *gin.Context) {
p, err := h.svc.Get(c.Request.Context(), c.Param("id"), auth.UserID(c))
if err != nil {
httpx.RespondError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{"photo": p})
}
type renameRequest struct {
OriginalFilename string `json:"original_filename" binding:"required"`
}
func (h *Handler) Rename(c *gin.Context) {
var req renameRequest
if err := c.ShouldBindJSON(&req); err != nil {
httpx.RespondError(c, httpx.BadRequest("invalid_request", "original_filename is required"))
return
}
p, err := h.svc.Rename(c.Request.Context(), c.Param("id"), auth.UserID(c), req.OriginalFilename)
if err != nil {
httpx.RespondError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{"photo": p})
}
func (h *Handler) Download(c *gin.Context) {
url, err := h.svc.Download(c.Request.Context(), c.Param("id"), auth.UserID(c))
if err != nil {
httpx.RespondError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{"url": url})
}
func (h *Handler) Delete(c *gin.Context) {
if err := h.svc.Delete(c.Request.Context(), c.Param("id"), auth.UserID(c)); err != nil {
httpx.RespondError(c, err)
return
}
c.Status(http.StatusNoContent)
}