photoprism/internal/api/video.go
Michael Mayer a77d74e6eb Add play button for videos to photo viewer #321
You can now play a video by clicking on the title (or description) if a video icon is shown next to it.

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
2020-05-20 10:42:48 +02:00

79 lines
1.8 KiB
Go

package api
import (
"net/http"
"path"
"github.com/gin-gonic/gin"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/query"
"github.com/photoprism/photoprism/internal/video"
"github.com/photoprism/photoprism/pkg/fs"
"github.com/photoprism/photoprism/pkg/txt"
)
// GET /api/v1/videos/:hash/: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, conf *config.Config) {
router.GET("/videos/:hash/:type", func(c *gin.Context) {
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 {
log.Errorf("video: %s", err.Error())
c.Data(http.StatusOK, "image/svg+xml", videoIconSvg)
return
}
if !f.FileVideo {
f, err = query.VideoByPhotoUUID(f.PhotoUUID)
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 := path.Join(conf.OriginalsPath(), f.FileName)
if !fs.FileExists(fileName) {
log.Errorf("video: could not find file for %s", fileName)
c.Data(http.StatusOK, "image/svg+xml", videoIconSvg)
// Set missing flag so that the file doesn't show up in search results anymore
f.FileMissing = true
entity.Db().Save(&f)
return
}
if c.Query("download") != "" {
c.FileAttachment(fileName, f.ShareFileName())
} else {
c.File(fileName)
}
return
})
}