Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit 1886fe6

Browse files
authored
Merge pull request #433 from humanprotocol/support-public-uri-download
Add support download files drom public URI
2 parents 68e1d7a + 9a1725e commit 1886fe6

File tree

5 files changed

+28
-4
lines changed

5 files changed

+28
-4
lines changed

hmt_escrow/storage.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import json
33
import logging
44
import os
5+
import urllib.request
6+
import re
57
from typing import Dict, Tuple, Optional, Union
68

79
import boto3
@@ -163,7 +165,13 @@ def download(key: str, private_key: bytes, public: bool = False) -> Dict:
163165
164166
"""
165167
try:
166-
content = download_from_storage(key=key, public=public)
168+
url_pattern = "^https?:\\/\\/(?:www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b(?:[-a-zA-Z0-9()@:%_\\+.~#?&\\/=]*)$"
169+
is_url = re.match(url_pattern, key)
170+
content = (
171+
urllib.request.urlopen(key).read()
172+
if is_url
173+
else download_from_storage(key=key, public=public)
174+
)
167175
artifact = (
168176
crypto.decrypt(private_key, content)
169177
if crypto.is_encrypted(content) is True

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hmt-escrow",
3-
"version": "0.14.5",
3+
"version": "0.14.6",
44
"description": "Launch escrow contracts to the HUMAN network",
55
"main": "truffle.js",
66
"directories": {

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setuptools.setup(
44
name="hmt-escrow",
5-
version="0.14.5",
5+
version="0.14.6",
66
author="HUMAN Protocol",
77
description="A python library to launch escrow contracts to the HUMAN network.",
88
url="https://github.com/humanprotocol/hmt-escrow",

test/hmt_escrow/storage/test_storage.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,22 @@ def test_public_private_download_from_storage(self):
209209
# Download from storage must be called as PRIVATE (public is TRUE)
210210
download_mock.assert_called_once_with(key=file_key, public=True)
211211

212+
def test_download_from_public_resource(self):
213+
file_key = "https://s3aaa.com"
214+
sample_data = '{"a": 1, "b": 2}'
215+
216+
with patch("urllib.request.urlopen") as mock_urlopen:
217+
cm = MagicMock()
218+
cm.read.side_effect = [
219+
crypto.encrypt(self.pub_key, sample_data),
220+
sample_data.encode("utf-8"),
221+
]
222+
mock_urlopen.return_value = cm
223+
224+
downloaded = download(key=file_key, private_key=self.priv_key)
225+
self.assertEqual(json.dumps(downloaded), sample_data)
226+
mock_urlopen.assert_called_once()
227+
212228

213229
if __name__ == "__main__":
214230
unittest.main(exit=True)

0 commit comments

Comments
 (0)