2020-03-28 15:29:17 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-06-30 12:58:56 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-03-28 15:29:17 +01:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2020-06-30 12:58:56 +02:00
|
|
|
"time"
|
2020-03-28 15:29:17 +01:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gin-gonic/gin/binding"
|
2020-06-25 14:54:04 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2020-03-28 15:29:17 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2020-07-04 12:54:35 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2020-05-08 15:41:01 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2020-06-25 14:54:04 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2020-04-06 10:26:26 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/workers"
|
2020-06-30 12:58:56 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-03-28 15:29:17 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// GET /api/v1/accounts
|
2020-06-25 14:54:04 +02:00
|
|
|
func GetAccounts(router *gin.RouterGroup) {
|
2020-03-28 15:29:17 +01:00
|
|
|
router.GET("/accounts", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourceAccounts, acl.ActionSearch)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-03-28 15:29:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var f form.AccountSearch
|
|
|
|
|
|
|
|
err := c.MustBindWith(&f, binding.Form)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2020-03-28 15:29:17 +01:00
|
|
|
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 {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2020-03-28 15:29:17 +01:00
|
|
|
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
|
2020-06-25 14:54:04 +02:00
|
|
|
func GetAccount(router *gin.RouterGroup) {
|
2020-03-28 15:29:17 +01:00
|
|
|
router.GET("/accounts/:id", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourceAccounts, acl.ActionRead)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-03-28 15:29:17 +01:00
|
|
|
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 {
|
2020-07-04 12:54:35 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrAccountNotFound)
|
2020-03-28 15:29:17 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:58:56 +02:00
|
|
|
// GET /api/v1/accounts/:id/folders
|
2020-03-31 17:26:25 +02:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// id: string Account ID as returned by the API
|
2020-06-30 12:58:56 +02:00
|
|
|
func GetAccountFolders(router *gin.RouterGroup) {
|
|
|
|
router.GET("/accounts/:id/folders", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourceAccounts, acl.ActionRead)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-03-31 17:26:25 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:58:56 +02:00
|
|
|
start := time.Now()
|
2020-03-31 17:26:25 +02:00
|
|
|
id := ParseUint(c.Param("id"))
|
2020-06-30 12:58:56 +02:00
|
|
|
cache := service.Cache()
|
|
|
|
cacheKey := fmt.Sprintf("account-folders:%d", id)
|
|
|
|
|
|
|
|
if cacheData, err := cache.Get(cacheKey); err == nil {
|
|
|
|
var cached fs.FileInfos
|
|
|
|
|
|
|
|
if err := json.Unmarshal(cacheData, &cached); err != nil {
|
|
|
|
log.Errorf("account-folders: %s", err)
|
|
|
|
} else {
|
|
|
|
log.Debugf("cache hit for %s [%s]", cacheKey, time.Since(start))
|
|
|
|
c.JSON(http.StatusOK, cached)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-03-31 17:26:25 +02:00
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
m, err := query.AccountByID(id)
|
2020-03-31 17:26:25 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrAccountNotFound)
|
2020-03-31 17:26:25 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-02 18:17:07 +02:00
|
|
|
list, err := m.Directories()
|
2020-03-31 17:26:25 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-06-30 12:58:56 +02:00
|
|
|
log.Errorf("account-folders: %s", err.Error())
|
2020-07-04 12:54:35 +02:00
|
|
|
Abort(c, http.StatusBadRequest, i18n.ErrConnectionFailed)
|
2020-03-31 17:26:25 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:58:56 +02:00
|
|
|
if c, err := json.Marshal(list); err == nil {
|
|
|
|
logError("account-folders", cache.Set(cacheKey, c))
|
|
|
|
log.Debugf("cached %s [%s]", cacheKey, time.Since(start))
|
|
|
|
}
|
|
|
|
|
2020-03-31 17:26:25 +02:00
|
|
|
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
|
2020-06-25 14:54:04 +02:00
|
|
|
func ShareWithAccount(router *gin.RouterGroup) {
|
2020-04-01 12:00:45 +02:00
|
|
|
router.POST("/accounts/:id/share", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourceAccounts, acl.ActionUpload)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-04-01 12:00:45 +02:00
|
|
|
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 {
|
2020-07-04 12:54:35 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrAccountNotFound)
|
2020-04-01 12:00:45 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var f form.AccountShare
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2020-04-01 12:00:45 +02:00
|
|
|
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 {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortEntityNotFound(c)
|
2020-04-01 12:00:45 +02:00
|
|
|
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-06-25 14:54:04 +02:00
|
|
|
workers.StartShare(service.Config())
|
2020-04-06 10:26:26 +02:00
|
|
|
|
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
|
2020-06-25 14:54:04 +02:00
|
|
|
func CreateAccount(router *gin.RouterGroup) {
|
2020-03-28 15:29:17 +01:00
|
|
|
router.POST("/accounts", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourceAccounts, acl.ActionCreate)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-03-28 15:29:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var f form.Account
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2020-03-28 15:29:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:02:01 +02:00
|
|
|
if err := f.ServiceDiscovery(); err != nil {
|
|
|
|
log.Error(err)
|
2020-07-04 12:54:35 +02:00
|
|
|
Abort(c, http.StatusBadRequest, i18n.ErrConnectionFailed)
|
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
|
|
|
|
2020-06-24 12:01:25 +02:00
|
|
|
log.Debugf("account: creating %+v %+v", f, m)
|
2020-03-28 15:29:17 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2020-03-28 15:29:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-04 12:54:35 +02:00
|
|
|
event.SuccessMsg(i18n.MsgAccountCreated)
|
2020-03-28 15:29:17 +01:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, m)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// PUT /api/v1/accounts/:id
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// id: string Account ID as returned by the API
|
2020-06-25 14:54:04 +02:00
|
|
|
func UpdateAccount(router *gin.RouterGroup) {
|
2020-03-28 15:29:17 +01:00
|
|
|
router.PUT("/accounts/:id", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourceAccounts, acl.ActionUpdate)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-03-28 15:29:17 +01:00
|
|
|
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 {
|
2020-07-04 12:54:35 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrAccountNotFound)
|
2020-03-28 15:29:17 +01:00
|
|
|
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)
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortSaveFailed(c)
|
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)
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
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)
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortSaveFailed(c)
|
2020-03-28 15:29:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-04 12:54:35 +02:00
|
|
|
event.SuccessMsg(i18n.MsgAccountSaved)
|
2020-03-28 15:29:17 +01:00
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
m, err = query.AccountByID(id)
|
2020-03-28 15:29:17 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortEntityNotFound(c)
|
2020-03-28 15:29:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, m)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DELETE /api/v1/accounts/:id
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// id: string Account ID as returned by the API
|
2020-06-25 14:54:04 +02:00
|
|
|
func DeleteAccount(router *gin.RouterGroup) {
|
2020-03-28 15:29:17 +01:00
|
|
|
router.DELETE("/accounts/:id", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourceAccounts, acl.ActionDelete)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-03-28 15:29:17 +01:00
|
|
|
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 {
|
2020-07-07 10:51:55 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrAccountNotFound)
|
2020-03-28 15:29:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
if err := m.Delete(); err != nil {
|
2020-07-07 10:51:55 +02:00
|
|
|
Error(c, http.StatusInternalServerError, err, i18n.ErrDeleteFailed)
|
2020-03-28 15:29:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-04 12:54:35 +02:00
|
|
|
event.SuccessMsg(i18n.MsgAccountDeleted)
|
2020-03-28 15:29:17 +01:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, m)
|
|
|
|
})
|
|
|
|
}
|