photoprism/internal/api/lenses.go
Michael Mayer bc327016ad Metadata: Add Camera Make and Model updates via CLI & API #5663 #5656
Mirror the lens make/model editing surface for cameras: entity UpdateMakeModel/SaveForm, form validation, query, search, the GET/PUT /api/v1/cameras endpoints, and the cameras CLI command, plus a cameras ACL resource and scope.

Also tidy the lens surface for parity: self-validating SaveForm, empty make/model guard, X-Count search header, service-role grant, the empty-id/slug docs, and order cameras before lenses everywhere.
2026-06-15 09:25:44 +00:00

81 lines
2 KiB
Go

package api
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/photoprism/photoprism/internal/auth/acl"
"github.com/photoprism/photoprism/internal/entity/query"
"github.com/photoprism/photoprism/internal/form"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/i18n"
)
// UpdateLens updates lens make and model properties.
//
// PUT /api/v1/lenses/:id
//
// @Summary updates lens name
// @Id UpdateLens
// @Tags Lenses
// @Accept json
// @Produce json
// @Success 200 {object} entity.Lens
// @Failure 401,403,404,429 {object} i18n.Response
// @Param id path string true "Lens ID"
// @Param lens body form.Lens true "Properties to be updated, only Make and Model supported"
// @Router /api/v1/lenses/{id} [put]
func UpdateLens(router *gin.RouterGroup) {
router.PUT("/lenses/:id", func(c *gin.Context) {
s := Auth(c, acl.ResourceLenses, acl.ActionUpdate)
if s.Abort(c) {
return
}
// Find lens by ID. A non-numeric id parses to 0 and is rejected as not found below,
// so the parse error is intentionally ignored.
lensId, _ := strconv.ParseUint(clean.Token(c.Param("id")), 10, 32)
m := query.FindLensByID(uint(lensId))
if m == nil {
Abort(c, http.StatusNotFound, i18n.ErrLensNotFound)
return
}
// Create new lens form.
frm, frmErr := form.NewLens(m)
if frmErr != nil {
Abort(c, http.StatusBadRequest, i18n.ErrBadRequest)
return
}
// Set form values from request.
LimitRequestBodyBytes(c, MaxMutationRequestBytes)
if frmErr = c.BindJSON(frm); frmErr != nil {
if IsRequestBodyTooLarge(frmErr) {
AbortRequestTooLarge(c, i18n.ErrBadRequest)
return
}
AbortBadRequest(c, frmErr)
return
} else if frmErr = frm.Validate(); frmErr != nil {
AbortInvalidName(c)
return
}
// Save lens and return new model values if successful.
if err := m.SaveForm(frm); err != nil {
log.Errorf("lens: %s", clean.Error(err))
AbortSaveFailed(c)
return
}
c.JSON(http.StatusOK, m)
})
}