Skip to content

[Confluence] Sanitize attachment filenames to prevent download failures #1549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions atlassian/confluence/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import re
import time
import warnings
from typing import cast

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