2020-05-13 15:36:42 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2020-12-05 04:24:10 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
|
|
|
|
2020-05-13 15:36:42 +02:00
|
|
|
"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/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")
|
|
|
|
|
2020-12-12 17:20:31 +01:00
|
|
|
videoType, ok := video.Types[typeName]
|
2020-05-13 15:36:42 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-12-05 04:24:10 +01:00
|
|
|
if mf, err := photoprism.NewMediaFile(fileName); err != nil {
|
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
|
2020-12-12 17:20:31 +01:00
|
|
|
} else if f.FileCodec != string(videoType.Codec) {
|
2020-12-05 04:24:10 +01:00
|
|
|
conv := service.Convert()
|
|
|
|
|
2021-02-16 11:40:40 +01:00
|
|
|
if avcFile, err := conv.ToAvc(mf, service.Config().FFmpegEncoder()); err != nil {
|
2021-02-15 20:26:32 +01:00
|
|
|
log.Errorf("video: transcoding %s failed", txt.Quote(f.FileName))
|
2020-12-05 04:24:10 +01:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", videoIconSvg)
|
|
|
|
return
|
2020-12-12 17:20:31 +01:00
|
|
|
} else {
|
|
|
|
fileName = avcFile.FileName()
|
2020-12-05 04:24:10 +01:00
|
|
|
}
|
2020-05-13 15:36:42 +02:00
|
|
|
}
|
|
|
|
|
2021-01-08 09:02:30 +01:00
|
|
|
AddContentTypeHeader(c, ContentTypeAvc)
|
2020-12-12 17:53:19 +01:00
|
|
|
|
2020-05-13 15:36:42 +02:00
|
|
|
if c.Query("download") != "" {
|
2021-01-27 21:30:10 +01:00
|
|
|
c.FileAttachment(fileName, f.DownloadName(DownloadName(c), 0))
|
2020-05-13 15:36:42 +02:00
|
|
|
} else {
|
|
|
|
c.File(fileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
})
|
|
|
|
}
|