From 78f77c182dc3f176e679289a4f3868cc684d48af Mon Sep 17 00:00:00 2001 From: 20esaua <20esaua@gmail.com> Date: Mon, 4 Jun 2018 09:29:04 -0700 Subject: [PATCH] Update onionrblockapi.py --- onionr/onionrblockapi.py | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/onionr/onionrblockapi.py b/onionr/onionrblockapi.py index f48800a6..6e7691da 100644 --- a/onionr/onionrblockapi.py +++ b/onionr/onionrblockapi.py @@ -529,6 +529,59 @@ class Block: return (None if not file is None else buffer) + def create(data = None, chunksize = 4999000, file = None, type = 'chunk', sign = True): + ''' + Creates a chain of blocks to store larger amounts of data + + The chunksize is set to 4999000 because it provides the least amount of PoW for the most amount of data. + + TODO: Add docs + ''' + + blocks = list() + + # initial datatype checks + if data is None and file is None: + return blocks + elif not (file is None or (isinstance(file, str) and os.path.exists(file))): + return blocks + elif isinstance(file, str): + file = open(file, 'rb') + if isinstance(data, str): + data = str(data) + + if not file is None: + while True: + # read chunksize bytes from the file + content = file.read(chunksize) + + # if it is the end of the file, exit + if not content: + break + + # create block + block = Block() + block.setType(type) + block.setContent(content) + block.setParent((blocks[-1] if len(blocks) != 0 else None)) + hash = block.save(sign = sign) + + # remember the hash in cache + blocks.append(hash) + elif not data is None: + for content in [data[n:n + chunksize] for n in range(0, len(data), chunksize)]: + # create block + block = Block() + block.setType(type) + block.setContent(content) + block.setParent((blocks[-1] if len(blocks) != 0 else None)) + hash = block.save(sign = sign) + + # remember the hash in cache + blocks.append(hash) + + return blocks + def exists(hash): ''' Checks if a block is saved to file or not