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.

48 lines
2.1 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 json
  20. import base64
  21. import logging
  22. from odoo import http
  23. from odoo.http import request
  24. from odoo.tools.misc import str2bool
  25. _logger = logging.getLogger(__name__)
  26. class AttachmentController(http.Controller):
  27. @http.route('/utils/attachment/add', type='http', auth="user", methods=['POST'])
  28. def add_attachment(self, ufile, temporary=False, **kw):
  29. tmp = temporary and str2bool(temporary) or False
  30. name = "Access Attachment: %s" % ufile.filename
  31. attachment = request.env['ir.attachment'].create({
  32. 'name': tmp and "%s (Temporary)" % name or name,
  33. 'datas': base64.b64encode(ufile.read()),
  34. 'datas_fname': ufile.filename,
  35. 'type': 'binary',
  36. 'public': False,
  37. 'temporary': tmp,
  38. })
  39. attachment.generate_access_token()
  40. base_url = request.env['ir.config_parameter'].sudo().get_param('web.base.url')
  41. result = attachment.read(['name', 'datas_fname', 'mimetype', 'checksum', 'access_token'])[0]
  42. result['url'] = '%s/web/content/%s?access_token=%s' % (base_url, attachment.id, attachment.access_token)
  43. return json.dumps(result)