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:
F.D.Castel 2023-06-21 20:48:17 -03:00 committed by Jesse Hills
parent cd773a1dec
commit ef8180c8a8
No known key found for this signature in database
GPG key ID: BEAAE804EFD8E83A

View file

@ -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()