| 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 |