(self, params)
| 101 | |
| 102 | # Update a site file request |
| 103 | def actionUpdate(self, params): |
| 104 | site = self.sites.get(params["site"]) |
| 105 | if not site or not site.isServing(): # Site unknown or not serving |
| 106 | self.response({"error": "Unknown site"}) |
| 107 | self.connection.badAction(1) |
| 108 | self.connection.badAction(5) |
| 109 | return False |
| 110 | |
| 111 | inner_path = params.get("inner_path", "") |
| 112 | current_content_modified = site.content_manager.contents.get(inner_path, {}).get("modified", 0) |
| 113 | body = params["body"] |
| 114 | |
| 115 | if not inner_path.endswith("content.json"): |
| 116 | self.response({"error": "Only content.json update allowed"}) |
| 117 | self.connection.badAction(5) |
| 118 | return |
| 119 | |
| 120 | should_validate_content = True |
| 121 | if "modified" in params and params["modified"] <= current_content_modified: |
| 122 | should_validate_content = False |
| 123 | valid = None # Same or earlier content as we have |
| 124 | elif not body: # No body sent, we have to download it first |
| 125 | self.log.debug("Missing body from update, downloading...") |
| 126 | peer = site.addPeer(self.connection.ip, self.connection.port, return_peer=True, source="update") # Add or get peer |
| 127 | try: |
| 128 | body = peer.getFile(site.address, inner_path).read() |
| 129 | except Exception as err: |
| 130 | self.log.debug("Can't download updated file %s: %s" % (inner_path, err)) |
| 131 | self.response({"error": "File invalid update: Can't download updaed file"}) |
| 132 | self.connection.badAction(5) |
| 133 | return |
| 134 | |
| 135 | if should_validate_content: |
| 136 | try: |
| 137 | content = json.loads(body.decode()) |
| 138 | except Exception as err: |
| 139 | self.log.debug("Update for %s is invalid JSON: %s" % (inner_path, err)) |
| 140 | self.response({"error": "File invalid JSON"}) |
| 141 | self.connection.badAction(5) |
| 142 | return |
| 143 | |
| 144 | file_uri = "%s/%s:%s" % (site.address, inner_path, content["modified"]) |
| 145 | |
| 146 | if self.server.files_parsing.get(file_uri): # Check if we already working on it |
| 147 | valid = None # Same file |
| 148 | else: |
| 149 | try: |
| 150 | valid = site.content_manager.verifyFile(inner_path, content) |
| 151 | except Exception as err: |
| 152 | self.log.debug("Update for %s is invalid: %s" % (inner_path, err)) |
| 153 | error = err |
| 154 | valid = False |
| 155 | |
| 156 | if valid is True: # Valid and changed |
| 157 | site.log.info("Update for %s looks valid, saving..." % inner_path) |
| 158 | self.server.files_parsing[file_uri] = True |
| 159 | site.storage.write(inner_path, body) |
| 160 | del params["body"] |
nothing calls this directly
no test coverage detected