|
| 1 | +"""Download a file from provider storage use case.""" |
| 2 | +# pylint: disable=duplicate-code |
| 3 | +import logging |
| 4 | +from django.contrib.auth.models import AbstractUser |
| 5 | +from api.access_policies.providers import ProviderAccessPolicy |
| 6 | +from api.repositories.providers import ProviderRepository |
| 7 | +from api.services.file_storage import FileStorage, WorkingDir |
| 8 | +from api.repositories.functions import FunctionRepository |
| 9 | +from api.domain.exceptions.not_found_error import NotFoundError |
| 10 | + |
| 11 | +from api.models import RUN_PROGRAM_PERMISSION |
| 12 | + |
| 13 | +logger = logging.getLogger("gateway.use_cases.files") |
| 14 | + |
| 15 | + |
| 16 | +class FilesProviderDownloadUseCase: |
| 17 | + """ |
| 18 | + Download a file from provider storage use case. |
| 19 | + """ |
| 20 | + |
| 21 | + function_repository = FunctionRepository() |
| 22 | + provider_repository = ProviderRepository() |
| 23 | + working_dir = WorkingDir.PROVIDER_STORAGE |
| 24 | + |
| 25 | + def execute( |
| 26 | + self, |
| 27 | + user: AbstractUser, |
| 28 | + provider_name: str, |
| 29 | + function_title: str, |
| 30 | + requested_file_name: str, |
| 31 | + ): |
| 32 | + """ |
| 33 | + Download a file from provider storage. |
| 34 | + """ |
| 35 | + |
| 36 | + provider = self.provider_repository.get_provider_by_name(name=provider_name) |
| 37 | + if provider is None or not ProviderAccessPolicy.can_access( |
| 38 | + user=user, provider=provider |
| 39 | + ): |
| 40 | + raise NotFoundError(f"Provider {provider_name} doesn't exist.") |
| 41 | + |
| 42 | + function = self.function_repository.get_function_by_permission( |
| 43 | + user=user, |
| 44 | + permission_name=RUN_PROGRAM_PERMISSION, |
| 45 | + function_title=function_title, |
| 46 | + provider_name=provider_name, |
| 47 | + ) |
| 48 | + |
| 49 | + if not function: |
| 50 | + raise NotFoundError( |
| 51 | + f"Qiskit Function {provider_name}/{function_title} doesn't exist." |
| 52 | + ) |
| 53 | + |
| 54 | + file_storage = FileStorage( |
| 55 | + username=user.username, |
| 56 | + working_dir=self.working_dir, |
| 57 | + function_title=function_title, |
| 58 | + provider_name=provider_name, |
| 59 | + ) |
| 60 | + result = file_storage.get_file(file_name=requested_file_name) |
| 61 | + |
| 62 | + if result is None: |
| 63 | + raise NotFoundError("Requested file was not found.") |
| 64 | + |
| 65 | + return result |
0 commit comments