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

* ensure proper resource management with io.BytesIO

* improve
pull/33136/head
Dean Lee 9 months ago committed by GitHub
parent 606890cba5
commit 2728c95b0d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 11
      system/loggerd/uploader.py

@ -9,7 +9,6 @@ import time
import traceback
import datetime
import zstandard as zstd
from typing import BinaryIO
from collections.abc import Iterator
from cereal import log
@ -152,14 +151,12 @@ class Uploader:
return FakeResponse()
with open(fn, "rb") as f:
data: BinaryIO
content = f.read()
if key.endswith('.zst') and not fn.endswith('.zst'):
compressed = zstd.compress(f.read(), LOG_COMPRESSION_LEVEL)
data = io.BytesIO(compressed)
else:
data = f
content = zstd.compress(content, LOG_COMPRESSION_LEVEL)
return requests.put(url, data=data, headers=headers, timeout=10)
with io.BytesIO(content) as data:
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:
try:

Loading…
Cancel
Save