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

Method actionBigfileUpload

plugins/Bigfile/BigfilePlugin.py:49–113  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

47
48 @helper.encodeResponse
49 def actionBigfileUpload(self):
50 nonce = self.get.get("upload_nonce")
51 if nonce not in upload_nonces:
52 return self.error403("Upload nonce error.")
53
54 upload_info = upload_nonces[nonce]
55 del upload_nonces[nonce]
56
57 self.sendHeader(200, "text/html", noscript=True, extra_headers={
58 "Access-Control-Allow-Origin": "null",
59 "Access-Control-Allow-Credentials": "true"
60 })
61
62 self.readMultipartHeaders(self.env['wsgi.input']) # Skip http headers
63
64 site = upload_info["site"]
65 inner_path = upload_info["inner_path"]
66
67 with site.storage.open(inner_path, "wb", create_dirs=True) as out_file:
68 merkle_root, piece_size, piecemap_info = site.content_manager.hashBigfile(
69 self.env['wsgi.input'], upload_info["size"], upload_info["piece_size"], out_file
70 )
71
72 if len(piecemap_info["sha512_pieces"]) == 1: # Small file, don't split
73 hash = binascii.hexlify(piecemap_info["sha512_pieces"][0])
74 hash_id = site.content_manager.hashfield.getHashId(hash)
75 site.content_manager.optionalDownloaded(inner_path, hash_id, upload_info["size"], own=True)
76
77 else: # Big file
78 file_name = helper.getFilename(inner_path)
79 site.storage.open(upload_info["piecemap"], "wb").write(Msgpack.pack({file_name: piecemap_info}))
80
81 # Find piecemap and file relative path to content.json
82 file_info = site.content_manager.getFileInfo(inner_path, new_file=True)
83 content_inner_path_dir = helper.getDirname(file_info["content_inner_path"])
84 piecemap_relative_path = upload_info["piecemap"][len(content_inner_path_dir):]
85 file_relative_path = inner_path[len(content_inner_path_dir):]
86
87 # Add file to content.json
88 if site.storage.isFile(file_info["content_inner_path"]):
89 content = site.storage.loadJson(file_info["content_inner_path"])
90 else:
91 content = {}
92 if "files_optional" not in content:
93 content["files_optional"] = {}
94
95 content["files_optional"][file_relative_path] = {
96 "sha512": merkle_root,
97 "size": upload_info["size"],
98 "piecemap": piecemap_relative_path,
99 "piece_size": piece_size
100 }
101
102 merkle_root_hash_id = site.content_manager.hashfield.getHashId(merkle_root)
103 site.content_manager.optionalDownloaded(inner_path, merkle_root_hash_id, upload_info["size"], own=True)
104 site.storage.writeJson(file_info["content_inner_path"], content)
105
106 site.content_manager.contents.loadItem(file_info["content_inner_path"]) # reload cache

Callers

nothing calls this directly

Calls 15

readMultipartHeadersMethod · 0.95
error403Method · 0.80
sendHeaderMethod · 0.80
openMethod · 0.80
hashBigfileMethod · 0.80
loadJsonMethod · 0.80
writeJsonMethod · 0.80
loadItemMethod · 0.80
getMethod · 0.45
getHashIdMethod · 0.45
optionalDownloadedMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected