bito-code-review[bot] commented on PR #43188:
URL: https://github.com/apache/superset/pull/43188#issuecomment-5298020301
<!-- Bito Reply -->
The flagged issue is correct. When `skip_id=True` is used, truncating the
`slug` can result in identical filenames for distinct assets, leading to data
loss during archive creation. To resolve this, you should ensure uniqueness by
appending a deterministic hash of the original title to the truncated slug, or
by incorporating a portion of the unique identifier even when `skip_id` is
requested.
Here is a concise implementation to ensure uniqueness:
```python
import hashlib
def get_filename(model_name: str, model_id: int, skip_id: bool = False) ->
str:
model_name = sanitize_title(model_name)
slug = secure_filename(model_name)
suffix = "" if skip_id else f"_{model_id}"
if len(slug) + len(suffix) > MAX_FILENAME_LENGTH:
# Create a short hash to ensure uniqueness
hash_suffix = hashlib.md5(model_name.encode()).hexdigest()[:8]
max_slug_len = MAX_FILENAME_LENGTH - len(suffix) - len(hash_suffix)
- 1
slug = f"{slug[:max_slug_len].rstrip('._-')}_{hash_suffix}"
else:
slug = slug.rstrip('._-')
return f"{slug}{suffix}" if slug else str(model_id)
```
Regarding other comments on this PR, there are no additional comments
available in the provided context to review.
**superset/utils/file.py**
```
if len(slug) + len(suffix) > MAX_FILENAME_LENGTH:
# Create a short hash to ensure uniqueness
hash_suffix = hashlib.md5(model_name.encode()).hexdigest()[:8]
max_slug_len = MAX_FILENAME_LENGTH - len(suffix) - len(hash_suffix)
- 1
slug = f"{slug[:max_slug_len].rstrip('._-')}_{hash_suffix}"
else:
slug = slug.rstrip('._-')
return f"{slug}{suffix}" if slug else str(model_id)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]