2020-03-28 15:29:17 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-06-30 12:58:56 +02:00
|
|
|
"fmt"
|
2020-03-28 15:29:17 +01:00
|
|
|
"net/http"
|
2020-10-19 09:52:52 +02:00
|
|
|
"path"
|
2021-01-27 21:30:10 +01:00
|
|
|
"strings"
|
2020-06-30 12:58:56 +02:00
|
|
|
"time"
|
2020-03-28 15:29:17 +01:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-11-26 14:28:50 +01:00
|
|
|
|
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"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2020-06-30 12:58:56 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-03-28 15:29:17 +01:00
|
|
|
)
|
|
|
|
|
2021-01-08 13:29:01 +01:00
|
|
|
// Namespaces for caching and logs.
|
|
|
|
const (
|
|
|
|
accountFolder = "account-folder"
|
|
|
|
)
|
|
|
|
|
2021-09-18 15:32:39 +02:00
|
|
|
// GetAccount returns an account as JSON.
|
|
|
|
//
|
2020-03-28 15:29:17 +01:00
|
|
|
// GET /api/v1/accounts/:id
|
|
|
|
//
|
|
|
|
// Parameters:
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-12-18 13:05:48 +01:00
|
|
|
conf := service.Config()
|
|
|
|
|
|
|
|
if conf.Demo() || conf.DisableSettings() {
|
|
|
|
AbortUnauthorized(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
id := clean.IdUint(c.Param("id"))
|
2020-03-28 15:29:17 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-18 15:32:39 +02:00
|
|
|
// GetAccountFolders returns folders that belong to an account as JSON.
|
|
|
|
//
|
2020-06-30 12:58:56 +02:00
|
|
|
// GET /api/v1/accounts/:id/folders
|
2020-03-31 17:26:25 +02:00
|
|
|
//
|
|
|
|
// Parameters:
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
|
|
|
// 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-12-18 13:05:48 +01:00
|
|
|
conf := service.Config()
|
|
|
|
|
|
|
|
if conf.Demo() || conf.DisableSettings() {
|
|
|
|
AbortUnauthorized(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:58:56 +02:00
|
|
|
start := time.Now()
|
2022-04-15 09:42:07 +02:00
|
|
|
id := clean.IdUint(c.Param("id"))
|
2021-01-08 13:29:01 +01:00
|
|
|
cache := service.FolderCache()
|
|
|
|
cacheKey := fmt.Sprintf("%s:%d", accountFolder, id)
|
|
|
|
|
|
|
|
if cacheData, ok := cache.Get(cacheKey); ok {
|
|
|
|
cached := cacheData.(fs.FileInfos)
|
2020-06-30 12:58:56 +02:00
|
|
|
|
2022-07-19 16:58:43 +02:00
|
|
|
log.Tracef("api-v1: cache hit for %s [%s]", cacheKey, time.Since(start))
|
2020-06-30 12:58:56 +02:00
|
|
|
|
2021-01-08 13:29:01 +01:00
|
|
|
c.JSON(http.StatusOK, cached)
|
|
|
|
return
|
2020-06-30 12:58:56 +02:00
|
|
|
}
|
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 {
|
2021-01-08 13:29:01 +01:00
|
|
|
log.Errorf("%s: %s", accountFolder, 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
|
|
|
|
}
|
|
|
|
|
2021-01-08 13:29:01 +01:00
|
|
|
cache.SetDefault(cacheKey, list)
|
|
|
|
log.Debugf("cached %s [%s]", cacheKey, time.Since(start))
|
2020-06-30 12:58:56 +02:00
|
|
|
|
2020-03-31 17:26:25 +02:00
|
|
|
c.JSON(http.StatusOK, list)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-27 17:32:54 +01:00
|
|
|
// ShareWithAccount uploads files to the selected account.
|
|
|
|
//
|
2020-04-01 12:00:45 +02:00
|
|
|
// GET /api/v1/accounts/:id/share
|
|
|
|
//
|
|
|
|
// Parameters:
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
id := clean.IdUint(c.Param("id"))
|
2020-04-01 12:00:45 +02:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-02-27 17:32:54 +01:00
|
|
|
folder := f.Folder
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
// Find files to share.
|
|
|
|
selection := query.ShareSelection(m.ShareOriginals())
|
|
|
|
files, err := query.SelectedFiles(f.Selection, selection)
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
var aliases = make(map[string]int)
|
|
|
|
|
2020-04-01 12:00:45 +02:00
|
|
|
for _, file := range files {
|
2022-02-27 17:32:54 +01:00
|
|
|
alias := path.Join(folder, file.ShareBase(0))
|
2021-01-27 21:30:10 +01:00
|
|
|
key := strings.ToLower(alias)
|
|
|
|
|
|
|
|
if seq := aliases[key]; seq > 0 {
|
|
|
|
alias = file.ShareBase(seq)
|
|
|
|
}
|
|
|
|
|
|
|
|
aliases[key] += 1
|
|
|
|
|
|
|
|
entity.FirstOrCreateFileShare(entity.NewFileShare(file.ID, m.ID, alias))
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-27 17:32:54 +01:00
|
|
|
// CreateAccount creates a new remote account configuration.
|
|
|
|
//
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-18 13:05:48 +01:00
|
|
|
conf := service.Config()
|
|
|
|
|
|
|
|
if conf.Demo() || conf.DisableSettings() {
|
|
|
|
AbortUnauthorized(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-28 15:29:17 +01:00
|
|
|
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
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-27 17:32:54 +01:00
|
|
|
// UpdateAccount updates a remote account configuration.
|
|
|
|
//
|
2020-03-28 15:29:17 +01:00
|
|
|
// PUT /api/v1/accounts/:id
|
|
|
|
//
|
|
|
|
// Parameters:
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-12-18 13:05:48 +01:00
|
|
|
conf := service.Config()
|
|
|
|
|
|
|
|
if conf.Demo() || conf.DisableSettings() {
|
|
|
|
AbortUnauthorized(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
id := clean.IdUint(c.Param("id"))
|
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
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-05 02:18:45 +01:00
|
|
|
if m.AccSync {
|
|
|
|
workers.StartSync(service.Config())
|
|
|
|
}
|
|
|
|
|
2020-03-28 15:29:17 +01:00
|
|
|
c.JSON(http.StatusOK, m)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-27 17:32:54 +01:00
|
|
|
// DeleteAccount removes a remote account configuration.
|
|
|
|
//
|
2020-03-28 15:29:17 +01:00
|
|
|
// DELETE /api/v1/accounts/:id
|
|
|
|
//
|
|
|
|
// Parameters:
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-12-18 13:05:48 +01:00
|
|
|
conf := service.Config()
|
|
|
|
|
|
|
|
if conf.Demo() || conf.DisableSettings() {
|
|
|
|
AbortUnauthorized(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
id := clean.IdUint(c.Param("id"))
|
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-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)
|
|
|
|
})
|
|
|
|
}
|