partial cleanup of ContentManager.py
This commit is contained in:
parent
31b6dc4516
commit
b6cb1062ce
1 changed files with 482 additions and 492 deletions
|
@ -11,7 +11,6 @@ class ContentManager:
|
||||||
self.loadContent(add_bad_files=False)
|
self.loadContent(add_bad_files=False)
|
||||||
self.site.settings["size"] = self.getTotalSize()
|
self.site.settings["size"] = self.getTotalSize()
|
||||||
|
|
||||||
|
|
||||||
# Load content.json to self.content
|
# Load content.json to self.content
|
||||||
# Return: Changed files ["index.html", "data/messages.json"]
|
# Return: Changed files ["index.html", "data/messages.json"]
|
||||||
def loadContent(self, content_inner_path="content.json", add_bad_files=True, load_includes=True):
|
def loadContent(self, content_inner_path="content.json", add_bad_files=True, load_includes=True):
|
||||||
|
@ -31,7 +30,6 @@ class ContentManager:
|
||||||
self.log.error("Content.json not exist: %s" % content_path)
|
self.log.error("Content.json not exist: %s" % content_path)
|
||||||
return False # Content.json not exist
|
return False # Content.json not exist
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Get the files where the sha512 changed
|
# Get the files where the sha512 changed
|
||||||
changed = []
|
changed = []
|
||||||
|
@ -83,7 +81,6 @@ class ContentManager:
|
||||||
|
|
||||||
return changed
|
return changed
|
||||||
|
|
||||||
|
|
||||||
# Get total size of site
|
# Get total size of site
|
||||||
# Return: 32819 (size of files in kb)
|
# Return: 32819 (size of files in kb)
|
||||||
def getTotalSize(self, ignore=None):
|
def getTotalSize(self, ignore=None):
|
||||||
|
@ -95,7 +92,6 @@ class ContentManager:
|
||||||
total_size += info["size"]
|
total_size += info["size"]
|
||||||
return total_size
|
return total_size
|
||||||
|
|
||||||
|
|
||||||
# Find the file info line from self.contents
|
# Find the file info line from self.contents
|
||||||
# Return: { "sha512": "c29d73d30ee8c9c1b5600e8a84447a6de15a3c3db6869aca4a2a578c1721f518", "size": 41 , "content_inner_path": "content.json"}
|
# Return: { "sha512": "c29d73d30ee8c9c1b5600e8a84447a6de15a3c3db6869aca4a2a578c1721f518", "size": 41 , "content_inner_path": "content.json"}
|
||||||
def getFileInfo(self, inner_path):
|
def getFileInfo(self, inner_path):
|
||||||
|
@ -112,7 +108,8 @@ class ContentManager:
|
||||||
|
|
||||||
if content and "user_contents" in content: # User dir
|
if content and "user_contents" in content: # User dir
|
||||||
back = content["user_contents"]
|
back = content["user_contents"]
|
||||||
back["content_inner_path"] = re.sub("(.*)/.*?$", "\\1/content.json", inner_path) # Content.json is in the users dir
|
# Content.json is in the users dir
|
||||||
|
back["content_inner_path"] = re.sub("(.*)/.*?$", "\\1/content.json", inner_path)
|
||||||
return back
|
return back
|
||||||
|
|
||||||
# No inner path in this dir, lets try the parent dir
|
# No inner path in this dir, lets try the parent dir
|
||||||
|
@ -121,8 +118,8 @@ class ContentManager:
|
||||||
else: # No more parent dirs
|
else: # No more parent dirs
|
||||||
break
|
break
|
||||||
|
|
||||||
return False # Not found
|
# Not found
|
||||||
|
return False
|
||||||
|
|
||||||
# Get rules for the file
|
# Get rules for the file
|
||||||
# Return: The rules for the file or False if not allowed
|
# Return: The rules for the file or False if not allowed
|
||||||
|
@ -157,8 +154,8 @@ class ContentManager:
|
||||||
user_address = re.match(".*/([A-Za-z0-9]*?)/.*?$", inner_path).group(1) # Delivered for directory
|
user_address = re.match(".*/([A-Za-z0-9]*?)/.*?$", inner_path).group(1) # Delivered for directory
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if not content: content = self.site.storage.loadJson(inner_path) # Read the file if no content specificed
|
if not content: content = self.site.storage.loadJson(inner_path) # Read the file if no content specified
|
||||||
except: # Content.json not exist
|
except (Exception, ): # Content.json not exist
|
||||||
return {"signers": [user_address], "user_address": user_address} # Return information that we know for sure
|
return {"signers": [user_address], "user_address": user_address} # Return information that we know for sure
|
||||||
|
|
||||||
"""if not "cert_user_name" in content: # New file, unknown user
|
"""if not "cert_user_name" in content: # New file, unknown user
|
||||||
|
@ -168,8 +165,10 @@ class ContentManager:
|
||||||
user_urn = "%s/%s" % (content["cert_auth_type"], content["cert_user_id"]) # web/nofish@zeroid.bit
|
user_urn = "%s/%s" % (content["cert_auth_type"], content["cert_user_id"]) # web/nofish@zeroid.bit
|
||||||
|
|
||||||
rules = copy.copy(user_contents["permissions"].get(content["cert_user_id"], {})) # Default rules by username
|
rules = copy.copy(user_contents["permissions"].get(content["cert_user_id"], {})) # Default rules by username
|
||||||
if rules == False: return False # User banned
|
if not rules:
|
||||||
if "signers" in rules: rules["signers"] = rules["signers"][:] # Make copy of the signers
|
return False # User banned
|
||||||
|
if "signers" in rules:
|
||||||
|
rules["signers"] = rules["signers"][:] # Make copy of the signers
|
||||||
for permission_pattern, permission_rules in user_contents["permission_rules"].items(): # Regexp rules
|
for permission_pattern, permission_rules in user_contents["permission_rules"].items(): # Regexp rules
|
||||||
if not re.match(permission_pattern, user_urn): continue # Rule is not valid for user
|
if not re.match(permission_pattern, user_urn): continue # Rule is not valid for user
|
||||||
# Update rules if its better than current recorded ones
|
# Update rules if its better than current recorded ones
|
||||||
|
@ -180,7 +179,8 @@ class ContentManager:
|
||||||
else:
|
else:
|
||||||
rules[key] = val
|
rules[key] = val
|
||||||
elif type(val) is int: # Int, update if larger
|
elif type(val) is int: # Int, update if larger
|
||||||
if val > rules[key]: rules[key] = val
|
if val > rules[key]:
|
||||||
|
rules[key] = val
|
||||||
elif hasattr(val, "startswith"): # String, update if longer
|
elif hasattr(val, "startswith"): # String, update if longer
|
||||||
if len(val) > len(rules[key]): rules[key] = val
|
if len(val) > len(rules[key]): rules[key] = val
|
||||||
elif type(val) is list: # List, append
|
elif type(val) is list: # List, append
|
||||||
|
@ -191,11 +191,8 @@ class ContentManager:
|
||||||
rules["signers"].append(user_address) # Add user as valid signer
|
rules["signers"].append(user_address) # Add user as valid signer
|
||||||
rules["user_address"] = user_address
|
rules["user_address"] = user_address
|
||||||
|
|
||||||
|
|
||||||
return rules
|
return rules
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Create and sign a content.json
|
# Create and sign a content.json
|
||||||
# Return: The new content if filewrite = False
|
# Return: The new content if filewrite = False
|
||||||
def sign(self, inner_path="content.json", privatekey=None, filewrite=True, update_changed_files=False, extend=None):
|
def sign(self, inner_path="content.json", privatekey=None, filewrite=True, update_changed_files=False, extend=None):
|
||||||
|
@ -203,7 +200,7 @@ class ContentManager:
|
||||||
if not content: # Content not exist yet, load default one
|
if not content: # Content not exist yet, load default one
|
||||||
self.log.info("File %s not exist yet, loading default values..." % inner_path)
|
self.log.info("File %s not exist yet, loading default values..." % inner_path)
|
||||||
content = {"files": {}, "signs": {}} # Default content.json
|
content = {"files": {}, "signs": {}} # Default content.json
|
||||||
if inner_path == "content.json": # Its the root content.json, add some more fields
|
if inner_path == "content.json": # It's the root content.json, add some more fields
|
||||||
content["title"] = "%s - ZeroNet_" % self.site.address
|
content["title"] = "%s - ZeroNet_" % self.site.address
|
||||||
content["description"] = ""
|
content["description"] = ""
|
||||||
content["signs_required"] = 1
|
content["signs_required"] = 1
|
||||||
|
@ -234,7 +231,6 @@ class ContentManager:
|
||||||
if file_inner_path in content["files"].keys() and hashed_files[file_inner_path]["sha512"] != content["files"][file_inner_path].get("sha512"):
|
if file_inner_path in content["files"].keys() and hashed_files[file_inner_path]["sha512"] != content["files"][file_inner_path].get("sha512"):
|
||||||
changed_files.append(file_path)
|
changed_files.append(file_path)
|
||||||
|
|
||||||
|
|
||||||
self.log.debug("Changed files: %s" % changed_files)
|
self.log.debug("Changed files: %s" % changed_files)
|
||||||
if update_changed_files:
|
if update_changed_files:
|
||||||
for file_path in changed_files:
|
for file_path in changed_files:
|
||||||
|
@ -294,7 +290,6 @@ class ContentManager:
|
||||||
else: # Return the new content
|
else: # Return the new content
|
||||||
return new_content
|
return new_content
|
||||||
|
|
||||||
|
|
||||||
# The valid signers of content.json file
|
# The valid signers of content.json file
|
||||||
# Return: ["1KRxE1s3oDyNDawuYWpzbLUwNm8oDbeEp6", "13ReyhCsjhpuCVahn1DHdf6eMqqEVev162"]
|
# Return: ["1KRxE1s3oDyNDawuYWpzbLUwNm8oDbeEp6", "13ReyhCsjhpuCVahn1DHdf6eMqqEVev162"]
|
||||||
def getValidSigners(self, inner_path, content=None):
|
def getValidSigners(self, inner_path, content=None):
|
||||||
|
@ -307,15 +302,14 @@ class ContentManager:
|
||||||
if rules and "signers" in rules:
|
if rules and "signers" in rules:
|
||||||
valid_signers += rules["signers"]
|
valid_signers += rules["signers"]
|
||||||
|
|
||||||
if self.site.address not in valid_signers: valid_signers.append(self.site.address) # Site address always valid
|
if self.site.address not in valid_signers:
|
||||||
|
valid_signers.append(self.site.address) # Site address always valid
|
||||||
return valid_signers
|
return valid_signers
|
||||||
|
|
||||||
|
|
||||||
# Return: The required number of valid signs for the content.json
|
# Return: The required number of valid signs for the content.json
|
||||||
def getSignsRequired(self, inner_path, content=None):
|
def getSignsRequired(self, inner_path, content=None):
|
||||||
return 1 # Todo: Multisig
|
return 1 # Todo: Multisig
|
||||||
|
|
||||||
|
|
||||||
def verifyCert(self, inner_path, content):
|
def verifyCert(self, inner_path, content):
|
||||||
from Crypt import CryptBitcoin
|
from Crypt import CryptBitcoin
|
||||||
|
|
||||||
|
@ -329,7 +323,6 @@ class ContentManager:
|
||||||
return False
|
return False
|
||||||
return CryptBitcoin.verify("%s#%s/%s" % (rules["user_address"], content["cert_auth_type"], name), cert_address, content["cert_sign"])
|
return CryptBitcoin.verify("%s#%s/%s" % (rules["user_address"], content["cert_auth_type"], name), cert_address, content["cert_sign"])
|
||||||
|
|
||||||
|
|
||||||
# Checks if the content.json content is valid
|
# Checks if the content.json content is valid
|
||||||
# Return: True or False
|
# Return: True or False
|
||||||
def validContent(self, inner_path, content):
|
def validContent(self, inner_path, content):
|
||||||
|
@ -375,8 +368,6 @@ class ContentManager:
|
||||||
|
|
||||||
return True # All good
|
return True # All good
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Verify file validity
|
# Verify file validity
|
||||||
# Return: None = Same as before, False = Invalid, True = Valid
|
# Return: None = Same as before, False = Invalid, True = Valid
|
||||||
def verifyFile(self, inner_path, file, ignore_same = True):
|
def verifyFile(self, inner_path, file, ignore_same = True):
|
||||||
|
@ -443,7 +434,8 @@ class ContentManager:
|
||||||
else:
|
else:
|
||||||
hash_valid = False
|
hash_valid = False
|
||||||
if file_info["size"] != file.tell():
|
if file_info["size"] != file.tell():
|
||||||
self.log.error("%s file size does not match %s <> %s, Hash: %s" % (inner_path, file.tell(), file_info["size"], hash_valid))
|
self.log.error("%s file size does not match %s <> %s, Hash: %s" % (inner_path, file.tell(),
|
||||||
|
file_info["size"], hash_valid))
|
||||||
return False
|
return False
|
||||||
return hash_valid
|
return hash_valid
|
||||||
|
|
||||||
|
@ -460,8 +452,6 @@ class ContentManager:
|
||||||
return file_dir
|
return file_dir
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def testSign():
|
def testSign():
|
||||||
global config
|
global config
|
||||||
from Config import config
|
from Config import config
|
||||||
|
|
Loading…
Reference in a new issue