Skip to content

Commit

Permalink
new CDN header
Browse files Browse the repository at this point in the history
  • Loading branch information
chamini2 committed Dec 9, 2024
1 parent 1189fbf commit 1f4cb19
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions projects/fal/src/fal/toolkit/file/providers/fal.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,16 @@ def save(

@dataclass
class FalCDNFileRepository(FileRepository):
def _object_lifecycle_headers(
self,
headers: dict[str, str],
object_lifecycle_preference: dict[str, str] | None,
):
if object_lifecycle_preference:
headers["X-Fal-Object-Lifecycle-Preference"] = json.dumps(
object_lifecycle_preference
)

@retry(max_retries=3, base_delay=1, backoff_type="exponential", jitter=True)
def save(
self,
Expand All @@ -548,10 +558,7 @@ def save(
"X-Fal-File-Name": file.file_name,
}

if object_lifecycle_preference:
headers["X-Fal-Object-Lifecycle-Preference"] = json.dumps(
object_lifecycle_preference
)
self._object_lifecycle_headers(headers, object_lifecycle_preference)

url = os.getenv("FAL_CDN_HOST", _FAL_CDN) + "/files/upload"
request = Request(url, headers=headers, method="POST", data=file.data)
Expand Down Expand Up @@ -588,6 +595,17 @@ class InternalFalFileRepositoryV3(FileRepository):
That way it can avoid the need to refresh the token for every upload.
"""

def _object_lifecycle_headers(
self,
headers: dict[str, str],
object_lifecycle_preference: dict[str, str] | None,
):
if object_lifecycle_preference:
if "expiration_duration_seconds" in object_lifecycle_preference:
headers["X-Fal-Object-Lifecycle-Expiration-Duration-Seconds"] = (
object_lifecycle_preference["expiration_duration_seconds"]
)

@retry(max_retries=3, base_delay=1, backoff_type="exponential", jitter=True)
def save(
self, file: FileData, object_lifecycle_preference: dict[str, str] | None
Expand All @@ -599,10 +617,7 @@ def save(
"X-Fal-File-Name": file.file_name,
}

if object_lifecycle_preference:
headers["X-Fal-Object-Lifecycle-Preference"] = json.dumps(
object_lifecycle_preference
)
self._object_lifecycle_headers(headers, object_lifecycle_preference)

url = os.getenv("FAL_CDN_V3_HOST", _FAL_CDN_V3) + "/files/upload"
request = Request(url, headers=headers, method="POST", data=file.data)
Expand Down

0 comments on commit 1f4cb19

Please sign in to comment.