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.

71 lines
3.2 KiB

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