Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #15 from dibik96/17.0-develop
Browse files Browse the repository at this point in the history
G2P-2557: Mimetype issue in storage file
  • Loading branch information
shibu-narayanan authored Jul 31, 2024
2 parents 3ede455 + 3167225 commit ce8882f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
53 changes: 52 additions & 1 deletion g2p_documents/models/document_file.py
Original file line number Diff line number Diff line change
@@ -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__)
Expand Down Expand Up @@ -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:
Expand All @@ -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."
Expand Down
7 changes: 4 additions & 3 deletions g2p_documents/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 }
Expand All @@ -300,7 +301,7 @@
span.pre {
white-space: pre }

span.problematic {
span.problematic, pre.problematic {
color: red }

span.section-subtitle {
Expand Down
1 change: 1 addition & 0 deletions g2p_documents/views/g2p_document_files.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<record id="view_g2p_document_files_form" model="ir.ui.view">
<field name="name">view_g2p_document_files_form</field>
<field name="model">storage.file</field>
<field name="priority" eval="99" />
<field name="inherit_id" ref="storage_file.storage_file_view_form" />
<field name="arch" type="xml">
<xpath expr="//field[@name='name']/.." position="after">
Expand Down

0 comments on commit ce8882f

Please sign in to comment.