Skip to content

Commit 50b69f9

Browse files
Merge pull request #117 from tjmlabs/trim-name
Trim upload path to 116 characters
2 parents 75f905d + 252348a commit 50b69f9

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

web/api/models.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,21 @@ def get_upload_path(instance, filename):
3535
# Sanitize email to be safe for use in paths
3636
safe_email = owner_email.replace("@", "_at_")
3737
if settings.DEBUG:
38-
return f"dev-documents/{safe_email}/{filename}" # pragma: no cover
39-
return f"documents/{safe_email}/{filename}"
38+
upload_path = f"dev-documents/{safe_email}/{filename}" # pragma: no cover
39+
else:
40+
upload_path = f"documents/{safe_email}/{filename}"
41+
42+
MAX_UPLOAD_PATH_LENGTH = 116
43+
if len(upload_path) <= MAX_UPLOAD_PATH_LENGTH:
44+
return upload_path
45+
46+
extension = os.path.splitext(upload_path)[1]
47+
trimmed_upload_path = (
48+
upload_path[: MAX_UPLOAD_PATH_LENGTH - len(extension)] + extension
49+
)
50+
51+
logger.info(f"Trimmed upload path to {trimmed_upload_path}")
52+
return trimmed_upload_path
4053

4154

4255
def get_extension_from_mime(mime_type):

web/api/tests/tests.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,37 @@ async def test_create_document_pdf_base64_await(async_client, user, collection):
800800
await Document.objects.all().adelete()
801801

802802

803+
async def test_create_document_pdf_base64_long_name_await(
804+
async_client, user, collection
805+
):
806+
# test_docs/ is a directory in the same level as the test.py file - we will use a sample PDF file from there
807+
808+
with open("api/tests/test_docs/sample.pdf", "rb") as f:
809+
# convert the file to base64
810+
base64_string = base64_string = base64.b64encode(f.read()).decode("utf-8")
811+
812+
response = await async_client.post(
813+
"/documents/upsert-document/",
814+
json={
815+
"name": "VeryLongDocumentName" * 10,
816+
"base64": base64_string,
817+
"wait": True,
818+
},
819+
headers={"Authorization": f"Bearer {user.token}"},
820+
)
821+
response_data = response.json()
822+
assert response.status_code == 201
823+
# The URL should now be a pre-signed S3 URL
824+
assert "s3.amazonaws.com" in response_data["url"]
825+
assert response_data["id"] == 1
826+
assert response_data["name"] == "VeryLongDocumentName" * 10
827+
assert response_data["metadata"] == {}
828+
assert response_data["num_pages"] == 1
829+
assert response_data["collection_name"] == "default_collection"
830+
assert response_data["pages"] is None
831+
await Document.objects.all().adelete()
832+
833+
803834
async def test_create_document_pdf_base64_async(async_client, user, collection):
804835
# test_docs/ is a directory in the same level as the test.py file - we will use a sample PDF file from there
805836

0 commit comments

Comments
 (0)