def GetFileBlockList(self, vault, file_id, marker=None, limit=None): """Retrieve the list of blocks assigned to the file :param vault: vault to the file belongs to :param fileid: fileid of the file in the Vault to list the blocks for :param marker: blockid within the list to start at :param limit: the maximum number of entries to retrieve :stores: The resulting block list in the file data for the vault. :returns: True on success """ if file_id not in vault.files: raise KeyError( 'file_id must specify a file in the provided Vault.') url = api_v1.get_fileblocks_path(vault.vault_id, file_id) if marker is not None or limit is not None: # add the separator between the URL and the parameters url = url + '?' # Apply the marker if marker is not None: url = '{0:}marker={1:}'.format(url, marker) # Apply a comma if the next item is not none if limit is not None: url = url + ',' # Apply the limit if limit is not None: url = '{0:}limit={1:}'.format(url, limit) self.ReInit(self.sslenabled, url) self.__update_headers() self.__log_request_data(fn='Get File Block List') res = requests.get(self.Uri, headers=self.Headers) self.__log_response_data(res, jsondata=True, fn='Get File Block List') if res.status_code == 200: block_ids = [] for block_id, offset in res.json(): vault.files[file_id].offsets[offset] = block_id block_ids.append(block_id) next_marker = None if 'x-next-batch' in res.headers: parsed_url = urlparse(res.headers['x-next-batch']) qs = parse_qs(parsed_url[4]) next_marker = qs['marker'][0] return (block_ids, next_marker) else: raise RuntimeError( 'Failed to get Block list for File . ' 'Error ({0:}): {1:}'.format(res.status_code, res.text))
def AssignBlocksToFile(self, vault, file_id, block_ids=None): """Assigns the specified block to a file :param vault: vault to containing the file :param file_id: file_id of the file in the vault that the block will be assigned to :param block_ids: optional parameter specify list of Block IDs that have already been assigned to the File object specified by file_id within the Vault in the form [(blockid, offset)] :returns: a list of blocks id that have to be uploaded to complete if all the required blocks have been uploaded the the list will be empty. """ if file_id not in vault.files: raise KeyError('file_id must specify a file in the provided Vault') if block_ids is not None: if len(block_ids) == 0: raise ValueError('block_ids must be iterable') for block_id, offset in block_ids: if str(offset) not in vault.files[file_id].offsets: raise KeyError( 'block offset {0} must be assigned in the File'. format(offset)) if vault.files[file_id].offsets[str(offset)] != block_id: raise ValueError( 'specified offset {0} must match the block {1}'. format(offset, block_id)) else: if len(vault.files[file_id].offsets) == 0: raise ValueError('File must have offsets specified') url = api_v1.get_fileblocks_path(vault.vault_id, file_id) self.ReInit(self.sslenabled, url) self.__update_headers() self.__log_request_data(fn='Assign Blocks To File') """ File Block Assignment Takes a JSON body containing the following: [ [block_id, offset], ... ] """ block_assignment_data = [] if block_ids is not None: block_assignment_data = [(block_id, offset) for block_id, offset in block_ids] else: block_assignment_data = [(block_id, offset) for offset, block_id in vault.files[file_id].offsets.items()] self.log.debug('Assigning blocks to offset:') for block_id, offset in block_assignment_data: self.log.debug('Offset, Block -> {0:}, {1:}'.format(offset, block_id)) res = requests.post(self.Uri, data=json.dumps(block_assignment_data), headers=self.Headers) self.__log_response_data(res, jsondata=True, fn='Assign Blocks To File') if res.status_code == 200: block_list_to_upload = [block_id for block_id in res.json()] return block_list_to_upload else: raise RuntimeError( 'Failed to Assign Blocks to the File. ' 'Error ({0:}): {1:}'.format(res.status_code, res.text))