-
Notifications
You must be signed in to change notification settings - Fork 8
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
a80ce16
commit 07fd95d
Showing
7 changed files
with
186 additions
and
14 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
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
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
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
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
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
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,4 +1,20 @@ | ||
import os | ||
import hashlib | ||
|
||
def mkdirs_for_file(path: str) -> None: | ||
os.makedirs(os.path.dirname(path), exist_ok=True) | ||
|
||
FILANEME_SAFE_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" | ||
|
||
def did_to_safe_filename(did: str) -> str: | ||
""" | ||
The format is <sha256(did)>_<filtered_did> | ||
The former guarantees uniqueness, and the latter makes it human-recognizeable (ish) | ||
""" | ||
|
||
hexdigest = hashlib.sha256(did.encode()).hexdigest() | ||
filtered = "".join(char for char in did if char in FILANEME_SAFE_CHARS) | ||
|
||
# Truncate to make sure we're staying within PATH_MAX | ||
# (with room to spare, in case the caller appends a file extension) | ||
return f"{hexdigest}_{filtered}"[:200] |