async def _download_and_store_entity(
        self,
        entity: FileEntity,
        stream: AsyncIterator[bytes],
        max_size: int,
        is_ctti: bool,
    ) -> FileEntity:
        """Download entity from source and store in persistent storage."""
        file_uuid = uuid4()
        safe_filename = self._safe_filename(entity.name)
        temp_path = os.path.join(self.base_temp_dir, f"{file_uuid}-{safe_filename}")

        try:
            downloaded_size = await self._download_file_stream(entity, stream, temp_path, max_size)

            if entity.should_skip:
                return entity

            # Calculate checksum and update entity
            await self._update_entity_metadata(entity, temp_path, file_uuid, downloaded_size)

            # Store in persistent storage for future use
            await self._store_entity_in_storage(entity, temp_path, is_ctti)

            # IMPORTANT: Keep temp file for processing - chunker will clean it up

        except Exception as e:
            logger.error(
                f"[Entity({entity.entity_id})] Error processing file {entity.name}: {str(e)}"
            )
            # Clean up partial file if it exists
            if os.path.exists(temp_path):
                os.remove(temp_path)
            raise e

        return entity
Download entity from source and store in persistent storage.
backend/airweave/platform/file_handling/file_manager.py