MCPcopy Index your code
hub / github.com/HelloZeroNet/ZeroNet / verifyContent

Method verifyContent

src/Content/ContentManager.py:832–886  ·  view source on GitHub ↗
(self, inner_path, content)

Source from the content-addressed store, hash-verified

830 # Checks if the content.json content is valid
831 # Return: True or False
832 def verifyContent(self, inner_path, content):
833 content_size = len(json.dumps(content, indent=1)) + sum([file["size"] for file in list(content["files"].values()) if file["size"] >= 0]) # Size of new content
834 # Calculate old content size
835 old_content = self.contents.get(inner_path)
836 if old_content:
837 old_content_size = len(json.dumps(old_content, indent=1)) + sum([file["size"] for file in list(old_content.get("files", {}).values())])
838 old_content_size_optional = sum([file["size"] for file in list(old_content.get("files_optional", {}).values())])
839 else:
840 old_content_size = 0
841 old_content_size_optional = 0
842
843 # Reset site site on first content.json
844 if not old_content and inner_path == "content.json":
845 self.site.settings["size"] = 0
846
847 content_size_optional = sum([file["size"] for file in list(content.get("files_optional", {}).values()) if file["size"] >= 0])
848 site_size = self.site.settings["size"] - old_content_size + content_size # Site size without old content plus the new
849 site_size_optional = self.site.settings["size_optional"] - old_content_size_optional + content_size_optional # Site size without old content plus the new
850
851 site_size_limit = self.site.getSizeLimit() * 1024 * 1024
852
853 # Check site address
854 if content.get("address") and content["address"] != self.site.address:
855 raise VerifyError("Wrong site address: %s != %s" % (content["address"], self.site.address))
856
857 # Check file inner path
858 if content.get("inner_path") and content["inner_path"] != inner_path:
859 raise VerifyError("Wrong inner_path: %s" % content["inner_path"])
860
861 # Check total site size limit
862 if site_size > site_size_limit:
863 if inner_path == "content.json" and self.site.settings["size"] == 0:
864 # First content.json download, save site size to display warning
865 self.site.settings["size"] = site_size
866 task = self.site.worker_manager.findTask(inner_path)
867 if task: # Dont try to download from other peers
868 self.site.worker_manager.failTask(task)
869 raise VerifyError("Content too large %sB > %sB, aborting task..." % (site_size, site_size_limit))
870
871 # Verify valid filenames
872 for file_relative_path in list(content.get("files", {}).keys()) + list(content.get("files_optional", {}).keys()):
873 if not self.isValidRelativePath(file_relative_path):
874 raise VerifyError("Invalid relative path: %s" % file_relative_path)
875
876 if inner_path == "content.json":
877 self.site.settings["size"] = site_size
878 self.site.settings["size_optional"] = site_size_optional
879 return True # Root content.json is passed
880 else:
881 if self.verifyContentInclude(inner_path, content, content_size, content_size_optional):
882 self.site.settings["size"] = site_size
883 self.site.settings["size_optional"] = site_size_optional
884 return True
885 else:
886 return False
887
888 def verifyContentInclude(self, inner_path, content, content_size, content_size_optional):
889 # Load include details

Callers 2

signMethod · 0.95
verifyFileMethod · 0.95

Calls 9

isValidRelativePathMethod · 0.95
verifyContentIncludeMethod · 0.95
sumFunction · 0.85
VerifyErrorClass · 0.85
valuesMethod · 0.80
getSizeLimitMethod · 0.80
findTaskMethod · 0.80
failTaskMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected