mirror of
https://github.com/camptocamp/odoo-cloud-platform.git
synced 2026-06-24 08:47:40 +00:00
[13.0][IMP] attachment_azure: Write and read storage name along filename for extended usability
This commit is contained in:
@@ -129,7 +129,8 @@ class IrAttachment(models.Model):
|
|||||||
return str.lower(storage_name)[:63]
|
return str.lower(storage_name)[:63]
|
||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def _get_azure_container(self):
|
def _get_azure_container(self, container_name=None):
|
||||||
|
if not container_name:
|
||||||
container_name = self._get_container_name()
|
container_name = self._get_container_name()
|
||||||
blob_service_client = self._get_blob_service_client()
|
blob_service_client = self._get_blob_service_client()
|
||||||
container_client = blob_service_client.get_container_client(container_name)
|
container_client = blob_service_client.get_container_client(container_name)
|
||||||
@@ -146,8 +147,12 @@ class IrAttachment(models.Model):
|
|||||||
@api.model
|
@api.model
|
||||||
def _store_file_read(self, fname, bin_size=False):
|
def _store_file_read(self, fname, bin_size=False):
|
||||||
if fname.startswith("azure://"):
|
if fname.startswith("azure://"):
|
||||||
container_client = self._get_azure_container()
|
|
||||||
key = fname.replace("azure://", "", 1).lower()
|
key = fname.replace("azure://", "", 1).lower()
|
||||||
|
if '/' in key:
|
||||||
|
container_name, key = key.split('/')
|
||||||
|
else:
|
||||||
|
container_name = None
|
||||||
|
container_client = self._get_azure_container(container_name)
|
||||||
try:
|
try:
|
||||||
blob_client = container_client.get_blob_client(key)
|
blob_client = container_client.get_blob_client(key)
|
||||||
read = base64.b64encode(blob_client.download_blob().readall())
|
read = base64.b64encode(blob_client.download_blob().readall())
|
||||||
@@ -163,11 +168,11 @@ class IrAttachment(models.Model):
|
|||||||
location = self.env.context.get("storage_location") or self._storage()
|
location = self.env.context.get("storage_location") or self._storage()
|
||||||
if location == "azure":
|
if location == "azure":
|
||||||
container_client = self._get_azure_container()
|
container_client = self._get_azure_container()
|
||||||
|
filename = "azure://%s/%s" % (container_client.container_name, key)
|
||||||
with io.BytesIO() as file:
|
with io.BytesIO() as file:
|
||||||
blob_client = container_client.get_blob_client(key.lower())
|
blob_client = container_client.get_blob_client(key.lower())
|
||||||
file.write(bin_data)
|
file.write(bin_data)
|
||||||
file.seek(0)
|
file.seek(0)
|
||||||
filename = "azure://%s" % (key)
|
|
||||||
try:
|
try:
|
||||||
blob_client.upload_blob(file, blob_type="BlockBlob")
|
blob_client.upload_blob(file, blob_type="BlockBlob")
|
||||||
except ResourceExistsError:
|
except ResourceExistsError:
|
||||||
@@ -186,8 +191,12 @@ class IrAttachment(models.Model):
|
|||||||
@api.model
|
@api.model
|
||||||
def _store_file_delete(self, fname):
|
def _store_file_delete(self, fname):
|
||||||
if fname.startswith("azure://"):
|
if fname.startswith("azure://"):
|
||||||
container_client = self._get_azure_container()
|
|
||||||
key = fname.replace("azure://", "", 1).lower()
|
key = fname.replace("azure://", "", 1).lower()
|
||||||
|
if '/' in key:
|
||||||
|
container_name, key = key.split('/')
|
||||||
|
else:
|
||||||
|
container_name = None
|
||||||
|
container_client = self._get_azure_container(container_name)
|
||||||
# delete the file only if it is on the current configured container
|
# delete the file only if it is on the current configured container
|
||||||
# otherwise, we might delete files used on a different environment
|
# otherwise, we might delete files used on a different environment
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user