Skip to content
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

Convert all files with CloudConvert #192

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion conversion/indico_conversion/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,14 @@ def submit_attachment_cloudconvert(task, attachment):
}

try:
file_ext = os.path.splitext(attachment.file.filename)[1]
GovernmentPlates marked this conversation as resolved.
Show resolved Hide resolved
job = client.Job.create(payload=job_definition)
upload_task = job['tasks'][0]
export_task = job['tasks'][-1]
assert upload_task['operation'] == 'import/upload'
assert export_task['operation'] == 'export/url'
with attachment.file.open() as fd:
client.Task.upload(upload_task, attachment.file.filename, fd, attachment.file.content_type)
client.Task.upload(upload_task, f'attachment{file_ext}', fd, attachment.file.content_type)
GovernmentPlates marked this conversation as resolved.
Show resolved Hide resolved
# add polling in case we miss a webhook
export_task_id = export_task['id']
cloudconvert_task_cache.set(export_task_id, 'pending')
Expand Down
22 changes: 16 additions & 6 deletions conversion/indico_conversion/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

from flask import flash, g
from flask_pluginengine import render_plugin_template, uses
from wtforms.fields import BooleanField, EmailField, IntegerField, URLField
from markupsafe import Markup
from wtforms.fields import BooleanField, EmailField, IntegerField, TextAreaField, URLField
from wtforms.validators import DataRequired, NumberRange, Optional

from indico.core import signals
Expand Down Expand Up @@ -57,6 +58,10 @@ class SettingsForm(IndicoForm):
cloudconvert_notify_email = EmailField(_('Notification email'), [HiddenUnless('use_cloudconvert',
preserve_data=True)],
description=_('Email to send the notifications to'))
cloudconvert_conversion_notice = TextAreaField(_('PDF conversion notice'),
description=_('A notice that will be shown to end users when '
'converting PDF files in the upload files dialog. '
'You may use basic HTML elements for formatting.'))
valid_extensions = TextListField(_('Extensions'),
filters=[lambda exts: sorted({ext.lower().lstrip('.').strip() for ext in exts})],
description=_('File extensions for which PDF conversion is supported. '
Expand All @@ -81,6 +86,7 @@ class ConversionPlugin(IndicoPlugin):
'cloudconvert_sandbox': False,
'cloudconvert_notify_threshold': None,
'cloudconvert_notify_email': '',
'cloudconvert_conversion_notice': '',
'valid_extensions': ['ppt', 'doc', 'pptx', 'docx', 'odp', 'sxi']}

def init(self):
Expand All @@ -105,10 +111,14 @@ def get_vars_js(self):

def _add_file_form_fields(self, form_cls, **kwargs):
exts = ', '.join(self.settings.get('valid_extensions'))
description = _('If enabled, your files will be converted to PDF if possible. '
GovernmentPlates marked this conversation as resolved.
Show resolved Hide resolved
'The following file types can be converted: {exts}').format(exts=exts)
if self.settings.get('cloudconvert_conversion_notice'):
description = '{}<br>{}'.format(self.settings.get('cloudconvert_conversion_notice'),
_('The following file types can be converted: {exts}').format(exts=exts))
return 'convert_to_pdf', \
BooleanField(_('Convert to PDF'), widget=SwitchWidget(),
description=_('If enabled, your files will be be converted to PDF if possible. '
'The following file types can be converted: {exts}').format(exts=exts),
description=Markup(description),
default=True)

def _add_url_form_fields(self, form_cls, **kwargs):
Expand Down Expand Up @@ -152,7 +162,7 @@ def _attachment_created(self, attachment, **kwargs):
# Prepare for submission (after commit)
if 'convert_attachments' not in g:
g.convert_attachments = set()
g.convert_attachments.add((attachment, attachment.is_protected))
g.convert_attachments.add(attachment)
# Set cache entry to show the pending attachment
pdf_state_cache.set(str(attachment.id), 'pending', timeout=info_ttl)
if not g.get('attachment_conversion_msg_displayed'):
Expand All @@ -165,9 +175,9 @@ def _attachment_created(self, attachment, **kwargs):
'automatically once the conversion is finished.'))

def _after_commit(self, sender, **kwargs):
for attachment, is_protected in g.get('convert_attachments', ()):
for attachment in g.get('convert_attachments', ()):
if attachment.type == AttachmentType.file:
if self.settings.get('use_cloudconvert') and not is_protected:
if self.settings.get('use_cloudconvert'):
submit_attachment_cloudconvert.delay(attachment)
else:
submit_attachment_doconverter.delay(attachment)
Expand Down