mirror of
https://github.com/camptocamp/odoo-cloud-platform.git
synced 2026-06-23 18:04:34 +00:00
feat: clean version for storage (#427)
This commit is contained in:
co-authored by
GitHub
parent
d600d74e78
commit
9069a09ad6
@@ -2,4 +2,3 @@
|
||||
# Copyright 2021 Open Source Integrators
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
|
||||
from . import ir_attachment
|
||||
from . import ir_binary
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
# Copyright 2016-2019 Camptocamp SA
|
||||
# Copyright 2021 Open Source Integrators
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
|
||||
from odoo import models
|
||||
from odoo.http import Stream
|
||||
|
||||
|
||||
class IrBinary(models.AbstractModel):
|
||||
_inherit = "ir.binary"
|
||||
_description = "File streaming helper model for controllers"
|
||||
|
||||
def _azure_stream(self, attachment):
|
||||
# we will create or own tream and return it
|
||||
stream_data = self.env["ir.attachment"]._store_file_read(attachment.store_fname)
|
||||
azurestream = Stream(
|
||||
type="data",
|
||||
data=stream_data,
|
||||
path=None,
|
||||
url=None,
|
||||
mimetype=attachment.mimetype or None,
|
||||
download_name=attachment.name,
|
||||
size=len(stream_data),
|
||||
etag=attachment.checksum,
|
||||
)
|
||||
return azurestream
|
||||
|
||||
def _record_to_stream(self, record, field_name):
|
||||
"""
|
||||
Low level method responsible for the actual conversion from a
|
||||
model record to a stream. This method is an extensible hook for
|
||||
other modules. It is not meant to be directly called from
|
||||
outside or the ir.binary model.
|
||||
|
||||
:param record: the record where to load the data from.
|
||||
:param str field_name: the binary field where to load the data
|
||||
from.
|
||||
:rtype: odoo.http.Stream
|
||||
"""
|
||||
if (
|
||||
record._name == "ir.attachment"
|
||||
and record.store_fname
|
||||
and record.store_fname.startswith("azure://")
|
||||
):
|
||||
# we will create or own tream and return it
|
||||
return self._azure_stream(record)
|
||||
elif (
|
||||
record._name == "documents.document"
|
||||
and record.attachment_id
|
||||
and record.attachment_id.store_fname
|
||||
and record.attachment_id.store_fname.startswith("azure://")
|
||||
):
|
||||
return self._azure_stream(record.attachment_id)
|
||||
|
||||
else:
|
||||
return super()._record_to_stream(record, field_name)
|
||||
|
||||
|
||||
# This part is used if the customer install tne enterprise module documents
|
||||
try:
|
||||
from odoo.addons import documents
|
||||
|
||||
documents.models.ir_binary.IrBinary._record_to_stream = IrBinary._record_to_stream
|
||||
except ImportError: # pylint: disable=except-pass
|
||||
# document enterprise module if not installed, we just ignore
|
||||
pass
|
||||
@@ -1 +1,27 @@
|
||||
from . import models
|
||||
from odoo.http import Stream
|
||||
|
||||
|
||||
old_from_attachment = Stream.from_attachment
|
||||
|
||||
|
||||
@classmethod
|
||||
def from_attachment(cls, attachment):
|
||||
if attachment.store_fname and attachment._is_file_from_a_store(
|
||||
attachment.store_fname
|
||||
):
|
||||
self = cls(
|
||||
mimetype=attachment.mimetype,
|
||||
download_name=attachment.name,
|
||||
conditional=True,
|
||||
etag=attachment.checksum,
|
||||
)
|
||||
self.type = "data"
|
||||
self.data = attachment.raw
|
||||
self.last_modified = attachment["__last_update"]
|
||||
self.size = len(self.data)
|
||||
return self
|
||||
return old_from_attachment(attachment)
|
||||
|
||||
|
||||
Stream.from_attachment = from_attachment
|
||||
|
||||
Reference in New Issue
Block a user