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

Method needFile

src/Site/Site.py:744–783  ·  view source on GitHub ↗
(self, inner_path, update=False, blocking=True, peer=None, priority=0)

Source from the content-addressed store, hash-verified

742
743 # Check and download if file not exist
744 def needFile(self, inner_path, update=False, blocking=True, peer=None, priority=0):
745 if self.storage.isFile(inner_path) and not update: # File exist, no need to do anything
746 return True
747 elif not self.isServing(): # Site not serving
748 return False
749 else: # Wait until file downloaded
750 self.bad_files[inner_path] = self.bad_files.get(inner_path, 0) + 1 # Mark as bad file
751 if not self.content_manager.contents.get("content.json"): # No content.json, download it first!
752 self.log.debug("Need content.json first")
753 gevent.spawn(self.announce)
754 if inner_path != "content.json": # Prevent double download
755 task = self.worker_manager.addTask("content.json", peer)
756 task["evt"].get()
757 self.content_manager.loadContent()
758 if not self.content_manager.contents.get("content.json"):
759 return False # Content.json download failed
760
761 file_info = None
762 if not inner_path.endswith("content.json"):
763 file_info = self.needFileInfo(inner_path)
764 if not file_info:
765 return False
766 if "cert_signers" in file_info and not file_info["content_inner_path"] in self.content_manager.contents:
767 self.log.debug("Missing content.json for requested user file: %s" % inner_path)
768 if self.bad_files.get(file_info["content_inner_path"], 0) > 5:
769 self.log.debug("File %s not reachable: retry %s" % (
770 inner_path, self.bad_files.get(file_info["content_inner_path"], 0)
771 ))
772 return False
773 self.downloadContent(file_info["content_inner_path"])
774
775 if not self.isFileDownloadAllowed(inner_path, file_info):
776 self.log.debug("%s: Download not allowed" % inner_path)
777 return False
778
779 task = self.worker_manager.addTask(inner_path, peer, priority=priority, file_info=file_info)
780 if blocking:
781 return task["evt"].get()
782 else:
783 return task["evt"]
784
785 # Add or update a peer to site
786 # return_peer: Always return the peer even if it was already present

Callers 14

siteNeedFileMethod · 0.95
downloadContentMethod · 0.95
redownloadContentsMethod · 0.95
pooledNeedFileMethod · 0.95
actionFileWriteMethod · 0.45
actionFileGetMethod · 0.45
actionFileNeedMethod · 0.45
actionSiteMediaMethod · 0.45
getDbMethod · 0.45
verifyFilesMethod · 0.45
actionUpdateMethod · 0.45
testRenameOptionalMethod · 0.45

Calls 8

isServingMethod · 0.95
needFileInfoMethod · 0.95
downloadContentMethod · 0.95
isFileDownloadAllowedMethod · 0.95
loadContentMethod · 0.80
isFileMethod · 0.45
getMethod · 0.45
addTaskMethod · 0.45

Tested by 5

getDbMethod · 0.36
verifyFilesMethod · 0.36
testRenameOptionalMethod · 0.36
testOptionalDownloadMethod · 0.36
testFindOptionalMethod · 0.36