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

Method hashFile

plugins/Bigfile/BigfilePlugin.py:282–332  ·  view source on GitHub ↗
(self, dir_inner_path, file_relative_path, optional=False)

Source from the content-addressed store, hash-verified

280 }
281
282 def hashFile(self, dir_inner_path, file_relative_path, optional=False):
283 inner_path = dir_inner_path + file_relative_path
284
285 file_size = self.site.storage.getSize(inner_path)
286 # Only care about optional files >1MB
287 if not optional or file_size < 1 * 1024 * 1024:
288 return super(ContentManagerPlugin, self).hashFile(dir_inner_path, file_relative_path, optional)
289
290 back = {}
291 content = self.contents.get(dir_inner_path + "content.json")
292
293 hash = None
294 piecemap_relative_path = None
295 piece_size = None
296
297 # Don't re-hash if it's already in content.json
298 if content and file_relative_path in content.get("files_optional", {}):
299 file_node = content["files_optional"][file_relative_path]
300 if file_node["size"] == file_size:
301 self.log.info("- [SAME SIZE] %s" % file_relative_path)
302 hash = file_node.get("sha512")
303 piecemap_relative_path = file_node.get("piecemap")
304 piece_size = file_node.get("piece_size")
305
306 if not hash or not piecemap_relative_path: # Not in content.json yet
307 if file_size < 5 * 1024 * 1024: # Don't create piecemap automatically for files smaller than 5MB
308 return super(ContentManagerPlugin, self).hashFile(dir_inner_path, file_relative_path, optional)
309
310 self.log.info("- [HASHING] %s" % file_relative_path)
311 merkle_root, piece_size, piecemap_info = self.hashBigfile(self.site.storage.open(inner_path, "rb"), file_size)
312 if not hash:
313 hash = merkle_root
314
315 if not piecemap_relative_path:
316 file_name = helper.getFilename(file_relative_path)
317 piecemap_relative_path = file_relative_path + ".piecemap.msgpack"
318 piecemap_inner_path = inner_path + ".piecemap.msgpack"
319
320 self.site.storage.open(piecemap_inner_path, "wb").write(Msgpack.pack({file_name: piecemap_info}))
321
322 back.update(super(ContentManagerPlugin, self).hashFile(dir_inner_path, piecemap_relative_path, optional=True))
323
324 piece_num = int(math.ceil(float(file_size) / piece_size))
325
326 # Add the merkle root to hashfield
327 hash_id = self.site.content_manager.hashfield.getHashId(hash)
328 self.optionalDownloaded(inner_path, hash_id, file_size, own=True)
329 self.site.storage.piecefields[hash].frombytes(b"\x01" * piece_num)
330
331 back[file_relative_path] = {"sha512": hash, "size": file_size, "piecemap": piecemap_relative_path, "piece_size": piece_size}
332 return back
333
334 def getPiecemap(self, inner_path):
335 file_info = self.site.content_manager.getFileInfo(inner_path)

Callers

nothing calls this directly

Calls 10

hashBigfileMethod · 0.95
optionalDownloadedMethod · 0.95
getSizeMethod · 0.80
openMethod · 0.80
getMethod · 0.45
writeMethod · 0.45
packMethod · 0.45
updateMethod · 0.45
getHashIdMethod · 0.45
frombytesMethod · 0.45

Tested by

no test coverage detected