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.

78 lines
3.6 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
  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. message = request.env['mail.thread'].message_parse(content, False)
  43. return self._make_parse_response(request.httprequest.url, message, attachment)
  44. def _set_query_parameter(self, url, param_name, param_value):
  45. scheme, netloc, path, query_string, fragment = urllib.parse.urlsplit(url)
  46. query_params = urllib.parse.parse_qs(query_string)
  47. query_params[param_name] = [param_value]
  48. new_query_string = urllib.parse.urlencode(query_params, doseq=True)
  49. return urllib.parse.urlunsplit((scheme, netloc, path, new_query_string, fragment))
  50. def _make_attachment_response(self, file, filename):
  51. headers = [('Content-Type', mimetypes.guess_type(urllib.request.pathname2url(filename))[0]),
  52. ('Content-Disposition', 'attachment; filename="{}";'.format(filename)),
  53. ('Content-Length', len(file))]
  54. return request.make_response(file, headers)
  55. def _make_parse_response(self, url, message, attachment):
  56. if attachment:
  57. for file in message["attachments"]:
  58. if file.fname and file.fname == attachment:
  59. return self._make_attachment_response(file.content, file.fname)
  60. else:
  61. attachments = []
  62. for file in message["attachments"]:
  63. mimetype = mimetypes.guess_type(urllib.request.pathname2url(file.fname))[0]
  64. extension = os.path.splitext(file.fname)[1]
  65. link = self._set_query_parameter(url, "attachment", file.fname)
  66. attachments.append(self._Attachment(file.fname, mimetype, extension, link, file.info))
  67. message["attachments"] = attachments
  68. return Response(json.dumps(message), content_type='application/json;charset=utf-8', status=200)