Add max. file size limit handling

This commit is contained in:
Nonoo 2023-08-15 10:06:07 +02:00
parent b89f9b8c1a
commit 2cbadadcc9
5 changed files with 31 additions and 16 deletions

View File

@ -4,3 +4,4 @@ BOT_TOKEN=
ALLOWED_USERIDS=
ADMIN_USERIDS=
ALLOWED_GROUPIDS=
MAX_SIZE=

View File

@ -1,11 +0,0 @@
#!/bin/bash
. config.inc.sh
API_ID=$API_ID \
API_HASH=$API_HASH \
BOT_TOKEN=$BOT_TOKEN \
ALLOWED_USERIDS=$ALLOWED_USERIDS \
ADMIN_USERIDS=$ADMIN_USERIDS \
ALLOWED_GROUPIDS=$ALLOWED_GROUPIDS \
go run *.go

View File

@ -8,6 +8,7 @@ import (
"strconv"
"strings"
"github.com/dustin/go-humanize"
"github.com/wader/goutubedl"
"golang.org/x/exp/slices"
)
@ -20,6 +21,8 @@ type paramsType struct {
AllowedUserIDs []int64
AdminUserIDs []int64
AllowedGroupIDs []int64
MaxSize int64
}
var params paramsType
@ -40,6 +43,8 @@ func (p *paramsType) Init() error {
flag.StringVar(&adminUserIDs, "admin-user-ids", "", "admin telegram user ids")
var allowedGroupIDs string
flag.StringVar(&allowedGroupIDs, "allowed-group-ids", "", "allowed telegram group ids")
var maxSize string
flag.StringVar(&maxSize, "max-size", "", "allowed max size of video files")
flag.Parse()
var err error
@ -51,7 +56,7 @@ func (p *paramsType) Init() error {
}
p.ApiID, err = strconv.Atoi(apiID)
if err != nil {
return fmt.Errorf("invalid env var API_ID")
return fmt.Errorf("invalid api_id")
}
if p.ApiHash == "" {
@ -89,7 +94,7 @@ func (p *paramsType) Init() error {
}
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
return fmt.Errorf("env var ALLOWED_USERIDS contains invalid user ID: " + idStr)
return fmt.Errorf("allowed user ids contains invalid user ID: " + idStr)
}
p.AllowedUserIDs = append(p.AllowedUserIDs, id)
}
@ -104,7 +109,7 @@ func (p *paramsType) Init() error {
}
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
return fmt.Errorf("env var ADMIN_USERIDS contains invalid user ID: " + idStr)
return fmt.Errorf("admin ids contains invalid user ID: " + idStr)
}
p.AdminUserIDs = append(p.AdminUserIDs, id)
if !slices.Contains(p.AllowedUserIDs, id) {
@ -122,11 +127,22 @@ func (p *paramsType) Init() error {
}
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
return fmt.Errorf("env var ALLOWED_GROUPIDS contains invalid group ID: " + idStr)
return fmt.Errorf("allowed group ids contains invalid group ID: " + idStr)
}
p.AllowedGroupIDs = append(p.AllowedUserIDs, id)
}
if maxSize == "" {
maxSize = os.Getenv("MAX_SIZE")
}
if maxSize != "" {
b, err := humanize.ParseBigBytes(maxSize)
if err != nil {
return fmt.Errorf("invalid max size: %w", err)
}
p.MaxSize = b.Int64()
}
// Writing env. var YTDLP_COOKIES contents to a file.
// In case a docker container is used, the yt-dlp.conf points yt-dlp to this cookie file.
if cookies := os.Getenv("YTDLP_COOKIES"); cookies != "" {

8
run.sh
View File

@ -2,10 +2,16 @@
. config.inc.sh
bin=./yt-dlp-telegram-bot
if [ ! -x "$bin" ]; then
bin="go run *.go"
fi
API_ID=$API_ID \
API_HASH=$API_HASH \
BOT_TOKEN=$BOT_TOKEN \
ALLOWED_USERIDS=$ALLOWED_USERIDS \
ADMIN_USERIDS=$ADMIN_USERIDS \
ALLOWED_GROUPIDS=$ALLOWED_GROUPIDS \
./yt-dlp-telegram-bot
MAX_SIZE=$MAX_SIZE \
$bin

View File

@ -34,6 +34,9 @@ func (p *Uploader) UploadFile(ctx context.Context, entities tg.Entities, u *tg.U
if n == 0 {
break
}
if params.MaxSize > 0 && buf.Len() > int(params.MaxSize) {
return fmt.Errorf("file is too big, max. allowed size is %s", humanize.BigBytes(big.NewInt(int64(params.MaxSize))))
}
buf.Write(b[:n])
}