all repos — fixyoutube-go @ a98db242719bb1b183676dd9eca524cbf3000361

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.StatusCode != http.StatusOK {
23		return nil, 0, resp.StatusCode
24	}
25
26	if resp.ContentLength == 0 {
27		return nil, 0, http.StatusNoContent
28	}
29
30	if resp.ContentLength > maxSizeBytes {
31		logger.Debug("Content-Length exceeds max size.")
32		return nil, 0, http.StatusBadRequest
33	}
34	defer resp.Body.Close()
35
36	b := new(bytes.Buffer)
37	l, err := io.Copy(b, resp.Body)
38	if l != resp.ContentLength {
39		logger.Debug("Content-Length is inconsistent.")
40		return nil, 0, http.StatusBadRequest
41	}
42
43	return b, l, http.StatusOK
44}