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

Method actionUpdate

src/File/FileRequest.py:103–204  ·  view source on GitHub ↗
(self, params)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 14

responseMethod · 0.95
isServingMethod · 0.80
badActionMethod · 0.80
addPeerMethod · 0.80
onceMethod · 0.80
goodActionMethod · 0.80
getMethod · 0.45
readMethod · 0.45
getFileMethod · 0.45
verifyFileMethod · 0.45
writeMethod · 0.45
publishMethod · 0.45

Tested by

no test coverage detected