Repository navigation
zulip: Add fetch_upload() for downloading uploaded files. - #948
harshvardhan178251 wants to merge 3 commits into
Conversation
|
Hi — a quick status check on this issue from my side. My PR #948 adds |
|
Hi — I recently opened #950 for the same issue before noticing this PR and that the issue is claimed by you; I have closed mine in favor of yours. One thing worth a look, since it came up in the review of the earlier attempt #932: if fetch_upload accepts absolute URLs, the client's Basic Auth credentials would be sent to whatever host appears in the URL, so it is worth rejecting URLs whose host differs from the realm's host. My now-closed PR has one way of handling that, if it is helpful. Good luck with the PR! |
Incorporate ideas from Johnboard-cmyk's closed PR zulip#950: strip a leading /api/v1/ prefix (people paste the wrong URL shape and hit the JSON temporary-URL instead of the file), accept bare relative paths under user_uploads/, and wrap connection failures in UnrecoverableNetworkError for consistency with do_api_query().
|
Thanks @Johnboard-cmyk — much appreciated. The credential-leak concern you flagged was already implemented in this PR (off-origin absolute URLs are rejected without issuing the request; see |
Fixes: #628
Zulip upload responses return a root-relative
/user_uploads/...URL, andGET /api/v1/user_uploads/...returns a JSON temporary-access URL rather than the file, socall_endpoint()cannot be used to download uploads. This PR addsClient.fetch_upload(url_or_path, timeout=None):user_uploads/1/x.png), a server-root-relative path (/user_uploads/1/x.png), a/user_uploads/temporary/...token URL, or an absolutehttp(s)URL on this Zulip server. A leading/api/v1/prefix is stripped (both for paths and absolute URLs), and the authenticated session follows the 302 to the file or the temporary URL.UnrecoverableNetworkErrorif the request can't be made andZulipErrorif the server returns an HTTP error status.call_endpointand no new API prefix behavior.Design decision worth flagging: the
/api/v1prefix refactor suggested as "one option" in the issue is deliberately not included here — it would break existing bots that rely oncall_endpoint()behaving as before.fetch_upload()is additive only; happy to revisit the refactor as a separate PR.How did you test this PR?
zulip/tests/test_fetch_upload.py(mocked HTTP layer): absolute-URL passthrough, root-relative path resolution against the server root, custom timeout, rejection of bare relative paths, non-200 →ZulipError.zuliptest suite passes locally (17 passed).ruff checkandmypyclean. (The pre-existingzulip_bots/failures were left untouched.)Self-review checklist
Communicate decisions, questions, and potential concerns.
Individual commits are ready for review (see commit discipline).