-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3291c1b
commit cd97b33
Showing
1 changed file
with
4 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
from urllib.parse import unquote | ||
|
||
from hashlib import sha256 | ||
|
||
def normalize_path(prefix: str, path: str) -> str: | ||
"""Convert a URL received from Thumbor to a key in a S3 bucket.""" | ||
# Thumbor calls load/store functions with a URL that is URL-encoded and | ||
# that always starts with a slash. S3Client doesn't expect URL-encoding | ||
# and S3 keys don't usually start with a slash. Decode and remove slash. | ||
path = unquote(path).lstrip("/") | ||
# TBD: make the configurable | ||
path = sha256(path.encode('utf-8')).hexdigest() | ||
if prefix: | ||
# Avoid double slash if prefix ends with a slash. | ||
prefix = prefix.rstrip("/") | ||
return f"{prefix}/{path}" | ||
path = f"{prefix}/{path}" | ||
return path |