Skip to content

Commit 1dcf237

Browse files
committed
Calculate file and redirect hashes based on metadata plus file body.
This ensures that changes to the bucket name or other options such as ACL or Cache-Control cause redeploys.
1 parent 90b6829 commit 1dcf237

1 file changed

Lines changed: 27 additions & 18 deletions

File tree

src/Deployer.php

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,6 @@ public function upload_files( string $processed_site_path ) : void {
6565
6666
$cache_key = str_replace( $processed_site_path, '', $filename );
6767

68-
if ( \WP2Static\DeployCache::fileisCached( $cache_key, $namespace ) ) {
69-
continue;
70-
}
71-
7268
if ( ! $real_filepath ) {
7369
$err = 'Trying to deploy unknown file to S3: ' . $filename;
7470
\WP2Static\WsLog::l( $err );
@@ -94,13 +90,26 @@ public function upload_files( string $processed_site_path ) : void {
9490
}
9591

9692
$put_data['Key'] = $s3_key;
97-
$put_data['Body'] = file_get_contents( $filename );
9893
$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+
}
99108

100109
$result = $s3->putObject( $put_data );
101110

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

105114
if ( $cf_max_paths >= count( $cf_stale_paths ) ) {
106115
$cf_key = $cache_key;
@@ -119,23 +128,12 @@ public function upload_files( string $processed_site_path ) : void {
119128
$redirects = apply_filters( 'wp2static_list_redirects', [] );
120129

121130
foreach ( $redirects as $redirect ) {
122-
$file_hash = md5( '301' . $redirect['redirect_to'] );
123131
$cache_key = $redirect['url'];
124132

125133
if ( mb_substr( $cache_key, -1 ) === '/' ) {
126134
$cache_key = $cache_key . 'index.html';
127135
}
128136

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

145143
$put_data['Key'] = $s3_key;
146144
$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+
}
147156

148157
$result = $s3->putObject( $put_data );
149158

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

153162
if ( $cf_max_paths >= count( $cf_stale_paths ) ) {
154163
$cf_key = $cache_key;

0 commit comments

Comments
 (0)