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

Commit

Permalink
Merge pull request #433 from humanprotocol/support-public-uri-download
Browse files Browse the repository at this point in the history
Add support download files drom public URI
  • Loading branch information
alidzm authored Nov 22, 2022
2 parents 68e1d7a + 9a1725e commit 1886fe6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
10 changes: 9 additions & 1 deletion hmt_escrow/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import json
import logging
import os
import urllib.request
import re
from typing import Dict, Tuple, Optional, Union

import boto3
Expand Down Expand Up @@ -163,7 +165,13 @@ def download(key: str, private_key: bytes, public: bool = False) -> Dict:
"""
try:
content = download_from_storage(key=key, public=public)
url_pattern = "^https?:\\/\\/(?:www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b(?:[-a-zA-Z0-9()@:%_\\+.~#?&\\/=]*)$"
is_url = re.match(url_pattern, key)
content = (
urllib.request.urlopen(key).read()
if is_url
else download_from_storage(key=key, public=public)
)
artifact = (
crypto.decrypt(private_key, content)
if crypto.is_encrypted(content) is True
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hmt-escrow",
"version": "0.14.5",
"version": "0.14.6",
"description": "Launch escrow contracts to the HUMAN network",
"main": "truffle.js",
"directories": {
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setuptools.setup(
name="hmt-escrow",
version="0.14.5",
version="0.14.6",
author="HUMAN Protocol",
description="A python library to launch escrow contracts to the HUMAN network.",
url="https://github.com/humanprotocol/hmt-escrow",
Expand Down
16 changes: 16 additions & 0 deletions test/hmt_escrow/storage/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,22 @@ def test_public_private_download_from_storage(self):
# Download from storage must be called as PRIVATE (public is TRUE)
download_mock.assert_called_once_with(key=file_key, public=True)

def test_download_from_public_resource(self):
file_key = "https://s3aaa.com"
sample_data = '{"a": 1, "b": 2}'

with patch("urllib.request.urlopen") as mock_urlopen:
cm = MagicMock()
cm.read.side_effect = [
crypto.encrypt(self.pub_key, sample_data),
sample_data.encode("utf-8"),
]
mock_urlopen.return_value = cm

downloaded = download(key=file_key, private_key=self.priv_key)
self.assertEqual(json.dumps(downloaded), sample_data)
mock_urlopen.assert_called_once()


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

1 comment on commit 1886fe6

@vercel
Copy link

@vercel vercel bot commented on 1886fe6 Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

dashboard-prod – ./escrow-dashboard

dashboard-prod-humanprotocol.vercel.app
dashboard.humanprotocol.org
dashboard-prod-git-master-humanprotocol.vercel.app

Please sign in to comment.