Skip to content

Commit

Permalink
Merge pull request #1436 from okhex/main
Browse files Browse the repository at this point in the history
feat(upload): add support for streamed uploads of Big File
  • Loading branch information
ernado authored Sep 16, 2024
2 parents 9a3a499 + 67750c8 commit 7bcc83a
Show file tree
Hide file tree
Showing 3 changed files with 14 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 @@ -53,14 +53,22 @@ 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
totalStreamSize := 0

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

n, err := io.ReadFull(r, buf.Buf)
if n > 0 {
totalStreamSize += n
}
switch {
case errors.Is(err, io.ErrUnexpectedEOF):
last = true
if upload.totalParts == -1 {
totalParts := (totalStreamSize + u.partSize - 1) / u.partSize
upload.totalParts = int(totalParts)
}
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).
// Support For Big Files
// 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
4 changes: 4 additions & 0 deletions telegram/uploader/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func (u *Uploader) Upload(ctx context.Context, upload *Upload) (tg.InputFileClas
if err := u.initUpload(upload); err != nil {
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 7bcc83a

Please sign in to comment.