def _get_blob(self, blob_hash: str, length: typing.Optional[int] = None): if self.config.save_blobs: return BlobFile(self.loop, blob_hash, length, self.blob_completed, self.blob_dir) else: if is_valid_blobhash(blob_hash) and os.path.isfile( os.path.join(self.blob_dir, blob_hash)): return BlobFile(self.loop, blob_hash, length, self.blob_completed, self.blob_dir) return BlobBuffer(self.loop, blob_hash, length, self.blob_completed, self.blob_dir)
async def make_sd_blob(self, blob_file_obj: typing.Optional[AbstractBlob] = None, old_sort: typing.Optional[bool] = False, blob_completed_callback: typing.Optional[ typing.Callable[['AbstractBlob'], None]] = None, added_on: float = None, is_mine: bool = False): sd_hash = self.calculate_sd_hash( ) if not old_sort else self.calculate_old_sort_sd_hash() if not old_sort: sd_data = self.as_json() else: sd_data = self.old_sort_json() sd_blob = blob_file_obj or BlobFile(self.loop, sd_hash, len(sd_data), blob_completed_callback, self.blob_dir, added_on, is_mine) if blob_file_obj: blob_file_obj.set_length(len(sd_data)) if not sd_blob.get_is_verified(): writer = sd_blob.get_blob_writer() writer.write(sd_data) await sd_blob.verified.wait() sd_blob.close() return sd_blob
async def test_decode_corrupt_blob_raises_proper_exception_and_deletes_corrupt_file(self): loop = asyncio.get_event_loop() tmp_dir = tempfile.mkdtemp() self.addCleanup(lambda: shutil.rmtree(tmp_dir)) sd_hash = '9313d1807551186126acc3662e74d9de29cede78d4f133349ace846273ef116b9bb86be86c54509eb84840e4b032f6b2' with open(os.path.join(tmp_dir, sd_hash), 'wb') as handle: handle.write(b'doesnt work') blob = BlobFile(loop, sd_hash, blob_directory=tmp_dir) self.assertTrue(blob.file_exists) self.assertIsNotNone(blob.length) with self.assertRaises(InvalidStreamDescriptorError): await StreamDescriptor.from_stream_descriptor_blob( loop, tmp_dir, blob ) self.assertFalse(blob.file_exists) # fixme: this is an emergency PR, plase move this to blob_file tests later self.assertIsNone(blob.length)
async def test_delete_corrupt(self): tmp_dir = tempfile.mkdtemp() self.addCleanup(lambda: shutil.rmtree(tmp_dir)) blob = BlobFile( self.loop, self.blob_hash, len(self.blob_bytes), blob_completed_callback=self.blob_manager.blob_completed, blob_directory=tmp_dir ) writer = blob.get_blob_writer() writer.write(self.blob_bytes) await blob.verified.wait() blob.close() blob = BlobFile( self.loop, self.blob_hash, len(self.blob_bytes), blob_completed_callback=self.blob_manager.blob_completed, blob_directory=tmp_dir ) self.assertTrue(blob.get_is_verified()) with open(blob.file_path, 'wb+') as f: f.write(b'\x00') blob = BlobFile( self.loop, self.blob_hash, len(self.blob_bytes), blob_completed_callback=self.blob_manager.blob_completed, blob_directory=tmp_dir ) self.assertFalse(blob.get_is_verified()) self.assertFalse(os.path.isfile(blob.file_path))