mirror of
https://github.com/esphome/esphome.git
synced 2024-11-29 18:24:13 +01:00
dashboard: Adds "compressed=1" to /download.bin endpoint. (...) (#4966)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
parent
314c1c8b5c
commit
f72b07eb0e
1 changed files with 16 additions and 1 deletions
|
@ -3,6 +3,7 @@ import binascii
|
||||||
import codecs
|
import codecs
|
||||||
import collections
|
import collections
|
||||||
import functools
|
import functools
|
||||||
|
import gzip
|
||||||
import hashlib
|
import hashlib
|
||||||
import hmac
|
import hmac
|
||||||
import json
|
import json
|
||||||
|
@ -485,6 +486,7 @@ class DownloadBinaryRequestHandler(BaseHandler):
|
||||||
@bind_config
|
@bind_config
|
||||||
def get(self, configuration=None):
|
def get(self, configuration=None):
|
||||||
type = self.get_argument("type", "firmware.bin")
|
type = self.get_argument("type", "firmware.bin")
|
||||||
|
compressed = self.get_argument("compressed", "0") == "1"
|
||||||
|
|
||||||
storage_path = ext_storage_path(settings.config_dir, configuration)
|
storage_path = ext_storage_path(settings.config_dir, configuration)
|
||||||
storage_json = StorageJSON.load(storage_path)
|
storage_json = StorageJSON.load(storage_path)
|
||||||
|
@ -534,6 +536,8 @@ class DownloadBinaryRequestHandler(BaseHandler):
|
||||||
self.send_error(404)
|
self.send_error(404)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
filename = filename + ".gz" if compressed else filename
|
||||||
|
|
||||||
self.set_header("Content-Type", "application/octet-stream")
|
self.set_header("Content-Type", "application/octet-stream")
|
||||||
self.set_header("Content-Disposition", f'attachment; filename="{filename}"')
|
self.set_header("Content-Disposition", f'attachment; filename="{filename}"')
|
||||||
self.set_header("Cache-Control", "no-cache")
|
self.set_header("Cache-Control", "no-cache")
|
||||||
|
@ -543,9 +547,20 @@ class DownloadBinaryRequestHandler(BaseHandler):
|
||||||
|
|
||||||
with open(path, "rb") as f:
|
with open(path, "rb") as f:
|
||||||
while True:
|
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:
|
if not data:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
if compressed:
|
||||||
|
data = gzip.compress(data, 9)
|
||||||
|
|
||||||
self.write(data)
|
self.write(data)
|
||||||
self.finish()
|
self.finish()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue