Skip to content

Commit 87d5964

Browse files
authored
Fix tempfile issue on windows (#596)
* Fix tempfile issue on windows * Cleanup * Rename var
1 parent 24d23ad commit 87d5964

1 file changed

Lines changed: 17 additions & 23 deletions

File tree

streamrip/client/downloadable.py

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -133,29 +133,23 @@ async def _download(self, path: str, callback):
133133
blowfish_key,
134134
)
135135

136-
assert self.chunk_size == 2048 * 3
137-
138-
# Write data from server to tempfile because there's no
139-
# efficient way to guarantee a fixed chunk size for all iterations
140-
# in async
141-
async with aiofiles.tempfile.TemporaryFile("wb+") as tmp:
142-
async for chunk in resp.content.iter_chunks():
143-
data, _ = chunk
144-
await tmp.write(data)
145-
callback(len(data))
146-
147-
await tmp.seek(0)
148-
async with aiofiles.open(path, "wb") as audio:
149-
while chunk := await tmp.read(self.chunk_size):
150-
if len(chunk) >= 2048:
151-
decrypted_chunk = (
152-
self._decrypt_chunk(blowfish_key, chunk[:2048])
153-
+ chunk[2048:]
154-
)
155-
else:
156-
decrypted_chunk = chunk
157-
158-
await audio.write(decrypted_chunk)
136+
buf = bytearray()
137+
async for data, _ in resp.content.iter_chunks():
138+
buf += data
139+
callback(len(data))
140+
141+
async with aiofiles.open(path, "wb") as audio:
142+
buflen = len(buf)
143+
for i in range(0, buflen, self.chunk_size):
144+
data = buf[i : min(i + self.chunk_size, buflen - 1)]
145+
if len(data) >= 2048:
146+
decrypted_chunk = (
147+
self._decrypt_chunk(blowfish_key, data[:2048])
148+
+ data[2048:]
149+
)
150+
else:
151+
decrypted_chunk = data
152+
await audio.write(decrypted_chunk)
159153

160154
@staticmethod
161155
def _decrypt_chunk(key, data):

0 commit comments

Comments
 (0)