Files
gzth/preview_easy/controllers/preview.py
T
2024-10-20 10:36:26 +08:00

60 lines
3.2 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="public")
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']
excel = ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel']
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)
elif attach.mimetype in excel:
# base_url = request.env['ir.config_parameter'].sudo().get_param('web.base.url')
# env.cr.dbname
# /web/content/ir.attachment/2/datas?download = true 这个是下载
url_login = "https://pengyuthon.com/web/content/ir.attachment/" + str(id) + '/datas'
url = "https://pengyuthon.com/web/content/preview/" + str(id) + "/"
html = "https://view.officeapps.live.com/op/view.aspx?src=" + url
# html = "https://view.officeapps.live.com/op/view.aspx?src=" + "http://youthpower.ltd/blogger_lib_admin/test.xlsx"
return werkzeug.wrappers.Response(f"""
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
</head>
<body onLoad="parent.location='{html}'">
</body>
</html>
""", status=200, headers=[('Content-Type', 'text/html'), ('charset', 'utf-8')])
else:
return werkzeug.wrappers.Response('This format is not currently supported', status=200, headers=[('Content-Type', 'text/html'),('charset','utf-8')])