mirror of
https://github.com/camptocamp/odoo-cloud-platform.git
synced 2026-06-24 02:08:36 +00:00
Migrate attachments s3 to new pattern
So remove the compatibility code altogether
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
{'name': 'Attachments on S3 storage',
|
{'name': 'Attachments on S3 storage',
|
||||||
'summary': 'Store assets and attachments on a S3 compatible object storage',
|
'summary': 'Store assets and attachments on a S3 compatible object storage',
|
||||||
'version': '9.0.1.0.0',
|
'version': '9.0.1.1.0',
|
||||||
'author': 'Camptocamp',
|
'author': 'Camptocamp',
|
||||||
'license': 'AGPL-3',
|
'license': 'AGPL-3',
|
||||||
'category': 'Knowledge Management',
|
'category': 'Knowledge Management',
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Copyright 2016 Camptocamp SA
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def migrate(cr, version):
|
||||||
|
cr.execute("""
|
||||||
|
SELECT value FROM ir_config_parameter
|
||||||
|
WHERE key = 'ir_attachment.location'
|
||||||
|
""")
|
||||||
|
row = cr.fetchone()
|
||||||
|
bucket = os.environ.get('AWS_BUCKETNAME')
|
||||||
|
if row[0] == 's3' and bucket:
|
||||||
|
cr.execute("""
|
||||||
|
UPDATE ir_attachment
|
||||||
|
SET store_fname = 's3://' || %s || '/' || store_fname
|
||||||
|
WHERE store_fname IS NOT NULL AND store_fname NOT LIKE '%%/%%'
|
||||||
|
""", (os.environ['AWS_BUCKETNAME'],))
|
||||||
@@ -87,23 +87,9 @@ class IrAttachment(models.Model):
|
|||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def _file_read_s3(self, fname, bin_size=False):
|
def _file_read_s3(self, fname, bin_size=False):
|
||||||
try:
|
s3uri = S3Uri(fname)
|
||||||
s3uri = S3Uri(fname)
|
bucket = self._get_s3_bucket(name=s3uri.bucket())
|
||||||
except ValueError:
|
filekey = bucket.get_key(s3uri.item())
|
||||||
# compatibility mode: previously we stored only the key
|
|
||||||
# of the object, not we store the uri:
|
|
||||||
# example:
|
|
||||||
# before: fc02f84c0db500c69204972d27356ffdf0759386
|
|
||||||
# now: s3://bucket/fc02f84c0db500c69204972d27356ffdf0759386
|
|
||||||
# where 'project-odoo-prod' is the bucket name
|
|
||||||
bucket_name = None
|
|
||||||
item_name = fname
|
|
||||||
else:
|
|
||||||
bucket_name = s3uri.bucket()
|
|
||||||
item_name = s3uri.item()
|
|
||||||
|
|
||||||
bucket = self._get_s3_bucket(name=bucket_name)
|
|
||||||
filekey = bucket.get_key(item_name)
|
|
||||||
if filekey:
|
if filekey:
|
||||||
read = base64.b64encode(filekey.get_contents_as_string())
|
read = base64.b64encode(filekey.get_contents_as_string())
|
||||||
else:
|
else:
|
||||||
@@ -113,23 +99,11 @@ class IrAttachment(models.Model):
|
|||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def _file_read(self, fname, bin_size=False):
|
def _file_read(self, fname, bin_size=False):
|
||||||
storage = self._storage()
|
if fname.startswith('s3://'):
|
||||||
if storage == 's3' or fname.startswith('s3://'):
|
return self._file_read_s3(fname, bin_size=bin_size)
|
||||||
read = self._file_read_s3(fname, bin_size=bin_size)
|
|
||||||
if not read and not fname.startswith('s3://'):
|
|
||||||
# If the attachment has been created before the installation
|
|
||||||
# of the addon, it might still be stored on the filesystem.
|
|
||||||
# Fallback on the filesystem read.
|
|
||||||
try:
|
|
||||||
_super = super(IrAttachment, self)
|
|
||||||
read = _super._file_read(fname, bin_size=bin_size)
|
|
||||||
except (IOError, OSError):
|
|
||||||
# File is missing
|
|
||||||
read = ''
|
|
||||||
else:
|
else:
|
||||||
_super = super(IrAttachment, self)
|
_super = super(IrAttachment, self)
|
||||||
read = _super._file_read(fname, bin_size=bin_size)
|
return _super._file_read(fname, bin_size=bin_size)
|
||||||
return read
|
|
||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def _file_write(self, value, checksum):
|
def _file_write(self, value, checksum):
|
||||||
|
|||||||
Reference in New Issue
Block a user