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.

62 lines
2.7 KiB

  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 uuid
  20. import logging
  21. import mimetypes
  22. import werkzeug
  23. from odoo import _, http
  24. from odoo.http import request
  25. from odoo.addons.muk_utils.tools.http import get_response
  26. from odoo.addons.muk_utils.tools.http import make_error_response
  27. _logger = logging.getLogger(__name__)
  28. MIMETPYES = [
  29. 'application/msword', 'application/ms-word', 'application/vnd.ms-word.document.macroEnabled.12',
  30. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.mspowerpoint',
  31. 'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  32. 'application/vnd.ms-powerpoint.presentation.macroEnabled.12'
  33. ]
  34. class MSOfficeParserController(http.Controller):
  35. @http.route('/web/preview/msoffice', auth="user", type='http')
  36. def preview_msoffice(self, url, **kw):
  37. status, headers, content = get_response(url)
  38. if status != 200:
  39. return make_error_response(status, content or _("Unknown Error"))
  40. elif headers['content-type'] not in MIMETPYES:
  41. return werkzeug.exceptions.UnsupportedMediaType()
  42. else:
  43. try:
  44. filename = "%s%s" % (uuid.uuid4(), mimetypes.guess_extension(headers['content-type']))
  45. output = request.env['muk_converter.converter'].convert(filename, content)
  46. return self._make_pdf_response(output, "%s.pdf" % filename)
  47. except Exception:
  48. _logger.exception("Error while convert the file.")
  49. return werkzeug.exceptions.InternalServerError()
  50. def _make_pdf_response(self, file, filename):
  51. headers = [('Content-Type', 'application/pdf'),
  52. ('Content-Disposition', 'attachment; filename="{}";'.format(filename)),
  53. ('Content-Length', len(file))]
  54. return request.make_response(file, headers)