Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions backend/src/baserow/contrib/database/airtable/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from io import BytesIO, IOBase
from typing import Dict, List, Optional, Tuple, Union

from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.core.files.storage import Storage
Expand Down Expand Up @@ -129,6 +130,36 @@ def download_airtable_file(
f"File {name} could not be downloaded (HTTP {response.status_code}).",
)

file_size_bytes = None

# Use Content-Range header for partial content
if response.status_code == HTTPStatus.PARTIAL_CONTENT:
content_range = response.headers.get("Content-Range", "")
try:
total_size = content_range.split("/")[-1]
file_size_bytes = int(total_size)
except (TypeError, ValueError):
raise FileDownloadFailed(f"Could not determine the size of file {name}.")

# Use Content-Length if the status code is not partial content
if file_size_bytes is None:
content_length = response.headers.get("Content-Length")
if content_length is not None:
try:
file_size_bytes = int(content_length)
except (TypeError, ValueError):
file_size_bytes = None

# If we cannot determine the size from headers, treat this as a download failure
if file_size_bytes is None:
raise FileDownloadFailed(f"Could not determine the size of file {name}.")

# Prevent upload to Baserow failures by excluding oversized files
max_size_bytes = settings.BASEROW_FILE_UPLOAD_SIZE_LIMIT_MB
if file_size_bytes > max_size_bytes:
raise FileDownloadFailed(
f"File {name} exceeds the size limit of {settings.BASEROW_FILE_UPLOAD_SIZE_LIMIT_MB} bytes."
)
return response


Expand Down
Loading
Loading