diff --git a/custom_ms_office_preview/__init__.py b/custom_ms_office_preview/__init__.py new file mode 100644 index 0000000..a03bfd0 --- /dev/null +++ b/custom_ms_office_preview/__init__.py @@ -0,0 +1 @@ +from . import controllers \ No newline at end of file diff --git a/custom_ms_office_preview/__manifest__.py b/custom_ms_office_preview/__manifest__.py new file mode 100644 index 0000000..5313333 --- /dev/null +++ b/custom_ms_office_preview/__manifest__.py @@ -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, +} diff --git a/custom_ms_office_preview/controllers/__init__.py b/custom_ms_office_preview/controllers/__init__.py new file mode 100644 index 0000000..deec4a8 --- /dev/null +++ b/custom_ms_office_preview/controllers/__init__.py @@ -0,0 +1 @@ +from . import main \ No newline at end of file diff --git a/custom_ms_office_preview/controllers/main.py b/custom_ms_office_preview/controllers/main.py new file mode 100644 index 0000000..2ea75ec --- /dev/null +++ b/custom_ms_office_preview/controllers/main.py @@ -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/", 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() diff --git a/custom_ms_office_preview/static/src/js/attachment_model_patch.js b/custom_ms_office_preview/static/src/js/attachment_model_patch.js new file mode 100644 index 0000000..c142005 --- /dev/null +++ b/custom_ms_office_preview/static/src/js/attachment_model_patch.js @@ -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; + }, +}); diff --git a/custom_ms_office_preview/static/src/xml/file_viewer.xml b/custom_ms_office_preview/static/src/xml/file_viewer.xml new file mode 100644 index 0000000..b476e9f --- /dev/null +++ b/custom_ms_office_preview/static/src/xml/file_viewer.xml @@ -0,0 +1,18 @@ + + + + + + + +