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.

134 lines
5.4 KiB

7 years ago
  1. # -*- coding: utf-8 -*-
  2. ###################################################################################
  3. #
  4. # Copyright (C) 2017 MuK IT GmbH
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ###################################################################################
  20. import os
  21. import sys
  22. import json
  23. import uuid
  24. import base64
  25. import urllib
  26. import urllib2
  27. import logging
  28. import tempfile
  29. import urlparse
  30. import cStringIO
  31. import mimetypes
  32. import collections
  33. import urlparse
  34. import werkzeug.exceptions
  35. from contextlib import closing
  36. from openerp.tools.translate import _
  37. from openerp import tools
  38. from openerp import http
  39. from openerp.http import request
  40. from openerp.http import Response
  41. _logger = logging.getLogger(__name__)
  42. try:
  43. import requests
  44. except ImportError:
  45. _logger.warn('Cannot `import requests`.')
  46. try:
  47. from cachetools import TTLCache
  48. pdf_cache = TTLCache(maxsize=25, ttl=1200)
  49. except ImportError:
  50. _logger.warn('Cannot `import cachetools`.')
  51. try:
  52. import pdfconv
  53. except ImportError:
  54. _logger.warn('Cannot `import pdfconv`.')
  55. class MSOfficeParserController(http.Controller):
  56. @http.route('/web/preview/converter/msoffice', auth="public", type='http')
  57. def convert_msoffice(self, url, export_filename=None, force_compute=False, **kw):
  58. try:
  59. response = pdf_cache[url] if pdf_cache and not force_compute else None
  60. except KeyError:
  61. response = None
  62. if not response:
  63. return self._get_response(url, export_filename)
  64. return response
  65. def _get_response(self, url, export_filename):
  66. if not bool(urlparse.urlparse(url).netloc):
  67. method, params = self._get_route(url)
  68. response = method(**params)
  69. if not response.status_code == 200:
  70. return self._make_error_response(response.status_code,response.description if hasattr(response, 'description') else _("Unknown Error"))
  71. else:
  72. content_type = response.headers['content-type']
  73. data = response.data
  74. else:
  75. try:
  76. response = requests.get(url)
  77. content_type = response.headers['content-type']
  78. data = response.content
  79. print response
  80. print "----------------------------------------------------------------------------------------------"
  81. except requests.exceptions.RequestException as exception:
  82. print self._make_error_response(exception.response.status_code, exception.response.reason or _("Unknown Error"))
  83. print "----------------------------------------------------------------------------------------------"
  84. return self._make_error_response(exception.response.status_code, exception.response.reason or _("Unknown Error"))
  85. try:
  86. par = urlparse.parse_qs(urlparse.urlparse(url).query)
  87. filenamefromurl = "".join(par['filename'])
  88. resp = pdfconv.converter.convert_binary2pdf(data,None,filenamefromurl.encode('utf8'), format='binary')
  89. response = self._make_pdf_response(resp, export_filename or uuid.uuid4())
  90. pdf_cache[url] = response
  91. print "----hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
  92. return response
  93. except KeyError:
  94. return werkzeug.exceptions.UnsupportedMediaType(_("The file couldn't be converted. Unsupported mine type."))
  95. except (ImportError, IOError, OSError) as error:
  96. _logger.error(error)
  97. return werkzeug.exceptions.InternalServerError(_("An error occurred during the process. Please contact your system administrator."))
  98. def _get_route(self, url):
  99. url_parts = url.split('?')
  100. path = url_parts[0]
  101. query_string = url_parts[1] if len(url_parts) > 1 else None
  102. router = request.httprequest.app.get_db_router(request.db).bind('')
  103. match = router.match(path, query_args=query_string)
  104. method = router.match(path, query_args=query_string)[0]
  105. params = dict(urlparse.parse_qsl(query_string))
  106. if len(match) > 1:
  107. params.update(match[1])
  108. return method, params
  109. def _make_error_response(self, status, message):
  110. exception = werkzeug.exceptions.HTTPException()
  111. exception.code = status
  112. exception.description = message
  113. return exception
  114. def _make_pdf_response(self, file, filename):
  115. headers = [('Content-Type', 'application/pdf'),
  116. ('Content-Disposition', 'attachment;filename="{}";'.format(filename)),
  117. ('Content-Length', len(file))]
  118. return request.make_response(file, headers)