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.

68 lines
3.1 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (C) 2018 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 logging
  20. from werkzeug import utils
  21. from werkzeug import wrappers
  22. from odoo import http
  23. from odoo.http import request
  24. _logger = logging.getLogger(__name__)
  25. def lobject_content(xmlid=None, model=None, id=None, field='content', unique=False,
  26. filename=None, filename_field='content_fname', download=False,
  27. mimetype=None, default_mimetype='application/octet-stream', env=None):
  28. return request.registry['ir.http'].lobject_content(
  29. xmlid=xmlid, model=model, id=id, field=field, unique=unique,
  30. filename=filename, filename_field=filename_field, download=download,
  31. mimetype=mimetype, default_mimetype=default_mimetype, env=env)
  32. class LargeObjectController(http.Controller):
  33. @http.route([
  34. '/web/lobject',
  35. '/web/lobject/<string:xmlid>',
  36. '/web/lobject/<string:xmlid>/<string:filename>',
  37. '/web/lobject/<int:id>',
  38. '/web/lobject/<int:id>/<string:filename>',
  39. '/web/lobject/<int:id>-<string:unique>',
  40. '/web/lobject/<int:id>-<string:unique>/<string:filename>',
  41. '/web/lobject/<string:model>/<int:id>/<string:field>',
  42. '/web/lobject/<string:model>/<int:id>/<string:field>/<string:filename>'
  43. ], type='http', auth="public")
  44. def content_lobject(self, xmlid=None, model=None, id=None, field='content',
  45. filename=None, filename_field='content_fname', unique=None,
  46. mimetype=None, download=None, data=None, token=None):
  47. status, headers, content = lobject_content(
  48. xmlid=xmlid, model=model, id=id, field=field, unique=unique, filename=filename,
  49. filename_field=filename_field, download=download, mimetype=mimetype)
  50. if status == 304:
  51. response = wrappers.Response(status=status, headers=headers)
  52. elif status == 301:
  53. return utils.redirect(content, code=301)
  54. elif status != 200:
  55. response = request.not_found()
  56. else:
  57. headers.append(('Content-Length', content.seek(0, 2)))
  58. content.seek(0, 0)
  59. response = wrappers.Response(content, headers=headers, status=status, direct_passthrough=True)
  60. if token:
  61. response.set_cookie('fileToken', token)
  62. return response