2020-05-13 13:36:42 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2021-12-14 17:34:52 +00:00
|
|
|
"github.com/photoprism/photoprism/pkg/sanitize"
|
|
|
|
|
2020-12-05 03:24:10 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
|
|
|
|
2020-05-13 13:36:42 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2020-06-07 08:09:35 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2020-05-13 13:36:42 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
|
|
|
"github.com/photoprism/photoprism/internal/video"
|
|
|
|
)
|
|
|
|
|
2020-05-27 17:38:40 +00:00
|
|
|
// GET /api/v1/videos/:hash/:token/:type
|
2020-05-13 13:36:42 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// hash: string The photo or video file hash as returned by the search API
|
|
|
|
// type: string Video type
|
2020-06-25 12:54:04 +00:00
|
|
|
func GetVideo(router *gin.RouterGroup) {
|
2020-05-27 17:38:40 +00:00
|
|
|
router.GET("/videos/:hash/:token/:type", func(c *gin.Context) {
|
2020-06-26 14:11:56 +00:00
|
|
|
if InvalidPreviewToken(c) {
|
2020-05-27 17:38:40 +00:00
|
|
|
c.Data(http.StatusForbidden, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-14 17:34:52 +00:00
|
|
|
fileHash := sanitize.Token(c.Param("hash"))
|
|
|
|
typeName := sanitize.Token(c.Param("type"))
|
2020-05-13 13:36:42 +00:00
|
|
|
|
2020-12-12 16:20:31 +00:00
|
|
|
videoType, ok := video.Types[typeName]
|
2020-05-13 13:36:42 +00:00
|
|
|
|
|
|
|
if !ok {
|
2021-12-14 19:01:39 +00:00
|
|
|
log.Errorf("video: invalid type %s", sanitize.Log(typeName))
|
2020-05-13 13:36:42 +00:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", videoIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := query.FileByHash(fileHash)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-05-20 08:42:48 +00:00
|
|
|
log.Errorf("video: %s", err.Error())
|
2020-05-13 13:36:42 +00:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", videoIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-20 08:42:48 +00:00
|
|
|
if !f.FileVideo {
|
2020-05-23 18:58:58 +00:00
|
|
|
f, err = query.VideoByPhotoUID(f.PhotoUID)
|
2020-05-20 08:42:48 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("video: %s", err.Error())
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", videoIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 13:36:42 +00:00
|
|
|
if f.FileError != "" {
|
|
|
|
log.Errorf("video: file error %s", f.FileError)
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", videoIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-07 08:09:35 +00:00
|
|
|
fileName := photoprism.FileName(f.FileRoot, f.FileName)
|
2020-05-13 13:36:42 +00:00
|
|
|
|
2020-12-05 03:24:10 +00:00
|
|
|
if mf, err := photoprism.NewMediaFile(fileName); err != nil {
|
2021-12-14 19:01:39 +00:00
|
|
|
log.Errorf("video: file %s is missing", sanitize.Log(f.FileName))
|
2020-05-13 13:36:42 +00:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", videoIconSvg)
|
|
|
|
|
2020-05-25 17:10:44 +00:00
|
|
|
// Set missing flag so that the file doesn't show up in search results anymore.
|
2020-05-28 14:26:22 +00:00
|
|
|
logError("video", f.Update("FileMissing", true))
|
2020-05-25 17:10:44 +00:00
|
|
|
|
2020-05-13 13:36:42 +00:00
|
|
|
return
|
2020-12-12 16:20:31 +00:00
|
|
|
} else if f.FileCodec != string(videoType.Codec) {
|
2020-12-05 03:24:10 +00:00
|
|
|
conv := service.Convert()
|
|
|
|
|
2021-02-16 10:40:40 +00:00
|
|
|
if avcFile, err := conv.ToAvc(mf, service.Config().FFmpegEncoder()); err != nil {
|
2021-12-14 19:01:39 +00:00
|
|
|
log.Errorf("video: transcoding %s failed", sanitize.Log(f.FileName))
|
2020-12-05 03:24:10 +00:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", videoIconSvg)
|
|
|
|
return
|
2020-12-12 16:20:31 +00:00
|
|
|
} else {
|
|
|
|
fileName = avcFile.FileName()
|
2020-12-05 03:24:10 +00:00
|
|
|
}
|
2020-05-13 13:36:42 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 08:02:30 +00:00
|
|
|
AddContentTypeHeader(c, ContentTypeAvc)
|
2020-12-12 16:53:19 +00:00
|
|
|
|
2020-05-13 13:36:42 +00:00
|
|
|
if c.Query("download") != "" {
|
2021-01-27 20:30:10 +00:00
|
|
|
c.FileAttachment(fileName, f.DownloadName(DownloadName(c), 0))
|
2020-05-13 13:36:42 +00:00
|
|
|
} else {
|
|
|
|
c.File(fileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
})
|
|
|
|
}
|