File size: 1,107 Bytes
56d74b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# coding=utf-8
"""
This is example to attach file with mimetype

"""
import logging

# https://pypi.org/project/python-magic/
import magic
from atlassian import Confluence

logging.basicConfig(level=logging.DEBUG)

confluence = Confluence(
    url="http://localhost:8090",
    username="admin",
    password="admin",
)


def attach_file(page_title, file_location, file_name, mime_type, space):
    page_id = confluence.get_page_by_title(space=space, title=page_title).get("id") or None
    if page_id is None:
        return 1
    try:
        confluence.attach_file(
            filename=file_location, name=file_name, content_type=mime_type, page_id=page_id, space=space
        )
    except Exception:
        return 1
    return 0


mime_type = magic.Magic(mime=True)

file_location_with_page = "~/test/test_file.pdf"
file_name = "So excited overview of report.pdf"
title = "The page with attachments"
space = "TST"

content_type = magic.from_file(file_name, mime=True)
attach_file(
    file_location=file_location_with_page, file_name=file_name, mime_type=content_type, page_title=title, space=space
)