Skip to content

Commit

Permalink
dashboard: Adds "compressed=1" to /download.bin endpoint. (...) (#4966)
Browse files Browse the repository at this point in the history
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
  • Loading branch information
fdcastel and jesserockz committed Jun 21, 2023
1 parent 314c1c8 commit f72b07e
Showing 1 changed file with 16 additions and 1 deletion.
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

0 comments on commit f72b07e

Please sign in to comment.