photoprism/internal/api/file.go
Michael Mayer 03ec4b586d Initial commit for folders and moments #154 #260 #331
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
2020-05-23 20:58:58 +02:00

66 lines
1.5 KiB
Go

package api
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/event"
"github.com/photoprism/photoprism/internal/query"
"github.com/photoprism/photoprism/pkg/txt"
)
// GET /api/v1/files/:hash
//
// Parameters:
// hash: string SHA-1 hash of the file
func GetFile(router *gin.RouterGroup, conf *config.Config) {
router.GET("/files/:hash", func(c *gin.Context) {
if Unauthorized(c, conf) {
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
return
}
p, err := query.FileByHash(c.Param("hash"))
if err != nil {
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
return
}
c.JSON(http.StatusOK, p)
})
}
// POST /api/v1/files/:uid/link
//
// Parameters:
// uid: string SHA-1 hash of the file
func LinkFile(router *gin.RouterGroup, conf *config.Config) {
router.POST("/files/:uid/link", func(c *gin.Context) {
if Unauthorized(c, conf) {
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
return
}
m, err := query.FileByUID(c.Param("uid"))
if err != nil {
c.AbortWithStatusJSON(http.StatusNotFound, ErrFileNotFound)
return
}
if link, err := newLink(c); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
return
} else {
entity.Db().Model(&m).Association("Links").Append(link)
}
event.Success("created file share link")
c.JSON(http.StatusOK, m)
})
}