Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dashboard: Adds "compressed=1" to /download.bin endpoint. (...) #4966

Merged
merged 2 commits into from Jun 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion esphome/dashboard/dashboard.py
Expand Up @@ -3,6 +3,7 @@
import codecs
import collections
import functools
import gzip
import hashlib
import hmac
import json
Expand Down Expand Up @@ -485,6 +486,7 @@ class DownloadBinaryRequestHandler(BaseHandler):
@bind_config
def get(self, configuration=None):
type = self.get_argument("type", "firmware.bin")
compressed = self.get_argument("compressed", "0") == "1"

storage_path = ext_storage_path(settings.config_dir, configuration)
storage_json = StorageJSON.load(storage_path)
Expand Down Expand Up @@ -534,6 +536,8 @@ def get(self, configuration=None):
self.send_error(404)
return

filename = filename + ".gz" if compressed else filename

self.set_header("Content-Type", "application/octet-stream")
self.set_header("Content-Disposition", f'attachment; filename="{filename}"')
self.set_header("Cache-Control", "no-cache")
Expand All @@ -543,9 +547,20 @@ def get(self, configuration=None):

with open(path, "rb") as f:
while True:
data = f.read(16384)
# For a 528KB image used as benchmark:
# - using 256KB blocks resulted in the smallest file size.
# - blocks larger than 256KB didn't improve the size of compressed file.
# - blocks smaller than 256KB hindered compression, making the output file larger.

# Read file in blocks of 256KB.
data = f.read(256 * 1024)

if not data:
break

if compressed:
data = gzip.compress(data, 9)

self.write(data)
self.finish()

Expand Down