2020-05-13 15:36:42 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2020-06-07 10:09:35 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2020-05-13 15:36:42 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
|
|
|
"github.com/photoprism/photoprism/internal/video"
|
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
)
|
|
|
|
|
2020-05-27 19:38:40 +02:00
|
|
|
// GET /api/v1/videos/:hash/:token/:type
|
2020-05-13 15:36:42 +02:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// hash: string The photo or video file hash as returned by the search API
|
|
|
|
// type: string Video type
|
2020-06-25 14:54:04 +02:00
|
|
|
func GetVideo(router *gin.RouterGroup) {
|
2020-05-27 19:38:40 +02:00
|
|
|
router.GET("/videos/:hash/:token/:type", func(c *gin.Context) {
|
2020-06-26 16:11:56 +02:00
|
|
|
if InvalidPreviewToken(c) {
|
2020-05-27 19:38:40 +02:00
|
|
|
c.Data(http.StatusForbidden, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-13 15:36:42 +02:00
|
|
|
fileHash := c.Param("hash")
|
|
|
|
typeName := c.Param("type")
|
|
|
|
|
|
|
|
_, ok := video.Types[typeName]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("video: invalid type %s", txt.Quote(typeName))
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", videoIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := query.FileByHash(fileHash)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-05-20 10:42:48 +02:00
|
|
|
log.Errorf("video: %s", err.Error())
|
2020-05-13 15:36:42 +02:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", videoIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-20 10:42:48 +02:00
|
|
|
if !f.FileVideo {
|
2020-05-23 20:58:58 +02:00
|
|
|
f, err = query.VideoByPhotoUID(f.PhotoUID)
|
2020-05-20 10:42:48 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("video: %s", err.Error())
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", videoIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 15:36:42 +02:00
|
|
|
if f.FileError != "" {
|
|
|
|
log.Errorf("video: file error %s", f.FileError)
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", videoIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-07 10:09:35 +02:00
|
|
|
fileName := photoprism.FileName(f.FileRoot, f.FileName)
|
2020-05-13 15:36:42 +02:00
|
|
|
|
|
|
|
if !fs.FileExists(fileName) {
|
2020-05-25 19:10:44 +02:00
|
|
|
log.Errorf("video: file %s is missing", txt.Quote(f.FileName))
|
2020-05-13 15:36:42 +02:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", videoIconSvg)
|
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
// Set missing flag so that the file doesn't show up in search results anymore.
|
2020-05-28 16:26:22 +02:00
|
|
|
logError("video", f.Update("FileMissing", true))
|
2020-05-25 19:10:44 +02:00
|
|
|
|
2020-05-13 15:36:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Query("download") != "" {
|
|
|
|
c.FileAttachment(fileName, f.ShareFileName())
|
|
|
|
} else {
|
|
|
|
c.File(fileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
})
|
|
|
|
}
|