all repos — fixyoutube-go @ dfabe54c50cb40c8ffbe27dd08c2c0da0d8055b4

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, formatIndex int) (*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("Format ", formatIndex, ": 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		return nil, 0, http.StatusBadRequest
32	}
33
34	return b, l, http.StatusOK
35}