Skip to content

Conversation

@hzhreal
Copy link
Contributor

@hzhreal hzhreal commented Oct 15, 2025

Summary

The point of this PR is to offer an option for developers to download attachments in chunks instead of processing the whole file in memory at once to reduce possible overhead.

Changes:

  • Add the method HTTPClient.get_from_cdn_stream that yields data in chunks
  • Add the method Attachment.read_chunked to process data in chunks
  • Modify the method Attachment.save giving the option to write data to a file in chunks through an optional parameter

Information

  • This PR fixes an issue.
  • This PR adds something new (e.g. new method or parameters).
  • This PR is a breaking change (e.g. methods or parameters removed/renamed).
  • This PR is not a code change (e.g. documentation, README, typehinting,
    examples, ...).

Checklist

  • I have searched the open pull requests for duplicates.
  • If code changes were made then they have been tested.
  • I have updated the documentation to reflect the changes.
  • If type: ignore comments were used, a comment is also left explaining why.
  • I have updated the changelog to include these changes.

@hzhreal hzhreal requested a review from a team as a code owner October 15, 2025 20:45
@pycord-app
Copy link

pycord-app bot commented Oct 15, 2025

Thanks for opening this pull request!
Please make sure you have read the Contributing Guidelines and Code of Conduct.

This pull request can be checked-out with:

git fetch origin pull/2956/head:pr-2956
git checkout pr-2956

This pull request can be installed with:

pip install git+https://github.com/Pycord-Development/pycord@refs/pull/2956/head

@hzhreal
Copy link
Contributor Author

hzhreal commented Oct 15, 2025

Test code:

import discord
import os
import aiofiles
from discord.ext import commands

bot = commands.Bot()

CHUNKSIZE = 1024 * 1024

@bot.slash_command()
async def download(ctx: discord.ApplicationContext, f: discord.Attachment) -> None:
    size = f.size
    written = 0
    progress = "{}/{}"
    await ctx.respond(progress.format(written, size))
    async with aiofiles.open("test1.bin", "wb") as out:
        async for chunk in f.read_chunked(chunksize=CHUNKSIZE):
            await out.write(chunk)
            chunksize = len(chunk)
            written += chunksize
            await ctx.edit(content=progress.format(written, size))
    await f.save("test2.bin", chunksize=chunksize)

bot.run(os.getenv("TOKEN"))

@Paillat-dev Paillat-dev added hold: changelog This pull request is missing a changelog entry hold: testing This pull request requires further testing priority: low Low Priority labels Oct 15, 2025
@hzhreal hzhreal requested review from a team as code owners October 16, 2025 12:59
@Paillat-dev
Copy link
Member

@hzhreal Could you please fix the merge conflicts ? Thx

Lulalaby
Lulalaby previously approved these changes Oct 20, 2025
@Lulalaby Lulalaby requested review from Paillat-dev and removed request for NeloBlivion, Paillat-dev and plun1331 October 20, 2025 10:45
@Lulalaby Lulalaby enabled auto-merge (squash) October 21, 2025 14:00
auto-merge was automatically disabled October 22, 2025 10:56

Head branch was pushed to by a user without write access

Signed-off-by: Lala Sabathil <[email protected]>
@Lulalaby Lulalaby requested a review from Paillat-dev October 22, 2025 11:17
@Paillat-dev Paillat-dev removed the hold: changelog This pull request is missing a changelog entry label Oct 26, 2025
@Paillat-dev Paillat-dev removed the hold: testing This pull request requires further testing label Nov 4, 2025
@Paillat-dev Paillat-dev enabled auto-merge (squash) November 4, 2025 08:25
@Paillat-dev Paillat-dev merged commit 54f6576 into Pycord-Development:master Nov 4, 2025
25 checks passed
@hzhreal hzhreal deleted the feat/attachment-chunking branch November 4, 2025 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority: low Low Priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants