uploader.py: ensure proper resource management with io.BytesIO (#33108)

* ensure proper resource management with io.BytesIO

* improve
old-commit-hash: 2728c95b0d
This commit is contained in:
Dean Lee 2024-07-30 04:43:31 +08:00 committed by GitHub
parent 3e39d610bc
commit 3c1af12490
1 changed files with 4 additions and 7 deletions

View File

@ -9,7 +9,6 @@ import time
import traceback import traceback
import datetime import datetime
import zstandard as zstd import zstandard as zstd
from typing import BinaryIO
from collections.abc import Iterator from collections.abc import Iterator
from cereal import log from cereal import log
@ -152,13 +151,11 @@ class Uploader:
return FakeResponse() return FakeResponse()
with open(fn, "rb") as f: with open(fn, "rb") as f:
data: BinaryIO content = f.read()
if key.endswith('.zst') and not fn.endswith('.zst'): if key.endswith('.zst') and not fn.endswith('.zst'):
compressed = zstd.compress(f.read(), LOG_COMPRESSION_LEVEL) content = zstd.compress(content, LOG_COMPRESSION_LEVEL)
data = io.BytesIO(compressed)
else:
data = f
with io.BytesIO(content) as data:
return requests.put(url, data=data, headers=headers, timeout=10) return requests.put(url, data=data, headers=headers, timeout=10)
def upload(self, name: str, key: str, fn: str, network_type: int, metered: bool) -> bool: def upload(self, name: str, key: str, fn: str, network_type: int, metered: bool) -> bool: