2020-03-28 15:29:17 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gin-gonic/gin/binding"
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2020-05-08 15:41:01 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2020-04-06 10:26:26 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/workers"
|
2020-03-28 15:29:17 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GET /api/v1/accounts
|
|
|
|
func GetAccounts(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.GET("/accounts", func(c *gin.Context) {
|
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var f form.AccountSearch
|
|
|
|
|
|
|
|
err := c.MustBindWith(&f, binding.Form)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
result, err := query.AccountSearch(f)
|
2020-03-28 15:29:17 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(400, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-20 20:07:58 +02:00
|
|
|
// TODO c.Header("X-Count", strconv.Itoa(count))
|
|
|
|
c.Header("X-Limit", strconv.Itoa(f.Count))
|
|
|
|
c.Header("X-Offset", strconv.Itoa(f.Offset))
|
2020-03-28 15:29:17 +01:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET /api/v1/accounts/:id
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// id: string Account ID as returned by the API
|
|
|
|
func GetAccount(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.GET("/accounts/:id", func(c *gin.Context) {
|
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id := ParseUint(c.Param("id"))
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
if m, err := query.AccountByID(id); err == nil {
|
2020-03-28 15:29:17 +01:00
|
|
|
c.JSON(http.StatusOK, m)
|
|
|
|
} else {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrAccountNotFound)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-02 18:17:07 +02:00
|
|
|
// GET /api/v1/accounts/:id/dirs
|
2020-03-31 17:26:25 +02:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// id: string Account ID as returned by the API
|
2020-04-02 18:17:07 +02:00
|
|
|
func GetAccountDirs(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.GET("/accounts/:id/dirs", func(c *gin.Context) {
|
2020-03-31 17:26:25 +02:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id := ParseUint(c.Param("id"))
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
m, err := query.AccountByID(id)
|
2020-03-31 17:26:25 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrAccountNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-02 18:17:07 +02:00
|
|
|
list, err := m.Directories()
|
2020-03-31 17:26:25 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("account: %s", err.Error())
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrConnectionFailed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, list)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-01 12:00:45 +02:00
|
|
|
// GET /api/v1/accounts/:id/share
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// id: string Account ID as returned by the API
|
|
|
|
func ShareWithAccount(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.POST("/accounts/:id/share", func(c *gin.Context) {
|
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id := ParseUint(c.Param("id"))
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
m, err := query.AccountByID(id)
|
2020-04-01 12:00:45 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrAccountNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var f form.AccountShare
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dst := f.Destination
|
2020-05-23 20:58:58 +02:00
|
|
|
files, err := query.FilesByUID(f.Photos, 1000, 0)
|
2020-04-01 12:00:45 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(404, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
dstFileName := dst + "/" + file.ShareFileName()
|
|
|
|
|
2020-04-01 18:17:29 +02:00
|
|
|
fileShare := entity.NewFileShare(file.ID, m.ID, dstFileName)
|
2020-05-26 11:00:39 +02:00
|
|
|
entity.FirstOrCreateFileShare(fileShare)
|
2020-04-01 12:00:45 +02:00
|
|
|
}
|
|
|
|
|
2020-04-06 10:26:26 +02:00
|
|
|
workers.StartShare(conf)
|
|
|
|
|
2020-04-01 12:00:45 +02:00
|
|
|
c.JSON(http.StatusOK, files)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-28 15:29:17 +01:00
|
|
|
// POST /api/v1/accounts
|
|
|
|
func CreateAccount(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.POST("/accounts", func(c *gin.Context) {
|
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var f form.Account
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:02:01 +02:00
|
|
|
if err := f.ServiceDiscovery(); err != nil {
|
|
|
|
log.Error(err)
|
2020-03-29 21:24:44 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
2020-03-29 12:02:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
m, err := entity.CreateAccount(f)
|
2020-03-28 15:29:17 +01:00
|
|
|
|
|
|
|
log.Debugf("create account: %+v %+v", f, m)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2020-03-29 21:24:44 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
2020-03-28 15:29:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
event.Success("account created")
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, m)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// PUT /api/v1/accounts/:id
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// id: string Account ID as returned by the API
|
|
|
|
func UpdateAccount(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.PUT("/accounts/:id", func(c *gin.Context) {
|
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id := ParseUint(c.Param("id"))
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
m, err := query.AccountByID(id)
|
2020-03-28 15:29:17 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1) Init form with model values
|
|
|
|
f, err := form.NewAccount(m)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-04-20 10:38:01 +02:00
|
|
|
log.Error(err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, ErrSaveFailed)
|
2020-03-28 15:29:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2) Update form with values from request
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-04-20 10:38:01 +02:00
|
|
|
log.Error(err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, ErrFormInvalid)
|
2020-03-28 15:29:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3) Save model with values from form
|
2020-05-26 11:00:39 +02:00
|
|
|
if err := m.SaveForm(f); err != nil {
|
2020-04-20 10:38:01 +02:00
|
|
|
log.Error(err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, ErrSaveFailed)
|
2020-03-28 15:29:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
event.Success("account saved")
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
m, err = query.AccountByID(id)
|
2020-03-28 15:29:17 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrAccountNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, m)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DELETE /api/v1/accounts/:id
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// id: string Account ID as returned by the API
|
|
|
|
func DeleteAccount(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.DELETE("/accounts/:id", func(c *gin.Context) {
|
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id := ParseUint(c.Param("id"))
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
m, err := query.AccountByID(id)
|
2020-03-28 15:29:17 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrAccountNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
if err := m.Delete(); err != nil {
|
2020-03-29 21:24:44 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, txt.UcFirst(err.Error()))
|
2020-03-28 15:29:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
event.Success("account deleted")
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, m)
|
|
|
|
})
|
|
|
|
}
|