Skip to content

Commit

Permalink
Merge pull request #117 from tjmlabs/trim-name
Browse files Browse the repository at this point in the history
Trim upload path to 116 characters
  • Loading branch information
Jonathan-Adly authored Dec 13, 2024
2 parents 75f905d + 252348a commit 50b69f9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
17 changes: 15 additions & 2 deletions web/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,21 @@ def get_upload_path(instance, filename):
# Sanitize email to be safe for use in paths
safe_email = owner_email.replace("@", "_at_")
if settings.DEBUG:
return f"dev-documents/{safe_email}/{filename}" # pragma: no cover
return f"documents/{safe_email}/{filename}"
upload_path = f"dev-documents/{safe_email}/{filename}" # pragma: no cover
else:
upload_path = f"documents/{safe_email}/{filename}"

MAX_UPLOAD_PATH_LENGTH = 116
if len(upload_path) <= MAX_UPLOAD_PATH_LENGTH:
return upload_path

extension = os.path.splitext(upload_path)[1]
trimmed_upload_path = (
upload_path[: MAX_UPLOAD_PATH_LENGTH - len(extension)] + extension
)

logger.info(f"Trimmed upload path to {trimmed_upload_path}")
return trimmed_upload_path


def get_extension_from_mime(mime_type):
Expand Down
31 changes: 31 additions & 0 deletions web/api/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,37 @@ async def test_create_document_pdf_base64_await(async_client, user, collection):
await Document.objects.all().adelete()


async def test_create_document_pdf_base64_long_name_await(
async_client, user, collection
):
# test_docs/ is a directory in the same level as the test.py file - we will use a sample PDF file from there

with open("api/tests/test_docs/sample.pdf", "rb") as f:
# convert the file to base64
base64_string = base64_string = base64.b64encode(f.read()).decode("utf-8")

response = await async_client.post(
"/documents/upsert-document/",
json={
"name": "VeryLongDocumentName" * 10,
"base64": base64_string,
"wait": True,
},
headers={"Authorization": f"Bearer {user.token}"},
)
response_data = response.json()
assert response.status_code == 201
# The URL should now be a pre-signed S3 URL
assert "s3.amazonaws.com" in response_data["url"]
assert response_data["id"] == 1
assert response_data["name"] == "VeryLongDocumentName" * 10
assert response_data["metadata"] == {}
assert response_data["num_pages"] == 1
assert response_data["collection_name"] == "default_collection"
assert response_data["pages"] is None
await Document.objects.all().adelete()


async def test_create_document_pdf_base64_async(async_client, user, collection):
# test_docs/ is a directory in the same level as the test.py file - we will use a sample PDF file from there

Expand Down

0 comments on commit 50b69f9

Please sign in to comment.