Skip to content

Commit 0ebce54

Browse files
authored
Merge pull request #10 from luyadev/content-disposition
Allow object mutations
2 parents 61849c6 + 56ecb97 commit 0ebce54

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/).
44
In order to read more about upgrading and BC breaks have a look at the [UPGRADE Document](UPGRADE.md).
55

6+
## 1.2.1 (19. November 2020)
7+
8+
+ [#10](https://github.com/luyadev/luya-aws/pull/10) Fix issue where content type was not provided correctly, also ensure the disposition is set correctly when uploading or updating and object.
9+
610
## 1.2.0 (27. August 2020)
711

812
+ [#6](https://github.com/luyadev/luya-aws/pull/6) Add new option to update bucket policy, add command to update policy, add helper method with policies as JSON.

src/S3FileSystem.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use yii\base\InvalidConfigException;
99
use Aws\S3\S3Client;
1010
use Aws\S3\Exception\S3Exception;
11+
use luya\admin\events\FileEvent;
12+
use luya\admin\Module;
13+
use luya\helpers\FileHelper;
1114
use luya\helpers\StringHelper;
1215

1316
/**
@@ -53,7 +56,7 @@ class S3FileSystem extends BaseFileSystemStorage
5356
public $region;
5457

5558
/**
56-
* @var string The ACL default permission when writing new files.
59+
* @var string The ACL default permission when writing new files. All available options are `private|public-read|public-read-write|authenticated-read|aws-exec-read|bucket-owner-read|bucket-owner-full-control`
5760
*/
5861
public $acl = 'public-read';
5962

@@ -79,6 +82,26 @@ public function init()
7982
if ($this->region === null || $this->bucket === null || $this->key === null) {
8083
throw new InvalidConfigException("region, bucket and key must be provided for s3 component configuration.");
8184
}
85+
86+
$this->on(self::FILE_UPDATE_EVENT, function(FileEvent $event) {
87+
// Copy the object in order to not upload the content again
88+
$config = [
89+
'Bucket' => $this->bucket,
90+
'CopySource' => "{$this->bucket}/{$event->file->name_new_compound}",
91+
'Key' => $event->file->name_new_compound,
92+
'MetadataDirective' => 'REPLACE',
93+
'ContentType' => $event->file->mime_type,
94+
];
95+
96+
if ($event->file->inline_disposition) {
97+
// keep ContentDisposition because this is the default value for s3 objects
98+
// therefore ensure its not provided in the config.
99+
} else {
100+
$config['ContentDisposition'] = 'attachement'; // inline is default setting
101+
}
102+
103+
return $this->client->copyObject($config);
104+
});
82105
}
83106

84107
private $_client;
@@ -234,8 +257,14 @@ public function fileSystemSaveFile($source, $fileName)
234257
'Bucket' => $this->bucket,
235258
'Key' => $fileName,
236259
'SourceFile' => $source,
260+
'ContentType' => FileHelper::getMimeType($source),
237261
];
262+
263+
if (!Module::getInstance()->fileDefaultInlineDisposition) {
264+
$config['ContentDisposition'] = 'attachement'; // inline is default setting
265+
}
238266

267+
// see https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-s3-2006-03-01.html#putobject
239268
return $this->client->putObject($config);
240269
}
241270

0 commit comments

Comments
 (0)