Repository: ResearchObject/ro-crate-py

Issue:
Add contentUrl property to files when it's known
E.g., when a remote file is added with `fetch_remote=True`. See https://github.com/ResearchObject/ro-crate/pull/189#issuecomment-1227697348.

Discussion:

Patch:
diff --git a/rocrate/model/file.py b/rocrate/model/file.py
index 9251aeb..a5d1c43 100644
--- a/rocrate/model/file.py
+++ b/rocrate/model/file.py
@@ -59,6 +59,7 @@ class File(FileOrDir):
                     if self.fetch_remote:
                         out_file_path.parent.mkdir(parents=True, exist_ok=True)
                         urllib.request.urlretrieve(response.url, out_file_path)
+                        self._jsonld['contentUrl'] = str(self.source)
         elif self.source is None:
             # Allows to record a File entity whose @id does not exist, see #73
             warnings.warn(f"No source for {self.id}")


Test patch:
diff --git a/test/test_write.py b/test/test_write.py
index 10f0ec3..6fb57b6 100644
--- a/test/test_write.py
+++ b/test/test_write.py
@@ -150,6 +150,7 @@ def test_remote_uri(tmpdir, helpers, fetch_remote, validate_url, to_zip):
     if fetch_remote:
         out_file = out_crate.dereference(file_.id)
         assert (out_path / relpath).is_file()
+        assert out_file["contentUrl"] == url
     else:
         out_file = out_crate.dereference(url)
         assert not (out_path / relpath).exists()