Skip to content

Commit

Permalink
Fix tempfile issue on windows (#596)
Browse files Browse the repository at this point in the history
* Fix tempfile issue on windows

* Cleanup

* Rename var
  • Loading branch information
nathom authored Jan 25, 2024
1 parent 24d23ad commit 87d5964
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions streamrip/client/downloadable.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,29 +133,23 @@ async def _download(self, path: str, callback):
blowfish_key,
)

assert self.chunk_size == 2048 * 3

# Write data from server to tempfile because there's no
# efficient way to guarantee a fixed chunk size for all iterations
# in async
async with aiofiles.tempfile.TemporaryFile("wb+") as tmp:
async for chunk in resp.content.iter_chunks():
data, _ = chunk
await tmp.write(data)
callback(len(data))

await tmp.seek(0)
async with aiofiles.open(path, "wb") as audio:
while chunk := await tmp.read(self.chunk_size):
if len(chunk) >= 2048:
decrypted_chunk = (
self._decrypt_chunk(blowfish_key, chunk[:2048])
+ chunk[2048:]
)
else:
decrypted_chunk = chunk

await audio.write(decrypted_chunk)
buf = bytearray()
async for data, _ in resp.content.iter_chunks():
buf += data
callback(len(data))

async with aiofiles.open(path, "wb") as audio:
buflen = len(buf)
for i in range(0, buflen, self.chunk_size):
data = buf[i : min(i + self.chunk_size, buflen - 1)]
if len(data) >= 2048:
decrypted_chunk = (
self._decrypt_chunk(blowfish_key, data[:2048])
+ data[2048:]
)
else:
decrypted_chunk = data
await audio.write(decrypted_chunk)

@staticmethod
def _decrypt_chunk(key, data):
Expand Down

0 comments on commit 87d5964

Please sign in to comment.