Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(upload): add support for streamed uploads of Big File #1436

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading