-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbase_file.py
36 lines (30 loc) · 1.05 KB
/
base_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from uuid import UUID
from sqlalchemy.dialects.postgresql import UUID as SA_UUID
from sqlmodel import Field, Column, ForeignKey
from fastapi_app.utils.s3_client import s3_client
from .base import SQLModelBase, PydanticModelBase
class BaseFile(SQLModelBase):
file_key: str = Field(index=True)
content_type: str = Field()
account_id: UUID = Field(
sa_column=Column(
SA_UUID(as_uuid=True),
ForeignKey("accounts.id", ondelete="CASCADE"),
nullable=False,
index=True,
),
)
@classmethod
def bucket_name(cls) -> str:
raise NotImplementedError("Subclasses must implement this property")
def image_url(self) -> str:
signed_url = s3_client.generate_presigned_url(
"get_object",
Params={"Bucket": self.bucket_name(), "Key": self.file_key},
ExpiresIn=3600, # URL valid for 1 hour (3600 seconds)
)
return signed_url
class BaseFileCreate(PydanticModelBase):
file_key: str
content_type: str
account_id: UUID