using temp directory for downloads

This commit is contained in:
Stefan Tobler
2024-01-28 18:59:56 -05:00
committed by psychedelicious
parent d0f3571e59
commit f15aa562c2
4 changed files with 37 additions and 20 deletions

View File

@ -397,7 +397,7 @@ async def download_images_from_list(
raise HTTPException(status_code=400, detail="No images or board id specified.")
bulk_download_item_id: str = uuid_string() if board_id is None else board_id
board_name: str = (
"" if board_id is None else ApiDependencies.invoker.services.board_records.get(board_id).board_name
"" if board_id is None else ApiDependencies.invoker.services.bulk_download.get_clean_board_name(board_id)
)
# Type narrowing handled above ^, we know that image_names is not None, trying to keep null checks at the boundaries

View File

@ -45,7 +45,7 @@ class BulkDownloadBase(ABC):
"""
@abstractmethod
def get_board_name(self, board_id: str) -> str:
def get_clean_board_name(self, board_id: str) -> str:
"""
Get the name of the board.

View File

@ -1,4 +1,5 @@
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Optional, Union
from zipfile import ZipFile
@ -19,6 +20,7 @@ from .bulk_download_base import BulkDownloadBase
class BulkDownloadService(BulkDownloadBase):
__output_folder: Path
__temp_directory: TemporaryDirectory
__bulk_downloads_folder: Path
__event_bus: EventServiceBase
__invoker: Invoker
@ -35,7 +37,8 @@ class BulkDownloadService(BulkDownloadBase):
Initialize the downloader object.
"""
self.__output_folder: Path = output_folder if isinstance(output_folder, Path) else Path(output_folder)
self.__bulk_downloads_folder = self.__output_folder / "bulk_downloads"
self.__temp_directory = TemporaryDirectory(dir=self.__output_folder)
self.__bulk_downloads_folder = Path(self.__temp_directory.name) / "bulk_downloads"
self.__bulk_downloads_folder.mkdir(parents=True, exist_ok=True)
def handler(self, image_names: list[str], board_id: Optional[str], bulk_download_item_id: Optional[str]) -> None:
@ -57,8 +60,7 @@ class BulkDownloadService(BulkDownloadBase):
image_dtos: list[ImageDTO] = []
if board_id:
board_name = self.get_board_name(board_id)
board_name = self._clean_string_to_path_safe(board_name)
board_name = self.get_clean_board_name(board_id)
# -1 is the default value for limit, which means no limit, is_intermediate only gives us completed images
image_dtos = self.__invoker.services.images.get_many(
@ -80,11 +82,11 @@ class BulkDownloadService(BulkDownloadBase):
self.__invoker.services.logger.error("Problem bulk downloading images.")
raise e
def get_board_name(self, board_id: str) -> str:
def get_clean_board_name(self, board_id: str) -> str:
if board_id == "none":
return "Uncategorized"
return self.__invoker.services.board_records.get(board_id).board_name
return self._clean_string_to_path_safe(self.__invoker.services.board_records.get(board_id).board_name)
def _create_zip_file(self, image_dtos: list[ImageDTO], bulk_download_item_id: str) -> str:
"""
@ -145,11 +147,7 @@ class BulkDownloadService(BulkDownloadBase):
def stop(self, *args, **kwargs):
"""Stop the bulk download service and delete the files in the bulk download folder."""
# Get all the files in the bulk downloads folder, only .zip files
files = self.__bulk_downloads_folder.glob("*.zip")
# Delete all the files
for file in files:
file.unlink()
self.__temp_directory.cleanup()
def delete(self, bulk_download_item_name: str) -> None:
"""