all repos — fixyoutube-go @ d9344f281e4e23360836847844572d3c9242741e

A better way to embed YouTube videos everywhere (inspired by FixTweet).

invidious/proxy.go (view raw)

 1package invidious
 2
 3import (
 4	"bytes"
 5	"io"
 6	"net/http"
 7)
 8
 9func (c *Client) ProxyVideo(url string) (*bytes.Buffer, int64, int) {
10	req, err := http.NewRequest(http.MethodGet, url, nil)
11	if err != nil {
12		logger.Error(err) // bad request
13		return nil, 0, http.StatusInternalServerError
14	}
15
16	resp, err := c.http.Do(req)
17	if err != nil {
18		logger.Error(err) // request failed
19		return nil, 0, http.StatusGone
20	}
21
22	if resp.ContentLength > maxSizeBytes {
23		logger.Debug("Content-Length exceeds max size.")
24		return nil, 0, http.StatusBadRequest
25	}
26	defer resp.Body.Close()
27
28	b := new(bytes.Buffer)
29	l, err := io.Copy(b, resp.Body)
30	if l != resp.ContentLength {
31		logger.Debug("Content-Length is inconsistent.")
32		return nil, 0, http.StatusBadRequest
33	}
34
35	return b, l, http.StatusOK
36}