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.

148 lines
7.0 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (c) 2017-2019 MuK IT GmbH.
  4. #
  5. # This file is part of MuK Converter
  6. # (see https://mukit.at).
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ###################################################################################
  22. import os
  23. import io
  24. import base64
  25. import shutil
  26. import urllib
  27. import logging
  28. import tempfile
  29. import mimetypes
  30. from subprocess import Popen
  31. from subprocess import PIPE
  32. from subprocess import CalledProcessError
  33. from contextlib import closing
  34. from odoo.tools import config
  35. from odoo.tools.mimetypes import guess_mimetype
  36. from odoo.addons.muk_utils.tools.file import guess_extension
  37. _logger = logging.getLogger(__name__)
  38. UNOCONV_FORMATS = [
  39. "bib", "bmp", "csv", "dbf", "dif", "doc", "doc6", "doc95", "docbook", "docx", "docx7", "emf",
  40. "eps", "fodg", "fodp", "fods", "fodt", "gif", "html", "jpg", "latex", "mediawiki", "met", "odd",
  41. "odg", "odp", "ods", "odt", "ooxml", "otg", "otp", "ots", "ott", "pbm", "pct", "pdb", "pdf", "pgm",
  42. "png", "pot", "potm", "ppm", "pps", "ppt", "pptx", "psw", "pwp", "pxl", "ras", "rtf", "sda", "sdc",
  43. "sdc3", "sdc4", "sdd", "sdd3", "sdd4", "sdw", "sdw3", "sdw4", "slk", "stc", "std", "sti", "stw",
  44. "svg", "svm", "swf", "sxc", "sxd", "sxd3", "sxd5", "sxi", "sxw", "text", "tiff", "txt", "uop", "uos",
  45. "uot", "vor", "vor3", "vor4", "vor5", "wmf", "wps", "xhtml", "xls", "xls5", "xls95", "xlsx", "xlt",
  46. "xlt5", "xlt95", "xpm""bib", "bmp", "csv", "dbf", "dif", "doc", "doc6", "doc95", "docbook", "docx",
  47. "docx7", "emf", "eps", "fodg", "fodp", "fods", "fodt", "gif", "html", "jpg", "latex", "mediawiki",
  48. "met", "odd", "odg", "odp", "ods", "odt", "ooxml", "otg", "otp", "ots", "ott", "pbm", "pct", "pdb",
  49. "pdf", "pgm", "png", "pot", "potm", "ppm", "pps", "ppt", "pptx", "psw", "pwp", "pxl", "ras", "rtf",
  50. "sda", "sdc", "sdc3", "sdc4", "sdd", "sdd3", "sdd4", "sdw", "sdw3", "sdw4", "slk", "stc", "std",
  51. "sti", "stw", "svg", "svm", "swf", "sxc", "sxd", "sxd3", "sxd5", "sxi", "sxw", "text", "tiff",
  52. "txt", "uop", "uos", "uot", "vor", "vor3", "vor4", "vor5", "wmf", "wps", "xhtml", "xls", "xls5",
  53. "xls95", "xlsx", "xlt", "xlt5", "xlt95", "xpm"
  54. ]
  55. UNOCONV_IMPORTS = [
  56. "bmp", "csv", "dbf", "dif", "doc", "docx", "dot", "emf", "eps", "epub", "fodg", "fodp", "fods",
  57. "fodt", "gif", "gnm", "gnumeric", "htm", "html", "jpeg", "jpg", "met", "mml", "odb", "odf", "odg",
  58. "odp", "ods", "odt", "pbm", "pct", "pdb", "pdf", "pgm", "png", "pot", "ppm", "pps", "ppt", "pptx",
  59. "psw", "pxl", "ras", "rtf", "sda", "sdc", "sdd", "sdp", "sdw", "sgl", "slk", "stc", "std", "sti",
  60. "stw", "svg", "svm", "swf", "sxc", "sxd", "sxi", "sxm", "sxw", "tif", "tiff", "txt", "uof", "uop",
  61. "uos", "uot", "vor", "wmf", "wri", "xls", "xlsx", "xlt", "xlw", "xml", "xpm""bmp", "csv", "dbf",
  62. "dif", "doc", "docx", "dot", "emf", "eps", "epub", "fodg", "fodp", "fods", "fodt", "gif", "gnm",
  63. "gnumeric", "htm", "html", "jpeg", "jpg", "met", "mml", "odb", "odf", "odg", "odp", "ods", "odt",
  64. "pbm", "pct", "pdb", "pdf", "pgm", "png", "pot", "ppm", "pps", "ppt", "pptx", "psw", "pxl", "ras",
  65. "rtf", "sda", "sdc", "sdd", "sdp", "sdw", "sgl", "slk", "stc", "std", "sti", "stw", "svg", "svm",
  66. "swf", "sxc", "sxd", "sxi", "sxm", "sxw", "tif", "tiff", "text", "uof", "uop", "uos", "uot", "vor",
  67. "wmf", "wri", "xls", "xlsx", "xlt", "xlw", "xml", "xpm"
  68. ]
  69. class UnoconvConverter(object):
  70. @property
  71. def formats(self):
  72. return UNOCONV_FORMATS
  73. @property
  74. def imports(self):
  75. return UNOCONV_IMPORTS
  76. def environ(self):
  77. env = os.environ.copy()
  78. uno_path = config.get('uno_path', False)
  79. if uno_path:
  80. env['UNO_PATH'] = config['uno_path']
  81. return env
  82. def convert(self, binary, mimetype=None, filename=None, export="binary", doctype="document", format="pdf"):
  83. """ Converts a binary value to the given format.
  84. :param binary: The binary value.
  85. :param mimetype: The mimetype of the binary value.
  86. :param filename: The filename of the binary value.
  87. :param export: The output format (binary, file, base64).
  88. :param doctype: Specify the document type (document, graphics, presentation, spreadsheet).
  89. :param format: Specify the output format for the document.
  90. :return: Returns the output depending on the given format.
  91. :raises ValueError: The file extension could not be determined or the format is invalid.
  92. """
  93. extension = guess_extension(filename=filename, mimetype=mimetype, binary=binary)
  94. if not extension:
  95. raise ValueError("The file extension could not be determined.")
  96. if format not in self.formats:
  97. raise ValueError("Invalid export format.")
  98. if extension not in self.imports:
  99. raise ValueError("Invalid import format.")
  100. tmp_dir = tempfile.mkdtemp()
  101. try:
  102. tmp_wpath = os.path.join(tmp_dir, "tmpfile." + extension)
  103. tmp_ppath = os.path.join(tmp_dir, "tmpfile." + format)
  104. if os.name == 'nt':
  105. tmp_wpath = tmp_wpath.replace("\\", "/")
  106. tmp_ppath = tmp_ppath.replace("\\", "/")
  107. with closing(open(tmp_wpath, 'wb')) as file:
  108. file.write(binary)
  109. shell = True if os.name in ('nt', 'os2') else False
  110. args = ['unoconv', '--format=%s' % format, '--output=%s' % tmp_ppath, tmp_wpath]
  111. process = Popen(args, stdout=PIPE, env=self.environ(), shell=shell)
  112. outs, errs = process.communicate()
  113. return_code = process.wait()
  114. if return_code:
  115. raise CalledProcessError(return_code, args, outs, errs)
  116. with closing(open(tmp_ppath, 'rb')) as file:
  117. if export == 'file':
  118. output = io.BytesIO()
  119. output.write(file.read())
  120. output.close()
  121. return output
  122. elif export == 'base64':
  123. return base64.b64encode(file.read())
  124. else:
  125. return file.read()
  126. except CalledProcessError:
  127. _logger.exception("Error while running unoconv.")
  128. raise
  129. except OSError:
  130. _logger.exception("Error while running unoconv.")
  131. raise
  132. finally:
  133. shutil.rmtree(tmp_dir)
  134. unoconv = UnoconvConverter()