Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fileinfo for arweave file #672

Merged
merged 3 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions ocean_provider/file_types/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ def check_details(self, with_checksum=False):
if result:
status_code = result.status_code
headers = copy.deepcopy(result.headers)
files_url = copy.deepcopy(result.url)
if "arweave" in result.url:
mariacarmina marked this conversation as resolved.
Show resolved Hide resolved
files_url = ""
mariacarmina marked this conversation as resolved.
Show resolved Hide resolved
else:
files_url = copy.deepcopy(result.url)
# always close requests session, see https://requests.readthedocs.io/en/latest/user/advanced/#body-content-workflow
result.close()
if status_code == 200:
Expand All @@ -85,18 +88,7 @@ def check_details(self, with_checksum=False):
file_name = None

if files_url:
filename = urlparse(files_url).path.split("/")[-1]
try:
if not self._validate_filename(filename):
msg = "Invalid file name format. It was not possible to get the file name."
logger.error(msg)
return False, {"error": msg}

file_name = filename
except Exception as e:
msg = f"It was not possible to get the file name. {e}"
logger.warning(msg)
return False, {"error": msg}
file_name = urlparse(files_url).path.split("/")[-1]

if not content_length and content_range:
# sometimes servers send content-range instead
Expand Down Expand Up @@ -128,10 +120,6 @@ def check_details(self, with_checksum=False):

return False, {}

def _validate_filename(self, header: str) -> bool:
pattern = re.compile(r"\\|\.\.|/")
return not bool(pattern.findall(header))

def _get_result_from_url(self, with_checksum=False):
func, func_args = self._get_func_and_args()

Expand Down
12 changes: 12 additions & 0 deletions ocean_provider/file_types/file_types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import logging
import os
import re
from typing import Any, Optional, Tuple
from urllib.parse import urljoin
from uuid import uuid4
Expand Down Expand Up @@ -34,11 +35,22 @@ def validate_dict(self) -> Tuple[bool, Any]:
if self.method not in ["get", "post"]:
return False, f"Unsafe method {self.method}."

if not self.validate_url(self.url):
msg = "Invalid file name format. It was not possible to get the file name."
logger.error(msg)
return False, msg

return True, self

def get_download_url(self):
return self.url

def validate_url(self, url: str) -> bool:
pattern = re.compile(r"^(.+)\/([^/]+)$")
if url.startswith("http://") or url.startswith("https://"):
return True
return not bool(pattern.findall(url))

@enforce_types
def get_filename(self) -> str:
return self.url.split("/")[-1]
Expand Down
11 changes: 11 additions & 0 deletions ocean_provider/utils/test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,17 @@ def test_validate_url_object():
assert result is False
assert message == "Unsafe method delete."

url_object = {
"url": "./../dir",
"type": "url",
"method": "GET",
}
result, message = FilesTypeFactory.validate_and_create(url_object)
assert result is False
assert (
message == "Invalid file name format. It was not possible to get the file name."
)


@pytest.mark.unit
def test_build_download_response_ipfs():
Expand Down
1 change: 1 addition & 0 deletions tests/test_fileinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def test_check_arweave_good(client):
assert file_info["contentType"] == "application/octet-stream"
assert file_info["valid"] is True
assert file_info["type"] == "arweave"
assert file_info["name"] == ""


@pytest.mark.unit
Expand Down
Loading