Last active
November 1, 2025 12:15
-
-
Save Ichinga-Samuel/73bf5d8cdc835ee2d2188330dc1aaf73 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Memory storage for FastStore. This storage is used to store files in memory. | |
| """ | |
| import asyncio | |
| from logging import getLogger | |
| from .main import FileUpload, FileData | |
| from fastapi import UploadFile | |
| logger = getLogger() | |
| class Memory(FileUpload): | |
| """ | |
| Memory storage for FastAPI. | |
| This storage is used to store files in memory and returned as bytes. | |
| """ | |
| async def upload(self, *, file: UploadFile) -> FileData: | |
| try: | |
| file_object = await file.read() | |
| return FileData(size=file.size, filename=file.filename, content_type=file.content_type, file=file_object, | |
| message=f'{file.filename} saved successfully') | |
| except Exception as err: | |
| logger.error(f'Error uploading file: {err} in {self.__class__.__name__}') | |
| return FileData(status=False, error=str(err), message='Unable to save file') | |
| async def multi_upload(self, *, files: list[UploadFile]) -> list[FileData]: | |
| return list(await asyncio.gather(*[self.upload(file=file) for file in files])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment