I have added two many2many fields(to ir.attachment) in project.project model for uploading images and videos. I need to access these files from a CMS website done in wordpress. I have created a controller so that these attachments can be fetched from Odoo like the following:
@http.route('/public/api/video/<int:id>', type='http', method='POST',auth='public', csrf=False)
def get_public_video(xmlid=None, model='ir.attachment', id=None,
field='datas',
filename=None, filename_field='name', unique=None,
mimetype=None,
download=None, data=None, token=None,
access_token=None, **kw):
status, headers, content = request.env['ir.http'].sudo().with_context(
public_api_image=1).binary_content(
xmlid=False, model=model, id=id, field=field, unique=unique,
filename=filename,
filename_field=filename_field, download=download,
mimetype=mimetype, access_token=access_token)
content_base64 = base64.b64decode(content)
headers.append(('Content-Length', len(content_base64)))
response = request.make_response(content_base64, headers)
return response
and a method in ir_http like the following:
def binary_content(self, xmlid=None, model='ir.attachment', id=None, field='datas',unique=False, filename=None, filename_field='name', download=False,
mimetype=None, default_mimetype='application/octet-stream',
access_token=None):
obj = None
if xmlid:
obj = self._xmlid_to_obj(self.env, xmlid)
elif 'public_api_image' in self.env.context:
obj = self.env['ir.attachment'].sudo().browse(int(id))
elif id and model in self.env:
obj = self.env[model].browse(int(id))
if obj and 'website_published' in obj._fields:
if self.env[obj._name].sudo().search([('id', '=', obj.id), ('website_published', '=', True)]):
self = self.sudo()
return super(Http, self).binary_content(
xmlid=xmlid, model=model, id=id, field=field, unique=unique, filename=filename,
filename_field=filename_field, download=download, mimetype=mimetype,
default_mimetype=default_mimetype, access_token=access_token)
This does works but, the problem is that the URL will be something like this: https://example.com/public/api/video/649 and the video will be downloaded instead of displaying. Same is for images except that it will be displayed.
What I am looking for is a way to create a URL so that these attachments will be displayed with an extension like https://example.com/public/api/video/my_video.mp4 . Is it possible in Odoo to get the attachment URLs like this?