def _process(self): from indico_conversion.plugin import ConversionPlugin try: payload = secure_serializer.loads(request.form['directory'], salt='pdf-conversion') except BadData: ConversionPlugin.logger.exception('Received invalid payload (%s)', request.form['directory']) return jsonify(success=False) attachment = Attachment.get(payload['attachment_id']) if not attachment or attachment.is_deleted or attachment.folder.is_deleted: ConversionPlugin.logger.info('Attachment has been deleted: %s', attachment) return jsonify(success=True) elif request.form['status'] != '1': ConversionPlugin.logger.error('Received invalid status %s for %s', request.form['status'], attachment) return jsonify(success=False) name, ext = os.path.splitext(attachment.file.filename) title = get_pdf_title(attachment) pdf_attachment = Attachment(folder=attachment.folder, user=attachment.user, title=title, description=attachment.description, type=AttachmentType.file, protection_mode=attachment.protection_mode, acl=attachment.acl) # TODO: remove first case when Conversion Server is fully on new version if 'content' in request.form: # handling of legacy API data = BytesIO(base64.decodestring(request.form['content'])) else: filepdf = request.files['content'] data = filepdf.stream.read() pdf_attachment.file = AttachmentFile(user=attachment.file.user, filename='{}.pdf'.format(name), content_type='application/pdf') pdf_attachment.file.save(data) db.session.add(pdf_attachment) db.session.flush() cache.set(unicode(attachment.id), 'finished', timedelta(minutes=15)) ConversionPlugin.logger.info('Added PDF attachment %s for %s', pdf_attachment, attachment) signals.attachments.attachment_created.send(pdf_attachment, user=None) return jsonify(success=True)
def _attachment_created(self, attachment, **kwargs): if not g.get('convert_attachments_pdf' ) or attachment.type != AttachmentType.file: return ext = os.path.splitext(attachment.file.filename)[1].lstrip('.') if ext not in self.settings.get('valid_extensions'): return # Prepare for submission (after commit) if 'convert_attachments_ids' not in g: g.convert_attachments_ids = set() g.convert_attachments_ids.add(attachment.id) # Set cache entry to show the pending attachment cache.set(unicode(attachment.id), 'pending', info_ttl) if not g.get('attachment_conversion_msg_displayed'): g.attachment_conversion_msg_displayed = True flash( _('Your file(s) have been sent to the conversion system. The PDF file(s) will be attached ' 'automatically once the conversion finished.').format( file=attachment.file.filename))