def convert(self): self.ensure_one() if not self.input_url and not self.input_binary: raise UserError(_("Please choose a file to convert.")) if self.input_url: status, headers, content = get_response(self.input_url) if status != 200: raise ValueError( _("Failed to retrieve the file from the url.")) else: content = base64.b64encode(content) else: content = self.input_binary name = "%s.%s" % (os.path.splitext(self.input_name)[0], self.format) output = self.env['muk_converter.converter'].convert( self.input_name, content, format=self.format) self.write({ 'state': 'download', 'output_name': name, 'output_binary': output }) return { "name": _("Convert File"), 'type': 'ir.actions.act_window', 'res_model': 'muk_converter.convert', 'view_mode': 'form', 'view_type': 'form', 'res_id': self.id, 'views': [(False, 'form')], 'target': 'new', }
def preview_mail(self, url, attachment=None, **kw): status, headers, content = get_response(url) if status != 200: return make_error_response(status, content or _("Unknown Error")) elif headers['content-type'] != 'message/rfc822': return werkzeug.exceptions.UnsupportedMediaType( _("Unparsable message! The file has to be of type: message/rfc822" )) else: message = request.env['mail.thread'].message_parse(content, False) return self._make_parse_response(request.httprequest.url, message, attachment)
def preview_msoffice(self, url, **kw): status, headers, content = get_response(url) if status != 200: return make_error_response(status, content or _("Unknown Error")) elif headers['content-type'] not in MIMETPYES: return werkzeug.exceptions.UnsupportedMediaType() else: try: filename = "%s%s" % (uuid.uuid4(), mimetypes.guess_extension(headers['content-type'])) output = request.env['muk_converter.converter'].convert(filename, content) return self._make_pdf_response(output, "%s.pdf" % filename) except Exception: _logger.exception("Error while convert the file.") return werkzeug.exceptions.InternalServerError()
def convert(self): def export(record, content, filename): name = "%s.%s" % (os.path.splitext(filename)[0], record.format) output = record.env['muk_converter.converter'].convert( filename, content) record.write({ 'state': 'download', 'output_name': name, 'output_binary': base64.b64encode(output) }) return { "name": _("Convert File"), 'type': 'ir.actions.act_window', 'res_model': 'muk_converter.convert', 'view_mode': 'form', 'view_type': 'form', 'res_id': record.id, 'views': [(False, 'form')], 'target': 'new', } record = self[0] if record.input_url: status, headers, content = get_response(record.input_url) if status != 200: raise ValueError("Failed to retrieve the file from the url.") else: extension = mimetypes.guess_extension( headers['content-type'])[1:].strip().lower() if extension not in converter.imports(): raise ValueError("Invalid import format.") else: return export( record, content, record.input_name or "%s.%s" % (uuid.uuid4(), extension)) elif record.input_name and record.input_binary: return export(record, base64.b64decode(record.input_binary), record.input_name) else: raise ValueError( "The conversion requires either a valid url or a filename and a file." )
def preview_rst(self, url, **kw): status, header, content = get_response(url) if status != 200: return make_error_response(status, content or _("Unknown Error")) else: return rst2html(content)