Improved Instagram video size detection (still very garbage)

This commit is contained in:
Logan G 2022-07-18 01:05:40 -06:00
parent 9b5b980e1d
commit c5500bc7fa
Signed by: logan
GPG key ID: E328528C921E7A7A

View file

@ -5,6 +5,7 @@ import (
"log" "log"
"regexp" "regexp"
"context" "context"
"io"
"bytes" "bytes"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
@ -153,18 +154,31 @@ func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate)
continue continue
} }
buf := new(bytes.Buffer)
buf.ReadFrom(downloadResult)
downloadResult.Close() // Test to see if the result is more than 8MB through the tried and true method of downloading it into a 8MB slice and seeing if it errors out
var buf = make([]byte, 8*1024*1024 + 1)
size, err := io.ReadFull(downloadResult, buf)
// This section is probably really bad, don't copy it. Actually, don't even look at it.
if size <= 8*1024*1024 && err.Error() == "unexpected EOF" {
log.Printf("Video Size: %fM", float64(size)/1024/1024)
var newBuf = make([]byte, size)
_, err := io.ReadFull(bytes.NewReader(buf), newBuf)
if err != nil {
log.Println(err)
continue
}
buf = nil
if buf.Len() <= 8*1024*1024 {
response.Files = append(response.Files, &discordgo.File { response.Files = append(response.Files, &discordgo.File {
Name: fmt.Sprintf("%s.%s", result.Info.ID, result.Formats()[0].Ext), Name: fmt.Sprintf("%s.%s", result.Info.ID, result.Formats()[0].Ext),
ContentType: "text/plain", // This is of course not true, but Discord doesn't give a shit ContentType: "text/plain", // This is of course not true, but Discord doesn't give a shit
Reader: buf, Reader: bytes.NewReader(newBuf),
}) })
} }
downloadResult.Close()
} }
respond = true respond = true