Skip to content

Instantly share code, notes, and snippets.

@miku
Last active May 15, 2020 22:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miku/fe4fc9f53258b23741f41c38764fd2b6 to your computer and use it in GitHub Desktop.
Save miku/fe4fc9f53258b23741f41c38764fd2b6 to your computer and use it in GitHub Desktop.
On the fly content type detection.
// Tee plus LimitWriter.
// LimitWriter taken from: https://github.com/kubernetes/kubernetes/blob/579e0c74c150085b3fac01f6a33b66db96922f93/pkg/kubelet/util/ioutils/ioutils.go#L39-L70
//
package main
import (
"bytes"
"flag"
"io"
"io/ioutil"
"log"
"net/http"
)
// LimitWriter is a copy of the standard library ioutils.LimitReader,
// applied to the writer interface.
// LimitWriter returns a Writer that writes to w
// but stops with EOF after n bytes.
// The underlying implementation is a *LimitedWriter.
func LimitWriter(w io.Writer, n int64) io.Writer { return &LimitedWriter{w, n} }
// A LimitedWriter writes to W but limits the amount of
// data returned to just N bytes. Each call to Write
// updates N to reflect the new amount remaining.
// Write returns EOF when N <= 0 or when the underlying W returns EOF.
type LimitedWriter struct {
W io.Writer // underlying writer
N int64 // max bytes remaining
}
func (l *LimitedWriter) Write(p []byte) (n int, err error) {
if l.N <= 0 {
return 0, nil
}
if int64(len(p)) > l.N {
p = p[0:l.N]
}
n, err = l.W.Write(p)
l.N -= int64(n)
return
}
var (
link = flag.String("l", "https://upload.wikimedia.org/wikipedia/commons/transcoded/4/4d/Wikipedia_Edit_2014.webm/Wikipedia_Edit_2014.webm.480p.vp9.webm", "link to check")
)
func main() {
flag.Parse()
log.Printf("fetching %v", *link)
resp, err := http.Get(*link)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
var (
buf bytes.Buffer
tee = io.TeeReader(resp.Body, LimitWriter(&buf, 512))
)
n, err := io.Copy(ioutil.Discard, tee)
if err != nil {
log.Fatal(err)
}
log.Printf("read: %d", n)
log.Printf("detected content-type: %v", http.DetectContentType(buf.Bytes()))
// 2020/05/15 23:43:32 fetching https://upload.wikimedia.org/wikipedia/commons/transcoded/4/4d/Wikipedia_Edit_2014.webm/Wikipedia_Edit_2014.webm.480p.vp9.webm
// 2020/05/15 23:44:18 read: 13174381
// 2020/05/15 23:44:18 detected content-type: video/webm
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment