work on sitesapi

This commit is contained in:
Kevin Froman 2019-10-31 04:29:03 -05:00
parent bc71e3f693
commit 9ccbcf6c1d
2 changed files with 25 additions and 2 deletions

View File

@ -29,6 +29,7 @@ import onionrexceptions
from onionrutils import stringvalidators from onionrutils import stringvalidators
from utils import safezip from utils import safezip
from onionrutils import mnemonickeys from onionrutils import mnemonickeys
from . import sitefiles
site_api = Blueprint('siteapi', __name__) site_api = Blueprint('siteapi', __name__)
@ -44,9 +45,9 @@ def site(name: str)->Response:
# Now make sure the key is regardless a valid base32 format ed25519 key (readding padding if necessary) # Now make sure the key is regardless a valid base32 format ed25519 key (readding padding if necessary)
if stringvalidators.validate_pub_key(name): if stringvalidators.validate_pub_key(name):
name = unpaddedbase32.repad(name) name = unpaddedbase32.repad(name)
resp = findsite.find_site(name) resp = sitefiles.get_file('index.html')
if stringvalidators.validate_hash(name): elif stringvalidators.validate_hash(name):
try: try:
resp = onionrblockapi.Block(name).bcontent resp = onionrblockapi.Block(name).bcontent
except onionrexceptions.NoDataAvailable: except onionrexceptions.NoDataAvailable:

View File

@ -0,0 +1,22 @@
from typing import Union
import tarfile
import io
from coredb import blockmetadb
from onionrblocks import onionrblockapi
def find_site_gzip(user_id: str)->str:
sites = blockmetadb.get_blocks_by_type('osite')
for site in sites:
if onionrblockapi.Block(site).isSigner(user_id):
return tarfile.open(fileobj=io.BytesIO(site.bcontent), mode='r')
return None
def get_file(user_id, file)->Union[bytes, None]:
ret_data = ""
site = find_site_gzip(user_id)
if site is None: return None
for file in site.getmembers():
if file.name == file:
return site.extractfile(file)
return None