diff --git a/g2p_documents/models/document_file.py b/g2p_documents/models/document_file.py index 3be5771..3ef890e 100644 --- a/g2p_documents/models/document_file.py +++ b/g2p_documents/models/document_file.py @@ -1,6 +1,13 @@ +import base64 +import binascii +import io import logging +import mimetypes +import os -from odoo import _, fields, models +from PIL import Image + +from odoo import _, api, fields, models from odoo.exceptions import UserError _logger = logging.getLogger(__name__) @@ -34,6 +41,49 @@ def _compute_file_type(self): else: file.file_type = False + def _inverse_data(self): + for record in self: + record.write(record._prepare_meta_for_file()) + if not record.mimetype: + binary_data = base64.b64decode(record.data) + mime = self._get_mime_type(binary_data) + record.mimetype = mime + + record.backend_id.sudo().add( + record.relative_path, + record.data, + mimetype=record.mimetype, + binary=False, + ) + + @api.depends("name") + @api.constrains("name") + def _compute_extract_filename(self): + for rec in self: + if rec.name: + rec.filename, rec.extension = os.path.splitext(rec.name) + mime, __ = mimetypes.guess_type(rec.name) + else: + rec.filename = rec.extension = mime = False + + if mime is None and rec.data: + try: + binary_data = base64.b64decode(rec.data) + mime = self._get_mime_type(binary_data) + except binascii.Error as e: + _logger.info(f"Base64 decoding error: {e}") + + rec.mimetype = mime + + def _get_mime_type(self, binary_data): + try: + image = Image.open(io.BytesIO(binary_data)) + mime_type = Image.MIME[image.format] + return mime_type + except OSError as e: + _logger.info(f"Image processing error: {e}") + return None + def _compute_data(self): # Handled key error for rec in self: @@ -44,6 +94,7 @@ def _compute_data(self): rec.data = rec.backend_id.sudo().get(rec.relative_path, binary=False) else: rec.data = None + except Exception as e: if "NoSuchKey" in str(e): err_msg = "The file with the given name is not present on the s3." diff --git a/g2p_documents/static/description/index.html b/g2p_documents/static/description/index.html index 25d168c..eb50fcb 100644 --- a/g2p_documents/static/description/index.html +++ b/g2p_documents/static/description/index.html @@ -8,10 +8,11 @@ /* :Author: David Goodger (goodger@python.org) -:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $ +:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $ :Copyright: This stylesheet has been placed in the public domain. Default cascading style sheet for the HTML output of Docutils. +Despite the name, some widely supported CSS2 features are used. See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to customize this style sheet. @@ -274,7 +275,7 @@ margin-left: 2em ; margin-right: 2em } -pre.code .ln { color: grey; } /* line numbers */ +pre.code .ln { color: gray; } /* line numbers */ pre.code, code { background-color: #eeeeee } pre.code .comment, code .comment { color: #5C6576 } pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold } @@ -300,7 +301,7 @@ span.pre { white-space: pre } -span.problematic { +span.problematic, pre.problematic { color: red } span.section-subtitle { diff --git a/g2p_documents/views/g2p_document_files.xml b/g2p_documents/views/g2p_document_files.xml index c6d6359..d979692 100644 --- a/g2p_documents/views/g2p_document_files.xml +++ b/g2p_documents/views/g2p_document_files.xml @@ -22,6 +22,7 @@ view_g2p_document_files_form storage.file +