Skip to content

Commit a8eabbf

Browse files
[Confluence] Sanitize attachment filenames by replacing invalid filesystem characters with underscores to prevent download errors and warn users of renaming (#1549)
1 parent 705b26f commit a8eabbf

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

atlassian/confluence/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import re
77
import time
8+
import warnings
89
from typing import cast
910

1011
import requests
@@ -1569,6 +1570,15 @@ def download_attachments_from_page(self, page_id, path=None, start=0, limit=50,
15691570
file_obj = io.BytesIO(response)
15701571
downloaded_files[file_name] = file_obj
15711572
else:
1573+
# Sanitize filename if needed
1574+
if re.search(r'[<>:"/\\|?*\x00-\x1F]', file_name):
1575+
sanitized = re.sub(r'[<>:"/\\|?*\x00-\x1F]', '_', file_name)
1576+
warnings.warn(
1577+
f"File name '{file_name}' contained invalid characters and was renamed to '{sanitized}'.",
1578+
UserWarning
1579+
)
1580+
file_name = sanitized
1581+
file_path = os.path.join(path, file_name)
15721582
# Save file to disk
15731583
file_path = os.path.join(path, file_name)
15741584
with open(file_path, "wb") as file:

0 commit comments

Comments
 (0)