Skip to content

Commit e1cc50c

Browse files
authored
Merge pull request #32 from john-shaffer/30-add-object-acl-option
Add Object ACL option
2 parents 0618558 + 1dcf237 commit e1cc50c

3 files changed

Lines changed: 68 additions & 20 deletions

File tree

src/Controller.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ public static function seedOptions() : void {
195195

196196
$wpdb->query( $query );
197197

198+
$query = $wpdb->prepare(
199+
$query_string,
200+
's3ObjectACL',
201+
'public-read',
202+
'Object ACL',
203+
''
204+
);
205+
206+
$wpdb->query( $query );
207+
198208
$query = $wpdb->prepare(
199209
$query_string,
200210
'cfMaxPathsToInvalidate',
@@ -433,6 +443,12 @@ public static function saveOptionsFromUI() : void {
433443
[ 'name' => 's3CacheControl' ]
434444
);
435445

446+
$wpdb->update(
447+
$table_name,
448+
[ 'value' => sanitize_text_field( $_POST['s3ObjectACL'] ) ],
449+
[ 'name' => 's3ObjectACL' ]
450+
);
451+
436452
$wpdb->update(
437453
$table_name,
438454
[ 'value' => sanitize_text_field( $_POST['cfMaxPathsToInvalidate'] ) ],
@@ -477,4 +493,5 @@ public function addOptionsPage() : void {
477493
[ $this, 'renderS3Page' ]
478494
);
479495
}
480-
}
496+
}
497+

src/Deployer.php

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ public function upload_files( string $processed_site_path ) : void {
3939
)
4040
);
4141

42+
$object_acl = Controller::getValue( 's3ObjectACL' );
4243
$put_data = [
4344
'Bucket' => Controller::getValue( 's3Bucket' ),
44-
'ACL' => 'public-read',
45+
'ACL' => $object_acl === '' ? 'public-read' : $object_acl,
4546
];
4647

4748
$cache_control = Controller::getValue( 's3CacheControl' );
@@ -64,10 +65,6 @@ public function upload_files( string $processed_site_path ) : void {
6465
6566
$cache_key = str_replace( $processed_site_path, '', $filename );
6667

67-
if ( \WP2Static\DeployCache::fileisCached( $cache_key, $namespace ) ) {
68-
continue;
69-
}
70-
7168
if ( ! $real_filepath ) {
7269
$err = 'Trying to deploy unknown file to S3: ' . $filename;
7370
\WP2Static\WsLog::l( $err );
@@ -93,13 +90,26 @@ public function upload_files( string $processed_site_path ) : void {
9390
}
9491

9592
$put_data['Key'] = $s3_key;
96-
$put_data['Body'] = file_get_contents( $filename );
9793
$put_data['ContentType'] = $mime_type;
94+
$put_data_hash = md5( json_encode( $put_data ) );
95+
$put_data['Body'] = file_get_contents( $filename );
96+
$body_hash = md5( $put_data['Body'] );
97+
$hash = md5( $put_data_hash . $body_hash );
98+
99+
$is_cached = \WP2Static\DeployCache::fileisCached(
100+
$cache_key,
101+
$namespace,
102+
$hash,
103+
);
104+
105+
if ( $is_cached ) {
106+
continue;
107+
}
98108

99109
$result = $s3->putObject( $put_data );
100110

101111
if ( $result['@metadata']['statusCode'] === 200 ) {
102-
\WP2Static\DeployCache::addFile( $cache_key, $namespace );
112+
\WP2Static\DeployCache::addFile( $cache_key, $namespace, $hash );
103113

104114
if ( $cf_max_paths >= count( $cf_stale_paths ) ) {
105115
$cf_key = $cache_key;
@@ -118,23 +128,12 @@ public function upload_files( string $processed_site_path ) : void {
118128
$redirects = apply_filters( 'wp2static_list_redirects', [] );
119129

120130
foreach ( $redirects as $redirect ) {
121-
$file_hash = md5( '301' . $redirect['redirect_to'] );
122131
$cache_key = $redirect['url'];
123132

124133
if ( mb_substr( $cache_key, -1 ) === '/' ) {
125134
$cache_key = $cache_key . 'index.html';
126135
}
127136

128-
$is_cached = \WP2Static\DeployCache::fileisCached(
129-
$cache_key,
130-
$namespace,
131-
$file_hash
132-
);
133-
134-
if ( $is_cached ) {
135-
continue;
136-
}
137-
138137
$s3_key =
139138
Controller::getValue( 's3RemotePath' ) ?
140139
Controller::getValue( 's3RemotePath' ) . '/' .
@@ -143,11 +142,22 @@ public function upload_files( string $processed_site_path ) : void {
143142

144143
$put_data['Key'] = $s3_key;
145144
$put_data['WebsiteRedirectLocation'] = $redirect['redirect_to'];
145+
$hash = md5( json_encode( $put_data ) );
146+
147+
$is_cached = \WP2Static\DeployCache::fileisCached(
148+
$cache_key,
149+
$namespace,
150+
$hash,
151+
);
152+
153+
if ( $is_cached ) {
154+
continue;
155+
}
146156

147157
$result = $s3->putObject( $put_data );
148158

149159
if ( $result['@metadata']['statusCode'] === 200 ) {
150-
\WP2Static\DeployCache::addFile( $cache_key, $namespace, $file_hash );
160+
\WP2Static\DeployCache::addFile( $cache_key, $namespace, $hash );
151161

152162
if ( $cf_max_paths >= count( $cf_stale_paths ) ) {
153163
$cf_key = $cache_key;

views/s3-page.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,27 @@
127127
</td>
128128
</tr>
129129

130+
<tr>
131+
<td style="width:50%;">
132+
<label
133+
for="<?php echo $view['options']['s3ObjectACL']->name; ?>"
134+
><?php echo $view['options']['s3ObjectACL']->label; ?></label>
135+
</td>
136+
<td>
137+
<select
138+
id="<?php echo $view['options']['s3ObjectACL']->name; ?>"
139+
name="<?php echo $view['options']['s3ObjectACL']->name; ?>"
140+
>
141+
<option
142+
<?php if ( $view['options']['s3ObjectACL']->value === 'public-read' ) { echo "selected"; } ?>
143+
value="public-read">public-read</option>
144+
<option
145+
<?php if ( $view['options']['s3ObjectACL']->value === 'private' ) { echo "selected"; } ?>
146+
value="private">private</option>
147+
</select>
148+
</td>
149+
</tr>
150+
130151
</tbody>
131152
</table>
132153

0 commit comments

Comments
 (0)