mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-31 12:14:46 -04:00
Rework transcoder routes to use b64 path and check for file existance
This commit is contained in:
parent
59e1832c3e
commit
da0f415ecf
@ -16,9 +16,9 @@ import (
|
|||||||
// Retrieve the raw video stream, in the same container as the one on the server. No transcoding or
|
// Retrieve the raw video stream, in the same container as the one on the server. No transcoding or
|
||||||
// transmuxing is done.
|
// transmuxing is done.
|
||||||
//
|
//
|
||||||
// Path: /direct
|
// Path: /:path/direct
|
||||||
func DirectStream(c echo.Context) error {
|
func DirectStream(c echo.Context) error {
|
||||||
path, err := GetPath(c)
|
path, _, err := GetPath(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -31,18 +31,18 @@ func DirectStream(c echo.Context) error {
|
|||||||
// Note that the direct stream is missing (since the direct is not an hls stream) and
|
// Note that the direct stream is missing (since the direct is not an hls stream) and
|
||||||
// subtitles/fonts are not included to support more codecs than just webvtt.
|
// subtitles/fonts are not included to support more codecs than just webvtt.
|
||||||
//
|
//
|
||||||
// Path: /master.m3u8
|
// Path: /:path/master.m3u8
|
||||||
func (h *Handler) GetMaster(c echo.Context) error {
|
func (h *Handler) GetMaster(c echo.Context) error {
|
||||||
client, err := GetClientId(c)
|
client, err := GetClientId(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
path, err := GetPath(c)
|
path, sha, err := GetPath(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
ret, err := h.transcoder.GetMaster(path, client, GetRoute(c))
|
ret, err := h.transcoder.GetMaster(path, client, sha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -55,7 +55,7 @@ func (h *Handler) GetMaster(c echo.Context) error {
|
|||||||
// This route can take a few seconds to respond since it will way for at least one segment to be
|
// This route can take a few seconds to respond since it will way for at least one segment to be
|
||||||
// available.
|
// available.
|
||||||
//
|
//
|
||||||
// Path: /:quality/index.m3u8
|
// Path: /:path/:quality/index.m3u8
|
||||||
func (h *Handler) GetVideoIndex(c echo.Context) error {
|
func (h *Handler) GetVideoIndex(c echo.Context) error {
|
||||||
quality, err := src.QualityFromString(c.Param("quality"))
|
quality, err := src.QualityFromString(c.Param("quality"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -65,12 +65,12 @@ func (h *Handler) GetVideoIndex(c echo.Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
path, err := GetPath(c)
|
path, sha, err := GetPath(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
ret, err := h.transcoder.GetVideoIndex(path, quality, client, GetRoute(c))
|
ret, err := h.transcoder.GetVideoIndex(path, quality, client, sha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -83,7 +83,7 @@ func (h *Handler) GetVideoIndex(c echo.Context) error {
|
|||||||
// This route can take a few seconds to respond since it will way for at least one segment to be
|
// This route can take a few seconds to respond since it will way for at least one segment to be
|
||||||
// available.
|
// available.
|
||||||
//
|
//
|
||||||
// Path: /audio/:audio/index.m3u8
|
// Path: /:path/audio/:audio/index.m3u8
|
||||||
func (h *Handler) GetAudioIndex(c echo.Context) error {
|
func (h *Handler) GetAudioIndex(c echo.Context) error {
|
||||||
audio, err := strconv.ParseInt(c.Param("audio"), 10, 32)
|
audio, err := strconv.ParseInt(c.Param("audio"), 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -93,12 +93,12 @@ func (h *Handler) GetAudioIndex(c echo.Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
path, err := GetPath(c)
|
path, sha, err := GetPath(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
ret, err := h.transcoder.GetAudioIndex(path, int32(audio), client, GetRoute(c))
|
ret, err := h.transcoder.GetAudioIndex(path, int32(audio), client, sha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -109,7 +109,7 @@ func (h *Handler) GetAudioIndex(c echo.Context) error {
|
|||||||
//
|
//
|
||||||
// Retrieve a chunk of a transmuxed video.
|
// Retrieve a chunk of a transmuxed video.
|
||||||
//
|
//
|
||||||
// Path: /:quality/segments-:chunk.ts
|
// Path: /:path/:quality/segments-:chunk.ts
|
||||||
func (h *Handler) GetVideoSegment(c echo.Context) error {
|
func (h *Handler) GetVideoSegment(c echo.Context) error {
|
||||||
quality, err := src.QualityFromString(c.Param("quality"))
|
quality, err := src.QualityFromString(c.Param("quality"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -123,12 +123,12 @@ func (h *Handler) GetVideoSegment(c echo.Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
path, err := GetPath(c)
|
path, sha, err := GetPath(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
ret, err := h.transcoder.GetVideoSegment(path, quality, segment, client, GetRoute(c))
|
ret, err := h.transcoder.GetVideoSegment(path, quality, segment, client, sha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -139,7 +139,7 @@ func (h *Handler) GetVideoSegment(c echo.Context) error {
|
|||||||
//
|
//
|
||||||
// Retrieve a chunk of a transcoded audio.
|
// Retrieve a chunk of a transcoded audio.
|
||||||
//
|
//
|
||||||
// Path: /audio/:audio/segments-:chunk.ts
|
// Path: /:path/audio/:audio/segments-:chunk.ts
|
||||||
func (h *Handler) GetAudioSegment(c echo.Context) error {
|
func (h *Handler) GetAudioSegment(c echo.Context) error {
|
||||||
audio, err := strconv.ParseInt(c.Param("audio"), 10, 32)
|
audio, err := strconv.ParseInt(c.Param("audio"), 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -153,12 +153,12 @@ func (h *Handler) GetAudioSegment(c echo.Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
path, err := GetPath(c)
|
path, sha, err := GetPath(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
ret, err := h.transcoder.GetAudioSegment(path, int32(audio), segment, client, GetRoute(c))
|
ret, err := h.transcoder.GetAudioSegment(path, int32(audio), segment, client, sha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -169,29 +169,20 @@ func (h *Handler) GetAudioSegment(c echo.Context) error {
|
|||||||
//
|
//
|
||||||
// Identify metadata about a file.
|
// Identify metadata about a file.
|
||||||
//
|
//
|
||||||
// Path: /info
|
// Path: /:path/info
|
||||||
func (h *Handler) GetInfo(c echo.Context) error {
|
func (h *Handler) GetInfo(c echo.Context) error {
|
||||||
path, err := GetPath(c)
|
path, sha, err := GetPath(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
route := GetRoute(c)
|
ret, err := src.GetInfo(path, sha)
|
||||||
sha, err := src.GetHash(path)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
ret, err := src.GetInfo(path, sha, route)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Run extractors to have them in cache
|
// Run extractors to have them in cache
|
||||||
src.Extract(ret.Path, sha, route)
|
src.Extract(ret.Path, sha)
|
||||||
go src.ExtractThumbnail(
|
go src.ExtractThumbnail(ret.Path, sha)
|
||||||
ret.Path,
|
|
||||||
route,
|
|
||||||
sha,
|
|
||||||
)
|
|
||||||
return c.JSON(http.StatusOK, ret)
|
return c.JSON(http.StatusOK, ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -199,9 +190,9 @@ func (h *Handler) GetInfo(c echo.Context) error {
|
|||||||
//
|
//
|
||||||
// Get a specific attachment.
|
// Get a specific attachment.
|
||||||
//
|
//
|
||||||
// Path: /attachment/:name
|
// Path: /:path/attachment/:name
|
||||||
func (h *Handler) GetAttachment(c echo.Context) error {
|
func (h *Handler) GetAttachment(c echo.Context) error {
|
||||||
path, err := GetPath(c)
|
path, sha, err := GetPath(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -210,12 +201,7 @@ func (h *Handler) GetAttachment(c echo.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
route := GetRoute(c)
|
wait, err := src.Extract(path, sha)
|
||||||
sha, err := src.GetHash(path)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
wait, err := src.Extract(path, sha, route)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -229,9 +215,9 @@ func (h *Handler) GetAttachment(c echo.Context) error {
|
|||||||
//
|
//
|
||||||
// Get a specific subtitle.
|
// Get a specific subtitle.
|
||||||
//
|
//
|
||||||
// Path: /subtitle/:name
|
// Path: /:path/subtitle/:name
|
||||||
func (h *Handler) GetSubtitle(c echo.Context) error {
|
func (h *Handler) GetSubtitle(c echo.Context) error {
|
||||||
path, err := GetPath(c)
|
path, sha, err := GetPath(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -240,12 +226,7 @@ func (h *Handler) GetSubtitle(c echo.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
route := GetRoute(c)
|
wait, err := src.Extract(path, sha)
|
||||||
sha, err := src.GetHash(path)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
wait, err := src.Extract(path, sha, route)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -259,22 +240,14 @@ func (h *Handler) GetSubtitle(c echo.Context) error {
|
|||||||
//
|
//
|
||||||
// Get a sprite file containing all the thumbnails of the show.
|
// Get a sprite file containing all the thumbnails of the show.
|
||||||
//
|
//
|
||||||
// Path: /thumbnails.png
|
// Path: /:path/thumbnails.png
|
||||||
func (h *Handler) GetThumbnails(c echo.Context) error {
|
func (h *Handler) GetThumbnails(c echo.Context) error {
|
||||||
path, err := GetPath(c)
|
path, sha, err := GetPath(c)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
sha, err := src.GetHash(path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
out, err := src.ExtractThumbnail(
|
out, err := src.ExtractThumbnail(path, sha)
|
||||||
path,
|
|
||||||
GetRoute(c),
|
|
||||||
sha,
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -287,22 +260,14 @@ func (h *Handler) GetThumbnails(c echo.Context) error {
|
|||||||
// Get a vtt file containing timing/position of thumbnails inside the sprite file.
|
// Get a vtt file containing timing/position of thumbnails inside the sprite file.
|
||||||
// https://developer.bitmovin.com/playback/docs/webvtt-based-thumbnails for more info.
|
// https://developer.bitmovin.com/playback/docs/webvtt-based-thumbnails for more info.
|
||||||
//
|
//
|
||||||
// Path: /:resource/:slug/thumbnails.vtt
|
// Path: /:path/:resource/:slug/thumbnails.vtt
|
||||||
func (h *Handler) GetThumbnailsVtt(c echo.Context) error {
|
func (h *Handler) GetThumbnailsVtt(c echo.Context) error {
|
||||||
path, err := GetPath(c)
|
path, sha, err := GetPath(c)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
sha, err := src.GetHash(path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
out, err := src.ExtractThumbnail(
|
out, err := src.ExtractThumbnail(path, sha)
|
||||||
path,
|
|
||||||
GetRoute(c),
|
|
||||||
sha,
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -328,17 +293,17 @@ func main() {
|
|||||||
transcoder: transcoder,
|
transcoder: transcoder,
|
||||||
}
|
}
|
||||||
|
|
||||||
e.GET("/direct", DirectStream)
|
e.GET("/:path/direct", DirectStream)
|
||||||
e.GET("/master.m3u8", h.GetMaster)
|
e.GET("/:path/master.m3u8", h.GetMaster)
|
||||||
e.GET("/:quality/index.m3u8", h.GetVideoIndex)
|
e.GET("/:path/:quality/index.m3u8", h.GetVideoIndex)
|
||||||
e.GET("/audio/:audio/index.m3u8", h.GetAudioIndex)
|
e.GET("/:path/audio/:audio/index.m3u8", h.GetAudioIndex)
|
||||||
e.GET("/:quality/:chunk", h.GetVideoSegment)
|
e.GET("/:path/:quality/:chunk", h.GetVideoSegment)
|
||||||
e.GET("/audio/:audio/:chunk", h.GetAudioSegment)
|
e.GET("/:path/audio/:audio/:chunk", h.GetAudioSegment)
|
||||||
e.GET("/info", h.GetInfo)
|
e.GET("/:path/info", h.GetInfo)
|
||||||
e.GET("/thumbnails.png", h.GetThumbnails)
|
e.GET("/:path/thumbnails.png", h.GetThumbnails)
|
||||||
e.GET("/thumbnails.vtt", h.GetThumbnailsVtt)
|
e.GET("/:path/thumbnails.vtt", h.GetThumbnailsVtt)
|
||||||
e.GET("/attachment/:name", h.GetAttachment)
|
e.GET("/:path/attachment/:name", h.GetAttachment)
|
||||||
e.GET("/subtitle/:name", h.GetSubtitle)
|
e.GET("/:path/subtitle/:name", h.GetSubtitle)
|
||||||
|
|
||||||
e.Logger.Fatal(e.Start(":7666"))
|
e.Logger.Fatal(e.Start(":7666"))
|
||||||
}
|
}
|
||||||
|
@ -33,14 +33,10 @@ func NewTranscoder() (*Transcoder, error) {
|
|||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Transcoder) getFileStream(path string, route string) (*FileStream, error) {
|
func (t *Transcoder) getFileStream(path string, sha string) (*FileStream, error) {
|
||||||
var err error
|
var err error
|
||||||
ret, _ := t.streams.GetOrCreate(path, func() *FileStream {
|
ret, _ := t.streams.GetOrCreate(path, func() *FileStream {
|
||||||
sha, err := GetHash(path)
|
return NewFileStream(path, sha)
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return NewFileStream(path, sha, route)
|
|
||||||
})
|
})
|
||||||
ret.ready.Wait()
|
ret.ready.Wait()
|
||||||
if err != nil || ret.err != nil {
|
if err != nil || ret.err != nil {
|
||||||
@ -50,8 +46,8 @@ func (t *Transcoder) getFileStream(path string, route string) (*FileStream, erro
|
|||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Transcoder) GetMaster(path string, client string, route string) (string, error) {
|
func (t *Transcoder) GetMaster(path string, client string, sha string) (string, error) {
|
||||||
stream, err := t.getFileStream(path, route)
|
stream, err := t.getFileStream(path, sha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@ -69,9 +65,9 @@ func (t *Transcoder) GetVideoIndex(
|
|||||||
path string,
|
path string,
|
||||||
quality Quality,
|
quality Quality,
|
||||||
client string,
|
client string,
|
||||||
route string,
|
sha string,
|
||||||
) (string, error) {
|
) (string, error) {
|
||||||
stream, err := t.getFileStream(path, route)
|
stream, err := t.getFileStream(path, sha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@ -89,9 +85,9 @@ func (t *Transcoder) GetAudioIndex(
|
|||||||
path string,
|
path string,
|
||||||
audio int32,
|
audio int32,
|
||||||
client string,
|
client string,
|
||||||
route string,
|
sha string,
|
||||||
) (string, error) {
|
) (string, error) {
|
||||||
stream, err := t.getFileStream(path, route)
|
stream, err := t.getFileStream(path, sha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@ -109,9 +105,9 @@ func (t *Transcoder) GetVideoSegment(
|
|||||||
quality Quality,
|
quality Quality,
|
||||||
segment int32,
|
segment int32,
|
||||||
client string,
|
client string,
|
||||||
route string,
|
sha string,
|
||||||
) (string, error) {
|
) (string, error) {
|
||||||
stream, err := t.getFileStream(path, route)
|
stream, err := t.getFileStream(path, sha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@ -130,9 +126,9 @@ func (t *Transcoder) GetAudioSegment(
|
|||||||
audio int32,
|
audio int32,
|
||||||
segment int32,
|
segment int32,
|
||||||
client string,
|
client string,
|
||||||
route string,
|
sha string,
|
||||||
) (string, error) {
|
) (string, error) {
|
||||||
stream, err := t.getFileStream(path, route)
|
stream, err := t.getFileStream(path, sha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,8 @@
|
|||||||
package src
|
package src
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha1"
|
|
||||||
"encoding/hex"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -18,15 +15,3 @@ func printExecTime(message string, args ...any) func() {
|
|||||||
log.Printf("%s finished in %s", msg, time.Since(start))
|
log.Printf("%s finished in %s", msg, time.Since(start))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetHash(path string) (string, error) {
|
|
||||||
info, err := os.Stat(path)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
h := sha1.New()
|
|
||||||
h.Write([]byte(path))
|
|
||||||
h.Write([]byte(info.ModTime().String()))
|
|
||||||
sha := hex.EncodeToString(h.Sum(nil))
|
|
||||||
return sha, nil
|
|
||||||
}
|
|
||||||
|
@ -1,30 +1,55 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha1"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
|
"github.com/zoriya/kyoo/transcoder/src"
|
||||||
)
|
)
|
||||||
|
|
||||||
var client = &http.Client{Timeout: 10 * time.Second}
|
var safe_path = src.GetEnvOr("GOCODER_SAFE_PATH", "/video")
|
||||||
|
|
||||||
type Item struct {
|
func GetPath(c echo.Context) (string, string, error) {
|
||||||
Path string `json:"path"`
|
key := c.Param("path")
|
||||||
}
|
|
||||||
|
|
||||||
func GetPath(c echo.Context) (string, error) {
|
|
||||||
key := c.Request().Header.Get("X-Path")
|
|
||||||
if key == "" {
|
if key == "" {
|
||||||
return "", echo.NewHTTPError(http.StatusBadRequest, "Missing resouce path. You need to set the X-Path header.")
|
return "", "", echo.NewHTTPError(http.StatusBadRequest, "Missing resouce path.")
|
||||||
}
|
}
|
||||||
return key, nil
|
pathb, err := base64.StdEncoding.DecodeString(key)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", echo.NewHTTPError(http.StatusBadRequest, "Invalid path. Should be base64 encoded.")
|
||||||
|
}
|
||||||
|
path := string(pathb)
|
||||||
|
if !filepath.IsAbs(path) {
|
||||||
|
return "", "", echo.NewHTTPError(http.StatusBadRequest, "Absolute path required.")
|
||||||
|
}
|
||||||
|
if !strings.HasPrefix(path, safe_path) {
|
||||||
|
return "", "", echo.NewHTTPError(http.StatusBadRequest, "Selected path is not marked as safe.")
|
||||||
|
}
|
||||||
|
hash, err := getHash(path)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", echo.NewHTTPError(http.StatusNotFound, "File does not exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
return path, hash, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRoute(c echo.Context) string {
|
func getHash(path string) (string, error) {
|
||||||
return c.Request().Header.Get("X-Route")
|
info, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
h := sha1.New()
|
||||||
|
h.Write([]byte(path))
|
||||||
|
h.Write([]byte(info.ModTime().String()))
|
||||||
|
sha := hex.EncodeToString(h.Sum(nil))
|
||||||
|
return sha, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func SanitizePath(path string) error {
|
func SanitizePath(path string) error {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user