Skip to content

Commit

Permalink
Added Support For Streamed Uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
okhex committed Sep 9, 2024
1 parent 7dcebbd commit ca17236
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions telegram/uploader/big.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package uploader
import (
"context"
"io"
"math"

"github.com/go-faster/errors"

Expand Down Expand Up @@ -53,13 +54,20 @@ func (u *Uploader) bigLoop(ctx context.Context, threads int, upload *Upload) err
r := syncio.NewReader(upload.from)
g.Go(func(ctx context.Context) error {
last := false
totalSize := 0

for {
buf := u.pool.GetSize(u.partSize)

n, err := io.ReadFull(r, buf.Buf)
if n > 0 {
totalSize += n
}
switch {
case errors.Is(err, io.ErrUnexpectedEOF):
if upload.totalParts == -1 {
upload.totalParts = int(math.Ceil(float64(totalSize) / float64(u.partSize)))
}
last = true
case errors.Is(err, io.EOF):
u.pool.Put(buf)
Expand Down
2 changes: 2 additions & 0 deletions telegram/uploader/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func (u *Uploader) FromFS(ctx context.Context, filesystem fs.FS, path string) (_
// FromReader uploads file from given io.Reader.
// NB: totally stream should not exceed the limit for
// small files (10 MB as docs says, may be a bit bigger).
// Now Supports Upto 2000MB
// https://core.telegram.org/api/files#streamed-uploads
func (u *Uploader) FromReader(ctx context.Context, name string, f io.Reader) (tg.InputFileClass, error) {
return u.Upload(ctx, NewUpload(name, f, -1))
}
Expand Down
5 changes: 5 additions & 0 deletions telegram/uploader/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ func (u *Uploader) Upload(ctx context.Context, upload *Upload) (tg.InputFileClas
return nil, err
}

if upload.totalBytes == -1 {
upload.big = true
upload.totalParts = -1
}

if !upload.big {
return u.uploadSmall(ctx, upload)
}
Expand Down

0 comments on commit ca17236

Please sign in to comment.