diff --git a/config.inc.sh-example b/config.inc.sh-example index 87841eb..08d8ec9 100755 --- a/config.inc.sh-example +++ b/config.inc.sh-example @@ -4,3 +4,4 @@ BOT_TOKEN= ALLOWED_USERIDS= ADMIN_USERIDS= ALLOWED_GROUPIDS= +MAX_SIZE= diff --git a/debug-run.sh b/debug-run.sh deleted file mode 100755 index 50123ad..0000000 --- a/debug-run.sh +++ /dev/null @@ -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 diff --git a/params.go b/params.go index c67e8e0..d5d6857 100644 --- a/params.go +++ b/params.go @@ -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 != "" { diff --git a/run.sh b/run.sh index 663d9c5..d1b4b00 100755 --- a/run.sh +++ b/run.sh @@ -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 diff --git a/upload.go b/upload.go index 9b028d4..daabf55 100644 --- a/upload.go +++ b/upload.go @@ -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]) }