mirror of
https://github.com/photoprism/photoprism.git
synced 2025-12-12 00:34:13 +01:00
75 lines
2.2 KiB
Go
75 lines
2.2 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/photoprism/photoprism/internal/ai/vision"
|
|
"github.com/photoprism/photoprism/internal/auth/acl"
|
|
"github.com/photoprism/photoprism/internal/photoprism/get"
|
|
"github.com/photoprism/photoprism/pkg/media"
|
|
"github.com/photoprism/photoprism/pkg/media/http/header"
|
|
)
|
|
|
|
// PostVisionNsfw checks the specified images for inappropriate content.
|
|
//
|
|
// @Summary checks the specified images for inappropriate content
|
|
// @Id PostVisionNsfw
|
|
// @Tags Vision
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} vision.ApiResponse
|
|
// @Failure 401,403,429 {object} i18n.Response
|
|
// @Param images body vision.ApiRequest true "list of image file urls"
|
|
// @Router /api/v1/vision/nsfw [post]
|
|
func PostVisionNsfw(router *gin.RouterGroup) {
|
|
router.POST("/vision/nsfw", func(c *gin.Context) {
|
|
s := Auth(c, acl.ResourceVision, acl.AccessAll)
|
|
|
|
// Abort if permission is not granted.
|
|
if s.Abort(c) {
|
|
return
|
|
}
|
|
|
|
var request vision.ApiRequest
|
|
|
|
// File uploads are not currently supported for this API endpoint.
|
|
if header.HasContentType(&c.Request.Header, header.ContentTypeMultipart) {
|
|
c.JSON(http.StatusBadRequest, vision.NewApiError(request.GetId(), http.StatusBadRequest))
|
|
return
|
|
}
|
|
|
|
// Assign and validate request form values.
|
|
if err := c.BindJSON(&request); err != nil {
|
|
c.JSON(http.StatusBadRequest, vision.NewApiError(request.GetId(), http.StatusBadRequest))
|
|
return
|
|
}
|
|
|
|
// Check if the Computer Vision API is enabled, otherwise abort with an error.
|
|
if !get.Config().VisionApi() {
|
|
AbortFeatureDisabled(c)
|
|
c.JSON(http.StatusForbidden, vision.NewApiError(request.GetId(), http.StatusForbidden))
|
|
return
|
|
}
|
|
|
|
// Run inference to check the specified images for inappropriate content.
|
|
results, err := vision.Nsfw(request.Images, media.SrcRemote)
|
|
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, vision.NewApiError(request.GetId(), http.StatusBadRequest))
|
|
return
|
|
}
|
|
|
|
// Generate Vision API service response.
|
|
response := vision.ApiResponse{
|
|
Id: request.GetId(),
|
|
Code: http.StatusOK,
|
|
Model: &vision.Model{Name: vision.NsfwModel.Name},
|
|
Result: &vision.ApiResult{Nsfw: results},
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response)
|
|
})
|
|
}
|