|
34 | 34 | from datapane.client.exceptions import IncompatibleVersionError, ReportTooLargeError, UnsupportedResourceError |
35 | 35 | from datapane.client.utils import TEST_ENV |
36 | 36 | from datapane.common import JSON, MIME, SIZE_1_MB, NPath, guess_type |
| 37 | +from datapane.common.ops_utils import inmemory_compress |
37 | 38 |
|
38 | 39 | ################################################################################ |
39 | 40 | # Tmpfile handling |
@@ -183,14 +184,25 @@ def post(self, params: t.Dict = None, overwrite: bool = False, **data: JSON) -> |
183 | 184 | def post_files(self, files: FileAttachmentList, overwrite: bool = False, **data: JSON) -> Munch: |
184 | 185 | # upload files using custom json-data protocol |
185 | 186 | # build the fields |
186 | | - file_header = {"Content-Encoding": "gzip"} |
187 | 187 |
|
188 | 188 | def _mk_file_fields(field_name: str, fobj: t.BinaryIO): |
189 | 189 | fobj.flush() |
190 | 190 | fobj.seek(0) |
| 191 | + mime_type: str = guess_type(Path(fobj.name)) |
| 192 | + if should_compress_mime_type_for_upload(mime_type): |
| 193 | + return_file = inmemory_compress(fobj) |
| 194 | + file_header = {"Content-Encoding": "gzip"} |
| 195 | + # Server side, our upload handler does not make it easy to |
| 196 | + # access the 'Content-Encoding' header and handle properly. But |
| 197 | + # we can access Content-Type, so we use a subtype parameter for |
| 198 | + # this, as per https://httpwg.org/specs/rfc9110.html#media.type |
| 199 | + mime_type = f"{mime_type}; gzipped=True" |
| 200 | + else: |
| 201 | + return_file = fobj |
| 202 | + file_header = {} |
191 | 203 | return ( |
192 | 204 | field_name, |
193 | | - (fobj.name, fobj, guess_type(Path(fobj.name)), file_header), |
| 205 | + (fobj.name, return_file, mime_type, file_header), |
194 | 206 | ) |
195 | 207 |
|
196 | 208 | fields = [_mk_file_fields(k, x) for (k, v) in files.items() for x in v] |
@@ -285,3 +297,44 @@ def do_download_file(download_url: t.Union[str, furl], fn: t.Optional[NPath] = N |
285 | 297 | for chunk in r.iter_content(chunk_size=1024 * 1024): # 1MB chunks |
286 | 298 | ffile.write(chunk) |
287 | 299 | return fn |
| 300 | + |
| 301 | + |
| 302 | +def should_compress_mime_type_for_upload(mime_type: str) -> bool: |
| 303 | + # Most file types people deal with are fairly well compressed already, and |
| 304 | + # there are many, so we use the approach of listing ones we guess will |
| 305 | + # benefit from compression rather than the other way around. |
| 306 | + return mime_type in _SHOULD_COMPRESS_MIME_TYPES |
| 307 | + |
| 308 | + |
| 309 | +# Mostly borrowed from here: https://letstalkaboutwebperf.com/en/gzip-brotli-server-config/ |
| 310 | +_SHOULD_COMPRESS_MIME_TYPES = { |
| 311 | + "application/atom+xml", |
| 312 | + "application/javascript", |
| 313 | + "application/json", |
| 314 | + "application/manifest+json", |
| 315 | + "application/vnd.pickle+binary", |
| 316 | + "application/rss+xml", |
| 317 | + "application/vnd.api+json", |
| 318 | + "application/vnd.ms-fontobject", |
| 319 | + "application/x-font-opentype", |
| 320 | + "application/x-font-truetype", |
| 321 | + "application/x-font-ttf", |
| 322 | + "application/x-javascript", |
| 323 | + "application/xhtml+xml", |
| 324 | + "application/xhtml+xml", |
| 325 | + "application/xml", |
| 326 | + "font/eot", |
| 327 | + "font/opentype", |
| 328 | + "font/otf", |
| 329 | + "font/ttf", |
| 330 | + "image/svg+xml", |
| 331 | + "image/vnd.microsoft.icon", |
| 332 | + "image/x-icon", |
| 333 | + "text/css", |
| 334 | + "text/csv", |
| 335 | + "text/html", |
| 336 | + "text/javascript", |
| 337 | + "text/plain", |
| 338 | + "text/x-component", |
| 339 | + "text/xml", |
| 340 | +} |