Skip to content

Commit

Permalink
✨ download_attachments: support create_html_directors option
Browse files Browse the repository at this point in the history
  • Loading branch information
huyz committed Apr 20, 2024
1 parent 7ff2740 commit 57e20ad
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/jiraone/reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,7 @@ def download_attachments(
download_path: str = "Downloads",
attach: int = 8,
skip_csv_header: bool = True,
create_html_redirectors: bool = False,
**kwargs,
) -> None:
"""Go through the attachment list CSV file named ``file_name`` and located in the
Expand Down Expand Up @@ -1001,14 +1002,39 @@ def download_attachments(
assumes that the first line represents a header row.
(Default is True, which corresponds to the output of ``def get_attachments_on_project()``)
:param create_html_redirectors: is used when you want to use the downloaded attachments
as part of a website to mirror and serve the attachments separately from the
Jira website. When set to True, an ``index.html`` will be created
for each attachment so that the original Jira Attachment URL, e.g.
https://yourorganization.atlassian.net/rest/api/3/attachment/content/112736 ,
can be more easily rewritten to something like
https://yourmirrorsite.com/jiraone/MYPROJ/attachment/content/112736 .
The ``index.html`` will take care of the HTTP redirect that will point to the
attachment with the original filename.
:param kwargs: Additional keyword argument
**Acceptable options**
* file: index of the column 'Name of file' in the attachment list CSV file.
(Default is 6, which corresponds to the output of
``def get_attachments_on_project()``)
:return: None
"""
HTML_REDIRECTOR_TEMPLATE = """
<!DOCTYPE html>
<html>
<head>
<title>Download File</title>
<meta http-equiv="refresh" content="0; url={path}">
</head>
<body>
<p>If you are not redirected automatically, please click <a href="{path}">here</a> to download the file.</p>
</body>
</html>
"""

file: int = kwargs.get("file", 6)
read = file_reader(
folder=file_folder,
Expand All @@ -1029,7 +1055,8 @@ def download_attachments(
attachment = r[attach]
_file_name = r[file]
if attachment == '' or _file_name == '':
# For example the last line of the attachment list CSV may have: ,,,,Total Size: 0.09 MB,,,,
# For example the last line of the attachment list CSV may have: ,,,,Total Size:
# 0.09 MB,,,,
continue
fetch = LOGIN.get(attachment)
content_id = attachment.split('/')[-1]
Expand All @@ -1043,6 +1070,12 @@ def download_attachments(
content=fetch.content,
mark="file",
)
if create_html_redirectors:
# Create HTML file with a template that
html_content = HTML_REDIRECTOR_TEMPLATE.format(path=_file_name)
# Write the content to file
with open(os.path.join(individual_download_path, 'index.html'), 'w') as html_file:
html_file.write(html_content)
print(
"Attachment downloaded to {}".format(individual_download_path),
"Status code: {}".format(fetch.status_code),
Expand Down

0 comments on commit 57e20ad

Please sign in to comment.