Skip to content

Commit

Permalink
🚑 fix image size in backend
Browse files Browse the repository at this point in the history
  • Loading branch information
diga authored and KolushovAlexandr committed Jul 5, 2019
1 parent d248e24 commit 71c2ab8
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions ir_attachment_s3/models/ir_attachment.py
Expand Up @@ -41,7 +41,7 @@ class IrAttachment(models.Model):

@api.multi
def unlink(self):
resized_ids = self.resized_ids
resized_ids = self.mapped('resized_ids')
super(IrAttachment, self).unlink()
return resized_ids.unlink() if resized_ids else True

Expand Down Expand Up @@ -107,9 +107,14 @@ def _inverse_datas(self):
for attach in self & s3_records: # datas field has got empty somehow in the result of ``s3_records = self.sudo().search([('id', 'in', self.ids)] + condition)`` search for non-superusers but it is in original recordset. Here we use original (with datas) in case it intersects with the search result
resized_to_remove |= attach.sudo().resized_ids
value = attach.datas
if attach.res_field == 'image_medium':
value = tools.image_resize_image(base64_source=attach.datas, size=(128, 128),
encoding='base64', filetype='PNG')
elif attach.res_field == 'image_small':
value = tools.image_resize_image(base64_source=attach.datas, size=(64, 64),
encoding='base64', filetype='PNG')
bin_data = value and value.decode('base64') or ''
fname = hashlib.sha1(bin_data).hexdigest()

bucket_name = self._get_s3_settings('s3.bucket', 'S3_BUCKET')
try:
s3.Bucket(bucket_name).put_object(
Expand Down Expand Up @@ -158,14 +163,15 @@ def _get_resized_from_cache(self, width, height):
def _set_resized_to_cache(self, width, height, field=None):
content = self.datas
content = tools.image_resize_image(base64_source=content, size=(width or None, height or None), encoding='base64', filetype='PNG')

new_resized_attachment_data = {
'name': '%sx%s %s' % (width, height, self.name),
'datas': content,
}
if field:
new_resized_attachment_data.update({
'res_model': self.res_model,
'res_field': field,
'res_field': self.res_field,
'res_id': self.res_id,
})
context = self._get_context_for_resized_att_creating()
Expand Down

0 comments on commit 71c2ab8

Please sign in to comment.