Limit length of filename

S3 has a limit of 2kb for metadata:

> the user-defined metadata is limited to 2 KB in size. The size of
> user-defined metadata is measured by taking the sum of the number of
> bytes in the UTF-8 encoding of each key and value.

– https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#object-metadata

This means we have a limit of 1870 bytes for the filename:
```python
encoded = 'notification_count50000template_id665d26e7-ceac-4cc5-82ed-63d773d21561validTrueoriginal_file_name'.encode('utf-8')
sys.getsizeof(b)
>>> 130
2000-130
>>> 1870
```

Or, in other words, ~918 characters:
```python
sys.getsizeof(('ü'*918).encode('utf-8'))
>>> 1869
```
This commit is contained in:
Chris Hill-Scott
2018-04-30 10:46:39 +01:00
parent 735d5f0a29
commit bc8bc727f3
3 changed files with 51 additions and 1 deletions

View File

@@ -46,6 +46,7 @@ from app.utils import (
get_errors_for_csv,
get_help_argument,
get_template,
unicode_truncate,
user_has_permissions,
)
@@ -555,7 +556,10 @@ def _check_messages(service_id, template_id, upload_id, preview_row, letters_as_
notification_count=len(recipients),
template_id=str(template_id),
valid=True,
original_file_name=request.args.get('original_file_name'),
original_file_name=unicode_truncate(
request.args.get('original_file_name', ''),
1872
),
)
else:
session['file_uploads'].pop(upload_id)