Skip to content

Commit

Permalink
refactor: support file download redirect (#37)
Browse files Browse the repository at this point in the history
* refactor: support file download redirect

* chore: bump version
  • Loading branch information
imathews authored Mar 19, 2024
1 parent be71665 commit ef9b12d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/redivis/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.14.5"
__version__ = "0.14.6"
23 changes: 19 additions & 4 deletions src/redivis/classes/File.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import os
from io import BytesIO
import pathlib
import re
import urllib
from .Base import Base
from ..common.api_request import make_request
from urllib.parse import quote as quote_uri
Expand Down Expand Up @@ -38,7 +40,7 @@ def download(self, path=None, *, overwrite=False, progress=True, on_progress=Non
elif os.path.exists(path) and os.path.isdir(path):
is_dir = True

with make_request(method="GET", path=f'{self.uri}', stream=True, parse_response=False) as r:
with make_request(method="GET", path=f'{self.uri}', query={"allowRedirect": "true"}, stream=True, parse_response=False) as r:
parse_headers(self, r)
name = self.properties["name"]

Expand Down Expand Up @@ -83,7 +85,20 @@ def stream(self):


def parse_headers(file, res):
file.properties["name"] = res.headers['x-redivis-filename']
file.properties["md5Hash"] = res.headers['digest'].replace("md5=", "")
file.properties["name"] = get_filename(res.headers['content-disposition'])
# TODO: md5Hash from x-goog-hash
file.properties["contentType"] = res.headers['content-type']
file.properties["size"] = int(res.headers['x-redivis-size'])
file.properties["size"] = int(res.headers['content-length'] or res.headers['x-goog-stored-content-length'])


def get_filename(s):
fname = re.findall("filename\*=([^;]+)", s, flags=re.IGNORECASE)
if not fname:
fname = re.findall("filename=([^;]+)", s, flags=re.IGNORECASE)
if "utf-8''" in fname[0].lower():
fname = re.sub("utf-8''", '', fname[0], flags=re.IGNORECASE)
fname = urllib.unquote(fname).decode('utf8')
else:
fname = fname[0]
# clean space and double quotes
return fname.strip().strip('"')
3 changes: 2 additions & 1 deletion src/redivis/classes/Table.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,4 +593,5 @@ def update_properties(instance, properties):
instance.qualified_reference = properties["qualifiedReference"]
instance.scoped_reference = properties["scopedReference"]
instance.name = properties["name"]
instance.uri = properties["uri"]
instance.uri = properties["uri"]

0 comments on commit ef9b12d

Please sign in to comment.