75 lines
4.1 KiB
Python
75 lines
4.1 KiB
Python
import werkzeug, io
|
|
import werkzeug.exceptions
|
|
import werkzeug.utils
|
|
import werkzeug.wrappers
|
|
import werkzeug.wsgi
|
|
from urllib.parse import quote
|
|
from odoo import http
|
|
from odoo.http import request, content_disposition
|
|
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 or word:
|
|
office_view_url = 'http://view.officeapps.live.com/op/view.aspx?src='
|
|
base_url = request.env['ir.config_parameter'].sudo().get_param('web.base.url')
|
|
html = f'{office_view_url}{quote(base_url + "/web/content/office_preview/" + str(attach.id))}'
|
|
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')])
|
|
|
|
@http.route('/web/content/office_preview/<int:id>', type='http', method=['Get'], auth="public")
|
|
def content_office_preview(self, id=None):
|
|
ir_attach = request.env['ir.attachment'].sudo()
|
|
attach = ir_attach.browse(id)
|
|
headers = [
|
|
('Content-Type', attach.mimetype),
|
|
('Content-Disposition', f'inline; filename={quote(attach.name)}'),
|
|
('Content-Length', len(attach.raw)),
|
|
('Connection', 'close'),
|
|
]
|
|
# 花生壳
|
|
# http://view.officeapps.live.com/op/view.aspx?src=https%3A%2F%2Fj2762p6576.vicp.fun%2Fpreview_easy%2Fstatic%2Fdescription%2F123.xlsx
|
|
# http://view.officeapps.live.com/op/view.aspx?src=https%3A%2F%2Fj2762p6576.vicp.fun%2Fweb%2Fcontent%2Foffice_preview%2F863
|
|
return werkzeug.wrappers.Response(attach.raw, status=200, headers=headers)
|
|
|
|
@http.route('/web/document_module/installed', type='json', method=['POST'], auth='none')
|
|
def document_module_installed(self):
|
|
document_module = request.env['ir.module.module'].sudo().search([('name', '=', 'documents')])
|
|
return {'installed': document_module and document_module.state == 'installed'}
|