Skip to content

Commit

Permalink
Fix an issue with Mermaid diagram background and scale
Browse files Browse the repository at this point in the history
  • Loading branch information
hunyadi committed Nov 22, 2024
1 parent 582fd94 commit e9b976b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
38 changes: 28 additions & 10 deletions md2conf/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,32 +178,47 @@ def get_attachment_by_name(
def upload_attachment(
self,
page_id: str,
attachment_path: Path,
attachment_name: str,
*,
attachment_path: Optional[Path] = None,
raw_data: Optional[bytes] = None,
content_type: Optional[str] = None,
comment: Optional[str] = None,
*,
space_key: Optional[str] = None,
force: bool = False,
) -> None:
content_type = mimetypes.guess_type(attachment_path, strict=True)[0]

if not raw_data and not attachment_path.is_file():
if attachment_path is None and raw_data is None:
raise ConfluenceError("required: `attachment_path` or `raw_data`")

if attachment_path is not None and raw_data is not None:
raise ConfluenceError("expected: either `attachment_path` or `raw_data`")

if content_type is None:
if attachment_path is not None:
name = str(attachment_path)
else:
name = attachment_name
content_type, _ = mimetypes.guess_type(name, strict=True)

if attachment_path is not None and not attachment_path.is_file():
raise ConfluenceError(f"file not found: {attachment_path}")

try:
attachment = self.get_attachment_by_name(
page_id, attachment_name, space_key=space_key
)

if not raw_data:
if attachment_path is not None:
if not force and attachment.file_size == attachment_path.stat().st_size:
LOGGER.info("Up-to-date attachment: %s", attachment_name)
return
else:
elif raw_data is not None:
if not force and attachment.file_size == len(raw_data):
LOGGER.info("Up-to-date embedded image: %s", attachment_name)
return
else:
raise NotImplementedError("never occurs")

id = removeprefix(attachment.id, "att")
path = f"/content/{page_id}/child/attachment/{id}/data"
Expand All @@ -213,7 +228,7 @@ def upload_attachment(

url = self._build_url(path)

if not raw_data:
if attachment_path is not None:
with open(attachment_path, "rb") as attachment_file:
file_to_upload = {
"comment": comment,
Expand All @@ -230,24 +245,27 @@ def upload_attachment(
files=file_to_upload, # type: ignore
headers={"X-Atlassian-Token": "no-check"},
)
else:
elif raw_data is not None:
LOGGER.info("Uploading raw data: %s", attachment_name)

raw_file = io.BytesIO(raw_data)
raw_file.name = attachment_name
file_to_upload = {
"comment": comment,
"file": (
attachment_name, # will truncate path component
io.BytesIO(raw_data), # type: ignore
raw_file, # type: ignore
content_type,
{"Expires": "0"},
),
}

response = self.session.post(
url,
files=file_to_upload, # type: ignore
headers={"X-Atlassian-Token": "no-check"},
)
else:
raise NotImplementedError("never occurs")

response.raise_for_status()
data = response.json()
Expand Down
3 changes: 1 addition & 2 deletions md2conf/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,13 @@ def _update_document(self, document: ConfluenceDocument, base_path: Path) -> Non
for image in document.images:
self.api.upload_attachment(
document.id.page_id,
base_path / image,
attachment_name(image),
attachment_path=base_path / image,
)

for name, data in document.embedded_images.items():
self.api.upload_attachment(
document.id.page_id,
Path("EMB") / name,
name,
raw_data=data,
)
Expand Down
4 changes: 4 additions & 0 deletions md2conf/mermaid.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ def render(source: str, output_format: Literal["png", "svg"] = "png") -> bytes:
filename,
"--outputFormat",
output_format,
"--backgroundColor",
"transparent",
"--scale",
"2",
]
root = os.path.dirname(__file__)
if is_docker():
Expand Down

0 comments on commit e9b976b

Please sign in to comment.