Refactor storage URL retrieval to use get_uploaded_file_url.
Replaced calls to get_file_url with get_uploaded_file_url where appropriate. Added the get_uploaded_file_url method to the StorageProvider class and its implementations for more specific URL generation. Updated corresponding tests and API routes to reflect this change.
This commit is contained in:
@@ -13,8 +13,6 @@ from app.schemas.presigned_urls import PresignedUrlResponse, PresignedUrlRequest
|
|||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/presigned-url", response_model=PresignedUrlResponse, operation_id="generate_presigned_url")
|
@router.post("/presigned-url", response_model=PresignedUrlResponse, operation_id="generate_presigned_url")
|
||||||
async def generate_presigned_url(
|
async def generate_presigned_url(
|
||||||
request: PresignedUrlRequest,
|
request: PresignedUrlRequest,
|
||||||
@@ -98,7 +96,7 @@ async def upload_file(
|
|||||||
await storage.save_file(file, destination)
|
await storage.save_file(file, destination)
|
||||||
|
|
||||||
# Return the file URL
|
# Return the file URL
|
||||||
return {"file_url": storage.get_file_url(destination)}
|
return {"file_url": storage.get_uploaded_file_url(destination)}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ from app.core.config import settings
|
|||||||
class StorageProvider(ABC):
|
class StorageProvider(ABC):
|
||||||
"""Base abstract class for storage providers."""
|
"""Base abstract class for storage providers."""
|
||||||
upload_folder: Path
|
upload_folder: Path
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def save_file(self, file: UploadFile, destination: str) -> str:
|
async def save_file(self, file: UploadFile, destination: str) -> str:
|
||||||
"""Save a file to storage and return the relative path."""
|
"""Save a file to storage and return the relative path."""
|
||||||
@@ -28,6 +29,11 @@ class StorageProvider(ABC):
|
|||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def get_uploaded_file_url(self, file_path: str) -> str:
|
||||||
|
"""Get the URL for accessing a file."""
|
||||||
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_file_url(self, file_path: str) -> str:
|
def get_file_url(self, file_path: str) -> str:
|
||||||
"""Get the URL for accessing a file."""
|
"""Get the URL for accessing a file."""
|
||||||
@@ -74,10 +80,14 @@ class LocalStorageProvider(StorageProvider):
|
|||||||
upload_url = f"{settings.API_VERSION_STR}/uploads/{token}"
|
upload_url = f"{settings.API_VERSION_STR}/uploads/{token}"
|
||||||
|
|
||||||
# The file URL is where the file will be accessible after upload
|
# The file URL is where the file will be accessible after upload
|
||||||
file_url = f"{self.files_url_path}/{file_path}"
|
file_url = f"{self.upload_folder}/{file_path}"
|
||||||
|
|
||||||
return upload_url, file_url
|
return upload_url, file_url
|
||||||
|
|
||||||
|
def get_uploaded_file_url(self, file_path: str) -> str:
|
||||||
|
"""Get the URL for accessing a file."""
|
||||||
|
return f"{self.upload_folder}/{file_path}"
|
||||||
|
|
||||||
def get_file_url(self, file_path: str) -> str:
|
def get_file_url(self, file_path: str) -> str:
|
||||||
"""Get the URL for accessing a file."""
|
"""Get the URL for accessing a file."""
|
||||||
return f"{self.files_url_path}/{file_path}"
|
return f"{self.files_url_path}/{file_path}"
|
||||||
|
|||||||
@@ -70,6 +70,6 @@ def test_get_file_url(test_storage):
|
|||||||
"""Test getting a file URL."""
|
"""Test getting a file URL."""
|
||||||
file_path = "images/test.jpg"
|
file_path = "images/test.jpg"
|
||||||
|
|
||||||
url = test_storage.get_file_url(file_path)
|
url = test_storage.get_uploaded_file_url(file_path)
|
||||||
|
|
||||||
assert url == f"/test-files/{file_path}"
|
assert url == f"/test-files/{file_path}"
|
||||||
|
|||||||
Reference in New Issue
Block a user