39 lines
1.8 KiB
Python
39 lines
1.8 KiB
Python
import werkzeug,io
|
|
import werkzeug.exceptions
|
|
import werkzeug.utils
|
|
import werkzeug.wrappers
|
|
import werkzeug.wsgi
|
|
from odoo import http
|
|
from odoo.http import request
|
|
from jinja2 import Environment, PackageLoader
|
|
import mammoth
|
|
class preview(http.Controller):
|
|
|
|
@http.route('/web/content/preview/<int:id>', type='http',method=['Get'], auth="user")
|
|
def content_preview(self, id=None):
|
|
ir_attach = request.env['ir.attachment'].sudo()
|
|
attach = ir_attach.browse(id)
|
|
image=['image/jpeg','image/png', 'image/bmp','image/gif', 'image/jpg','image/svg']
|
|
html=['text/html']
|
|
video=['video/mp4','video/webm','video/wmv','video/asf','video/divx','video/rm','video/mkv','video/flv','video/3gp',
|
|
'video/wma','video/wav','video/ape','video/mpeg','video/mpg']
|
|
pdf=[ 'application/pdf']
|
|
txt=['text/plain']
|
|
word=['application/vnd.openxmlformats-officedocument.wordprocessingml.document']
|
|
env = Environment(loader=PackageLoader('odoo.addons.preview_easy', 'static/src/xml'))
|
|
template = env.get_template('1.html')
|
|
if attach:
|
|
if attach.mimetype in image or attach.mimetype in video or attach.mimetype in pdf or attach.mimetype in html or attach.mimetype in txt:
|
|
headers = [('Content-Type', attach.mimetype)]
|
|
return werkzeug.wrappers.Response(attach.raw, status=200, headers=headers)
|
|
elif attach.mimetype in word:
|
|
with io.BytesIO(attach.raw) as file:
|
|
result = mammoth.convert_to_html(file)
|
|
html = result.value
|
|
return template.render(html=html)
|
|
|
|
else:
|
|
return werkzeug.wrappers.Response('This format is not currently supported', status=200, headers=[('Content-Type', 'text/html'),('charset','utf-8')])
|
|
|
|
|