photoprism/internal/api/video.go
Michael Mayer 4939e36ac6 Video: Update docker-compose.yml ARM64 example #703
Renamed PHOTOPRISM_FFMPEG_CODEC to PHOTOPRISM_FFMPEG_ENCODER as well.
Log level for errors has been increased to warn.
2021-02-16 11:40:40 +01:00

95 lines
2.4 KiB
Go

package api
import (
"net/http"
"github.com/photoprism/photoprism/internal/service"
"github.com/gin-gonic/gin"
"github.com/photoprism/photoprism/internal/photoprism"
"github.com/photoprism/photoprism/internal/query"
"github.com/photoprism/photoprism/internal/video"
"github.com/photoprism/photoprism/pkg/txt"
)
// GET /api/v1/videos/:hash/:token/:type
//
// Parameters:
// hash: string The photo or video file hash as returned by the search API
// type: string Video type
func GetVideo(router *gin.RouterGroup) {
router.GET("/videos/:hash/:token/:type", func(c *gin.Context) {
if InvalidPreviewToken(c) {
c.Data(http.StatusForbidden, "image/svg+xml", brokenIconSvg)
return
}
fileHash := c.Param("hash")
typeName := c.Param("type")
videoType, 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 {
log.Errorf("video: %s", err.Error())
c.Data(http.StatusOK, "image/svg+xml", videoIconSvg)
return
}
if !f.FileVideo {
f, err = query.VideoByPhotoUID(f.PhotoUID)
if err != nil {
log.Errorf("video: %s", err.Error())
c.Data(http.StatusOK, "image/svg+xml", videoIconSvg)
return
}
}
if f.FileError != "" {
log.Errorf("video: file error %s", f.FileError)
c.Data(http.StatusOK, "image/svg+xml", videoIconSvg)
return
}
fileName := photoprism.FileName(f.FileRoot, f.FileName)
if mf, err := photoprism.NewMediaFile(fileName); err != nil {
log.Errorf("video: file %s is missing", txt.Quote(f.FileName))
c.Data(http.StatusOK, "image/svg+xml", videoIconSvg)
// Set missing flag so that the file doesn't show up in search results anymore.
logError("video", f.Update("FileMissing", true))
return
} else if f.FileCodec != string(videoType.Codec) {
conv := service.Convert()
if avcFile, err := conv.ToAvc(mf, service.Config().FFmpegEncoder()); err != nil {
log.Errorf("video: transcoding %s failed", txt.Quote(f.FileName))
c.Data(http.StatusOK, "image/svg+xml", videoIconSvg)
return
} else {
fileName = avcFile.FileName()
}
}
AddContentTypeHeader(c, ContentTypeAvc)
if c.Query("download") != "" {
c.FileAttachment(fileName, f.DownloadName(DownloadName(c), 0))
} else {
c.File(fileName)
}
return
})
}