You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
3.7 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. ###################################################################################
  2. #
  3. # Copyright (C) 2017 MuK IT GmbH
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. ###################################################################################
  19. import os
  20. import json
  21. import urllib
  22. import logging
  23. import mimetypes
  24. import collections
  25. import werkzeug
  26. from odoo import _, http
  27. from odoo.http import request, Response
  28. from odoo.addons.muk_utils.tools.http import get_response
  29. from odoo.addons.muk_utils.tools.http import make_error_response
  30. _logger = logging.getLogger(__name__)
  31. class MailParserController(http.Controller):
  32. _Attachment = collections.namedtuple('Attachment', 'name mimetype extension url info')
  33. @http.route('/web/preview/mail', auth="user", type='http')
  34. def preview_mail(self, url, attachment=None, **kw):
  35. status, headers, content = get_response(url)
  36. if status != 200:
  37. return make_error_response(status, content or _("Unknown Error"))
  38. elif headers['content-type'] != 'message/rfc822':
  39. return werkzeug.exceptions.UnsupportedMediaType(
  40. _("Unparsable message! The file has to be of type: message/rfc822"))
  41. else:
  42. if not attachment:
  43. content = content.decode("latin-1").encode("utf8")
  44. message = request.env['mail.thread'].message_parse(content, False)
  45. return self._make_parse_response(request.httprequest.url, message, attachment)
  46. def _set_query_parameter(self, url, param_name, param_value):
  47. scheme, netloc, path, query_string, fragment = urllib.parse.urlsplit(url)
  48. query_params = urllib.parse.parse_qs(query_string)
  49. query_params[param_name] = [param_value]
  50. new_query_string = urllib.parse.urlencode(query_params, doseq=True)
  51. return urllib.parse.urlunsplit((scheme, netloc, path, new_query_string, fragment))
  52. def _make_attachment_response(self, file, filename):
  53. headers = [('Content-Type', mimetypes.guess_type(urllib.request.pathname2url(filename))[0]),
  54. ('Content-Disposition', 'attachment; filename="{}";'.format(filename)),
  55. ('Content-Length', len(file))]
  56. return request.make_response(file, headers)
  57. def _make_parse_response(self, url, message, attachment):
  58. if attachment:
  59. for file in message["attachments"]:
  60. if file.fname and file.fname == attachment:
  61. return self._make_attachment_response(file.content, file.fname)
  62. else:
  63. attachments = []
  64. for file in message["attachments"]:
  65. mimetype = mimetypes.guess_type(urllib.request.pathname2url(file.fname))[0]
  66. extension = os.path.splitext(file.fname)[1]
  67. link = self._set_query_parameter(url, "attachment", file.fname)
  68. attachments.append(self._Attachment(file.fname, mimetype, extension, link, file.info))
  69. message["attachments"] = attachments
  70. return Response(json.dumps(message), content_type='application/json;charset=utf-8', status=200)