Skip to content

Commit

Permalink
Detect Zotero-compressed data by inspecting the header
Browse files Browse the repository at this point in the history
  • Loading branch information
urschrei committed Oct 5, 2023
1 parent 7e9aec0 commit ff3cefe
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/pyzotero/zotero.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,20 +224,13 @@ def wrapped_f(self, *args, **kwargs):
# clear all query parameters
self.url_params = None
# Zotero API returns plain-text attachments as zipped content
# We can inspect the redirect header to check whether Zotero compressed the file
if fmt == "zip":
# we need to check whether it was originally zipped, or zipped plain text
# 1. get request path
url_path = PurePosixPath(unquote(urlparse(self.self_link).path))
# 2. we don't want the file again: we want the item
if url_path.parts[-1] == "file":
fixed_path = str(Path(*url_path.parts[:-1]))
else:
fixed_path = str(Path(*url_path.parts))
# 3. Get the item
item = self._retrieve_data(fixed_path)
itemtype = item.json()["data"]["contentType"]
if itemtype == "text/plain":
# Mismatch! decompress it transparently
if (
self.request.history
and self.request.history[0].headers.get("Zotero-File-Compressed")
== "Yes"
):
z = zipfile.ZipFile(io.BytesIO(retrieved.content))
namelist = z.namelist()
file = z.read(namelist[0])
Expand Down

0 comments on commit ff3cefe

Please sign in to comment.