GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — dp-server: Org: Fixed File uploading · datapane/datapane@4ce23b5

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • python-client/src/datapane
    • cloud_api
      • common.py
    • common
      • ops_utils.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎python-client/src/datapane/cloud_api/common.py‎

Copy file name to clipboardExpand all lines: python-client/src/datapane/cloud_api/common.py
+55-2Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from datapane.client.exceptions import IncompatibleVersionError, ReportTooLargeError, UnsupportedResourceError
3535
from datapane.client.utils import TEST_ENV
3636
from datapane.common import JSON, MIME, SIZE_1_MB, NPath, guess_type
37+
from datapane.common.ops_utils import inmemory_compress
3738

3839
################################################################################
3940
# Tmpfile handling
@@ -183,14 +184,25 @@ def post(self, params: t.Dict = None, overwrite: bool = False, **data: JSON) ->
183184
def post_files(self, files: FileAttachmentList, overwrite: bool = False, **data: JSON) -> Munch:
184185
# upload files using custom json-data protocol
185186
# build the fields
186-
file_header = {"Content-Encoding": "gzip"}
187187

188188
def _mk_file_fields(field_name: str, fobj: t.BinaryIO):
189189
fobj.flush()
190190
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 = {}
191203
return (
192204
field_name,
193-
(fobj.name, fobj, guess_type(Path(fobj.name)), file_header),
205+
(fobj.name, return_file, mime_type, file_header),
194206
)
195207

196208
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
285297
for chunk in r.iter_content(chunk_size=1024 * 1024): # 1MB chunks
286298
ffile.write(chunk)
287299
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+
}
Collapse file

‎python-client/src/datapane/common/ops_utils.py‎

Copy file name to clipboardExpand all lines: python-client/src/datapane/common/ops_utils.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def compress_file(f_name: NPath, level: int = 6) -> t.Generator[str, None, None]
8383
os.unlink(f_name_gz)
8484

8585

86-
def inmemory_compress(content: io.BytesIO) -> io.BytesIO:
86+
def inmemory_compress(content: t.BinaryIO) -> t.BinaryIO:
8787
"""(x-plat) Memory-based gzip compression"""
8888
content.seek(0)
8989
zbuf = io.BytesIO()

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.