Skip to content

Commit

Permalink
add conditionnal mimetype upload file s3
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrilmanuel committed Jan 25, 2023
1 parent 6dd6d71 commit d373948
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion attachment_s3/models/ir_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ def _store_file_read(self, fname, bin_size=False):
@api.model
def _store_file_write(self, key, bin_data):
location = self.env.context.get('storage_location') or self._storage()
mimetype = self.env.context.get('mimetype')
if location == 's3':
bucket = self._get_s3_bucket()
obj = bucket.Object(key=key)
Expand All @@ -189,7 +190,10 @@ def _store_file_write(self, key, bin_data):
file.seek(0)
filename = 's3://%s/%s' % (bucket.name, key)
try:
obj.upload_fileobj(file)
if mimetype:
obj.upload_fileobj(file, ExtraArgs={'Metadata': {'ContentType': mimetype}})
else:
obj.upload_fileobj(file)
except ClientError as error:
# log verbose error from s3, return short message for user
_logger.exception(
Expand Down
4 changes: 4 additions & 0 deletions base_attachment_object_storage/models/ir_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ def _store_in_db_instead_of_object_storage(self):
return len(bin_data) <= limit
return False

def _get_datas_related_values(self, data, mimetype):
self=self.with_context(mimetype=mimetype)
return super(IrAttachment, self)._get_datas_related_values(data, mimetype)

def _inverse_datas(self):
# override in order to store files that need fast access,
# we keep them in the database instead of the object storage
Expand Down

0 comments on commit d373948

Please sign in to comment.