2020-02-26 17:50:28 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2020-04-08 13:24:06 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2020-02-26 17:50:28 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2020-04-30 20:07:03 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2020-04-08 13:24:06 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2020-02-26 17:50:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// GET /api/v1/files/:hash
|
|
|
|
//
|
|
|
|
// Parameters:
|
2020-04-08 13:24:06 +02:00
|
|
|
// hash: string SHA-1 hash of the file
|
2020-02-26 17:50:28 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
q := service.Query()
|
2020-03-28 15:29:17 +01:00
|
|
|
p, err := q.FileByHash(c.Param("hash"))
|
2020-02-26 17:50:28 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, p)
|
|
|
|
})
|
|
|
|
}
|
2020-04-08 13:24:06 +02:00
|
|
|
|
|
|
|
// POST /api/v1/files/:hash/link
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// hash: string SHA-1 hash of the file
|
|
|
|
func LinkFile(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.POST("/files/:hash/link", func(c *gin.Context) {
|
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
db := conf.Db()
|
|
|
|
q := query.New(db)
|
|
|
|
|
|
|
|
m, err := q.FileByUUID(c.Param("hash"))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrAlbumNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if link, err := newLink(c); err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
db.Model(&m).Association("Links").Append(link)
|
|
|
|
}
|
|
|
|
|
|
|
|
event.Success("created file share link")
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, m)
|
|
|
|
})
|
|
|
|
}
|