preview microsoft file

This commit is contained in:
XuanHuyen
2025-04-03 15:20:36 +07:00
parent 00c63dde2d
commit 857155ba7a
7 changed files with 136 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
from . import controllers
+24
View File
@@ -0,0 +1,24 @@
{
"name": "Custom MS Office Preview",
"version": "1.0",
"summary": "Preview office files using Microsoft Online Viewer",
"description": """
This module allows users to preview Microsoft Office files (such as Word, Excel, and PowerPoint)
directly within the Odoo interface using the Microsoft Online Viewer.
It extends the attachment model to recognize Office file types and provides a seamless viewing experience
by embedding the Office Viewer in the file preview interface.
""",
"depends": ["web"],
"data": [
],
"assets": {
"web.assets_backend": [
"custom_ms_office_preview/static/src/xml/file_viewer.xml",
"custom_ms_office_preview/static/src/xml/mail_attachment_vew.xml",
"custom_ms_office_preview/static/src/js/attachment_model_patch.js",
],
},
'author': 'Quang Trinh',
"installable": True,
"application": False,
}
@@ -0,0 +1 @@
from . import main
@@ -0,0 +1,33 @@
import logging
from werkzeug.exceptions import NotFound
from odoo import http
from odoo.http import request
_logger = logging.getLogger(__name__)
class AttachmentPreviewController(http.Controller):
@http.route("/preview-url/<int:attachment_id>", type="http", auth="public", cors="*")
def public_guest_attachment(self, attachment_id, download=False, **kwargs):
try:
# Todo: Implement a better way to handle guest_token
attachment_sudo = request.env["ir.attachment"].sudo().browse(attachment_id)
_logger.debug(f"✅ Guest env applied {attachment_sudo}")
attachment_sudo.public = True
if not attachment_sudo.exists():
raise NotFound()
stream = request.env["ir.binary"]._get_stream_from(attachment_sudo)
# stream = request.env['ir.binary']._get_stream_from(record, field, filename, filename_field, mimetype)
stream.public = True # Allow Microsoft Online Viewer to access the file
return stream.get_response(as_attachment=bool(download))
except Exception as e:
_logger.exception(f"❌ Error serving attachment preview: {e}")
raise NotFound()
@@ -0,0 +1,44 @@
/** @odoo-module **/
import { Attachment } from "@mail/core/common/attachment_model";
import { patch } from "@web/core/utils/patch";
import { url } from "@web/core/utils/urls";
patch(Attachment.prototype, {
get isMsOfficeDocument() {
console.log("isMsOfficeDocument");
const officeMimeTypes = [
"application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .docx
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // .xlsx
"application/vnd.openxmlformats-officedocument.presentationml.presentation", // .pptx
"application/msword", // .doc
"application/vnd.ms-excel", // .xls
"application/vnd.ms-powerpoint", // .ppt
];
return officeMimeTypes.includes(this.mimetype);
},
get isViewable() {
return super.isViewable || this.isMsOfficeDocument;
},
get urlRoute() {
if (this.isMsOfficeDocument) {
return `/preview-url/${this.id}`;
}
return super.urlRoute;
},
get defaultSource() {
if (this.isMsOfficeDocument) {
const route = url(this.urlRoute, this.urlQueryParams);
const encodedRoute = encodeURIComponent(route);
console.log("route", route);
console.log("encodedRoute", encodedRoute);
return `https://view.officeapps.live.com/op/embed.aspx?src=${encodedRoute}&wdStartOn=1&wdPrint=1&wdEmbedCode=0`;
}
return super.defaultSource;
},
});
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<t t-inherit="web.FileViewer" t-inherit-mode="extension">
<xpath expr="//i[@t-if='state.file.isPdf']" position="after">
<i t-if="state.file.isMsOfficeDocument"
class="fa fa-file-text"
role="img" title="Docs file"/>
</xpath>
<xpath expr="//iframe[@t-if='state.file.isPdf']" position="after">
<iframe t-if="state.file.isMsOfficeDocument"
class="o-FileViewer-view w-75 h-100 border-0"
t-att-class="{ 'w-100': ui.isSmall }"
t-att-src="state.file.defaultSource"
allowfullscreen="true"
title="Microsoft Office Viewer"/>
</xpath>
</t>
</templates>
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<t t-inherit="mail.AttachmentView" t-inherit-mode="extension">
<xpath expr="//iframe[@t-if='state.thread.mainAttachment.isPdf']" position="after">
<iframe t-if="state.thread.mainAttachment.isMsOfficeDocument"
class="d-print-none mb48"
t-att-src="state.thread.mainAttachment.defaultSource"
style="width: 100%; height: 600px;"
allowfullscreen="true"
title="Microsoft Office Viewer"/>
</xpath>
</t>
</templates>