2019-07-02 22:03:23 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-01-23 10:10:32 +01:00
|
|
|
"net/http"
|
|
|
|
|
2019-07-02 22:03:23 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
2022-04-03 12:37:43 +02:00
|
|
|
|
2020-07-01 17:16:24 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
2022-04-03 12:37:43 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2020-07-01 17:16:24 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-07-02 22:03:23 +02:00
|
|
|
)
|
|
|
|
|
2022-04-03 12:37:43 +02:00
|
|
|
// GetErrors searches the error logs and returns the results as JSON.
|
|
|
|
//
|
|
|
|
// GET /api/v1/errors
|
2020-07-01 17:16:24 +02:00
|
|
|
func GetErrors(router *gin.RouterGroup) {
|
2020-07-02 10:03:00 +02:00
|
|
|
router.GET("/errors", func(c *gin.Context) {
|
2022-04-03 12:37:43 +02:00
|
|
|
// Check authentication and authorization.
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceLogs, acl.ActionSearch)
|
2020-07-01 17:16:24 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2020-07-01 17:16:24 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
limit := txt.Int(c.Query("count"))
|
|
|
|
offset := txt.Int(c.Query("offset"))
|
|
|
|
|
2022-04-03 12:37:43 +02:00
|
|
|
// Find and return matching logs.
|
2020-07-07 10:51:55 +02:00
|
|
|
if resp, err := query.Errors(limit, offset, c.Query("q")); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(400, gin.H{"error": txt.UpperFirst(err.Error())})
|
2020-07-01 17:16:24 +02:00
|
|
|
return
|
|
|
|
} else {
|
2021-01-08 09:02:30 +01:00
|
|
|
AddCountHeader(c, len(resp))
|
|
|
|
AddLimitHeader(c, limit)
|
|
|
|
AddOffsetHeader(c, offset)
|
2020-07-01 17:16:24 +02:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-04-03 12:37:43 +02:00
|
|
|
|
|
|
|
// DeleteErrors removes all entries from the error logs.
|
|
|
|
//
|
|
|
|
// DELETE /api/v1/errors
|
|
|
|
func DeleteErrors(router *gin.RouterGroup) {
|
|
|
|
router.DELETE("/errors", func(c *gin.Context) {
|
2022-10-15 21:54:11 +02:00
|
|
|
conf := get.Config()
|
2022-04-03 12:37:43 +02:00
|
|
|
|
|
|
|
// Disabled in public mode so that attackers cannot cover their tracks.
|
|
|
|
if conf.Public() {
|
|
|
|
Abort(c, http.StatusForbidden, i18n.ErrPublic)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check authentication and authorization.
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceLogs, acl.ActionDelete)
|
2022-04-03 12:37:43 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2022-04-03 12:37:43 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete error logs.
|
|
|
|
if err := query.DeleteErrors(); err != nil {
|
|
|
|
log.Errorf("errors: %s (delete)", err)
|
|
|
|
AbortDeleteFailed(c)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
c.JSON(http.StatusOK, i18n.NewResponse(http.StatusOK, i18n.MsgPermanentlyDeleted))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|