2021-09-25 17:00:19 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"git.netflux.io/rob/clipper/media"
|
|
|
|
"git.netflux.io/rob/clipper/youtube"
|
|
|
|
youtubev2 "github.com/kkdai/youtube/v2"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
2021-10-22 10:57:01 +00:00
|
|
|
// getMediaSet is a handler that responds with a MediaSet.
|
2021-10-22 10:57:05 +00:00
|
|
|
func getMediaSet(c echo.Context) error {
|
|
|
|
videoID := c.Param("id")
|
|
|
|
mediaSet := media.NewMediaSet(videoID)
|
|
|
|
|
2021-09-25 17:00:19 +00:00
|
|
|
if mediaSet.Exists() {
|
|
|
|
if err := mediaSet.Load(); err != nil {
|
|
|
|
log.Printf("error loading MediaSet: %v", err)
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "could not fetch media set")
|
|
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, mediaSet)
|
|
|
|
}
|
|
|
|
|
|
|
|
var youtubeClient youtubev2.Client
|
|
|
|
downloader := youtube.NewDownloader(&youtubeClient)
|
2021-10-22 10:57:05 +00:00
|
|
|
mediaSet, err := downloader.Download(c.Request().Context(), videoID)
|
2021-09-25 17:00:19 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error downloading MediaSet: %v", err)
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "could not fetch media set")
|
|
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, mediaSet)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getThumbnails is a handler that responds with a MediaSet thumbnail grid.
|
2021-10-22 10:57:05 +00:00
|
|
|
func getThumbnails(c echo.Context) error {
|
|
|
|
videoID := c.Param("id")
|
|
|
|
mediaSet := media.NewMediaSet(videoID)
|
2021-09-25 17:00:19 +00:00
|
|
|
if err := mediaSet.Load(); err != nil {
|
|
|
|
log.Printf("error loading MediaSet: %v", err)
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "could not load media set")
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.File(mediaSet.ThumbnailPath())
|
|
|
|
}
|
|
|
|
|
2021-10-08 14:38:35 +00:00
|
|
|
// getVideo is a handler that responds with the video file for a MediaSet
|
2021-10-22 10:57:05 +00:00
|
|
|
func getVideo(c echo.Context) error {
|
|
|
|
videoID := c.Param("id")
|
|
|
|
mediaSet := media.NewMediaSet(videoID)
|
2021-09-25 17:00:19 +00:00
|
|
|
if err := mediaSet.Load(); err != nil {
|
|
|
|
log.Printf("error loading MediaSet: %v", err)
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "could not load media set")
|
|
|
|
}
|
|
|
|
|
2021-10-08 14:38:35 +00:00
|
|
|
return c.File(mediaSet.VideoPath())
|
2021-09-25 17:00:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getPeaks is a handler that returns a two-dimensional array of peaks, with
|
|
|
|
// the number of bins matching the provided parameter.
|
2021-10-22 10:57:05 +00:00
|
|
|
func getPeaks(c echo.Context) error {
|
|
|
|
videoID := c.Param("id")
|
2021-09-25 17:00:19 +00:00
|
|
|
|
|
|
|
start, err := strconv.ParseInt(c.QueryParam("start"), 0, 64)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "invalid start parameter provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
end, err := strconv.ParseInt(c.QueryParam("end"), 0, 64)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "invalid end parameter provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
numBins, err := strconv.Atoi(c.QueryParam("bins"))
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "invalid bins parameter provided")
|
|
|
|
}
|
|
|
|
|
2021-10-22 10:57:05 +00:00
|
|
|
mediaSet := media.NewMediaSet(videoID)
|
2021-09-25 17:00:19 +00:00
|
|
|
if err = mediaSet.Load(); err != nil {
|
|
|
|
log.Printf("error loading MediaSet: %v", err)
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "could not load media set")
|
|
|
|
}
|
|
|
|
|
|
|
|
peaks, err := mediaSet.Peaks(start, end, numBins)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error generating peaks: %v", err)
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "could not generate peaks")
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.NewEncoder(c.Response()).Encode(peaks)
|
|
|
|
}
|