MCPcopy
hub / github.com/HelloZeroNet/ZeroNet / loadContent

Method loadContent

src/Content/ContentManager.py:72–303  ·  view source on GitHub ↗
(self, content_inner_path="content.json", add_bad_files=True, delete_removed_files=True, load_includes=True, force=False)

Source from the content-addressed store, hash-verified

70 # Load content.json to self.content
71 # Return: Changed files ["index.html", "data/messages.json"], Deleted files ["old.jpg"]
72 def loadContent(self, content_inner_path="content.json", add_bad_files=True, delete_removed_files=True, load_includes=True, force=False):
73 content_inner_path = content_inner_path.strip("/") # Remove / from beginning
74 old_content = self.contents.get(content_inner_path)
75 content_path = self.site.storage.getPath(content_inner_path)
76 content_dir = helper.getDirname(self.site.storage.getPath(content_inner_path))
77 content_inner_dir = helper.getDirname(content_inner_path)
78
79 if os.path.isfile(content_path):
80 try:
81 # Check if file is newer than what we have
82 if not force and old_content and not self.site.settings.get("own"):
83 for line in open(content_path):
84 if '"modified"' not in line:
85 continue
86 match = re.search(r"([0-9\.]+),$", line.strip(" \r\n"))
87 if match and float(match.group(1)) <= old_content.get("modified", 0):
88 self.log.debug("%s loadContent same json file, skipping" % content_inner_path)
89 return [], []
90
91 new_content = self.site.storage.loadJson(content_inner_path)
92 except Exception as err:
93 self.log.warning("%s load error: %s" % (content_path, Debug.formatException(err)))
94 return [], []
95 else:
96 self.log.debug("Content.json not exist: %s" % content_path)
97 return [], [] # Content.json not exist
98
99 try:
100 # Get the files where the sha512 changed
101 changed = []
102 deleted = []
103 # Check changed
104 for relative_path, info in new_content.get("files", {}).items():
105 if "sha512" in info:
106 hash_type = "sha512"
107 else: # Backward compatibility
108 hash_type = "sha1"
109
110 new_hash = info[hash_type]
111 if old_content and old_content["files"].get(relative_path): # We have the file in the old content
112 old_hash = old_content["files"][relative_path].get(hash_type)
113 else: # The file is not in the old content
114 old_hash = None
115 if old_hash != new_hash:
116 changed.append(content_inner_dir + relative_path)
117
118 # Check changed optional files
119 for relative_path, info in new_content.get("files_optional", {}).items():
120 file_inner_path = content_inner_dir + relative_path
121 new_hash = info["sha512"]
122 if old_content and old_content.get("files_optional", {}).get(relative_path):
123 # We have the file in the old content
124 old_hash = old_content["files_optional"][relative_path].get("sha512")
125 if old_hash != new_hash and self.site.isDownloadable(file_inner_path):
126 changed.append(file_inner_path) # Download new file
127 elif old_hash != new_hash and self.hashfield.hasHash(old_hash) and not self.site.settings.get("own"):
128 try:
129 old_hash_id = self.hashfield.getHashId(old_hash)

Callers 14

loadContentsMethod · 0.95
actionSiteSignMethod · 0.80
actionSiteReloadMethod · 0.80
actionFileWriteMethod · 0.80
actionFileDeleteMethod · 0.80
verifyFilesMethod · 0.80
downloadContentMethod · 0.80
updateMethod · 0.80
cloneMethod · 0.80
needFileMethod · 0.80
testBigUpdateMethod · 0.80
testOptionalDeleteMethod · 0.80

Calls 15

optionalRemovedMethod · 0.95
optionalDeleteMethod · 0.95
getFileChangesMethod · 0.95
optionalRenamedMethod · 0.95
removeContentMethod · 0.95
getTotalSizeMethod · 0.95
listModifiedMethod · 0.95
getPathMethod · 0.80
loadJsonMethod · 0.80
itemsMethod · 0.80
hasHashMethod · 0.80
renameMethod · 0.80

Tested by 4

verifyFilesMethod · 0.64
testBigUpdateMethod · 0.64
testOptionalDeleteMethod · 0.64
testOptionalRenameMethod · 0.64