Skip to content

Commit eac6da1

Browse files
committed
Better s3 error logging
1 parent 086b623 commit eac6da1

File tree

3 files changed

+91
-54
lines changed

3 files changed

+91
-54
lines changed

inc/classes/S3Client.php

Lines changed: 69 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use SecureMedia\Utils;
1212
use \Aws\S3\S3Client as AWSS3;
13+
use \Exception;
1314

1415
if ( ! defined( 'ABSPATH' ) ) {
1516
exit; // Exit if accessed directly.
@@ -50,12 +51,22 @@ public function setup() {
5051
* @return mixed
5152
*/
5253
public function delete( $key ) {
53-
return $this->s3_client->deleteObject(
54-
[
55-
'Bucket' => Utils\get_settings( 's3_bucket' ),
56-
'Key' => $key,
57-
]
58-
);
54+
$delete = false;
55+
56+
try {
57+
$delete = $this->s3_client->deleteObject(
58+
[
59+
'Bucket' => Utils\get_settings( 's3_bucket' ),
60+
'Key' => $key,
61+
]
62+
);
63+
} catch ( Exception $e ) {
64+
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
65+
trigger_error( $e->getMessage() ); // phpcs:ignore
66+
}
67+
}
68+
69+
return $delete;
5970
}
6071

6172
/**
@@ -64,35 +75,55 @@ public function delete( $key ) {
6475
* @param string $path Path to save file
6576
* @param string $key Object key
6677
* @param boolean $delete_remote Delete remote copy or not
67-
* @return void
78+
* @return mixed
6879
*/
6980
public function save( $path, $key, $delete_remote = false ) {
70-
$this->s3_client->getObject(
71-
[
72-
'Bucket' => Utils\get_settings( 's3_bucket' ),
73-
'Key' => $key,
74-
'SaveAs' => $path,
75-
]
76-
);
81+
$save = false;
82+
83+
try {
84+
$save = $this->s3_client->getObject(
85+
[
86+
'Bucket' => Utils\get_settings( 's3_bucket' ),
87+
'Key' => $key,
88+
'SaveAs' => $path,
89+
]
90+
);
91+
} catch ( Exception $e ) {
92+
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
93+
trigger_error( $e->getMessage() ); // phpcs:ignore
94+
}
95+
}
7796

7897
if ( $delete_remote ) {
7998
$this->delete( $key );
8099
}
100+
101+
return $save;
81102
}
82103

83104
/**
84105
* Get object by key
85106
*
86107
* @param string $key Object key
87-
* @return array
108+
* @return mixed
88109
*/
89110
public function get( $key ) {
90-
return $this->s3_client->getObject(
91-
[
92-
'Bucket' => Utils\get_settings( 's3_bucket' ),
93-
'Key' => $key,
94-
]
95-
);
111+
$get = false;
112+
113+
try {
114+
$get = $this->s3_client->getObject(
115+
[
116+
'Bucket' => Utils\get_settings( 's3_bucket' ),
117+
'Key' => $key,
118+
]
119+
);
120+
} catch ( Exception $e ) {
121+
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
122+
trigger_error( $e->getMessage() ); // phpcs:ignore
123+
}
124+
}
125+
126+
return $get;
96127
}
97128

98129
/**
@@ -103,13 +134,23 @@ public function get( $key ) {
103134
* @return mixed
104135
*/
105136
public function update_acl( $acl, $key ) {
106-
return $this->s3_client->putObjectAcl(
107-
[
108-
'Bucket' => Utils\get_settings( 's3_bucket' ),
109-
'Key' => $key,
110-
'ACL' => $acl,
111-
]
112-
);
137+
$update = false;
138+
139+
try {
140+
$update = $this->s3_client->putObjectAcl(
141+
[
142+
'Bucket' => Utils\get_settings( 's3_bucket' ),
143+
'Key' => $key,
144+
'ACL' => $acl,
145+
]
146+
);
147+
} catch ( Exception $e ) {
148+
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
149+
trigger_error( $e->getMessage() ); // phpcs:ignore
150+
}
151+
}
152+
153+
return $update;
113154
}
114155

115156
/**

inc/classes/SecureMedia.php

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -217,25 +217,21 @@ public function set_media_visibility( $post_id, $public ) {
217217
$acl = $public ? 'public-read' : 'private';
218218

219219
foreach ( $keys as $key ) {
220-
try {
221-
if ( Utils\get_settings( 's3_serve_from_wp' ) ) {
222-
if ( $public ) {
223-
if ( ! file_exists( dirname( WP_CONTENT_DIR . '/' . $key ) ) ) {
224-
mkdir( dirname( WP_CONTENT_DIR . '/' . $key ), 0777, true );
225-
}
226-
227-
S3Client::factory()->save( WP_CONTENT_DIR . '/' . $key, $key );
228-
} else {
229-
if ( file_exists( WP_CONTENT_DIR . '/' . $key ) ) {
230-
unlink( WP_CONTENT_DIR . '/' . $key );
231-
}
220+
if ( Utils\get_settings( 's3_serve_from_wp' ) ) {
221+
if ( $public ) {
222+
if ( ! file_exists( dirname( WP_CONTENT_DIR . '/' . $key ) ) ) {
223+
mkdir( dirname( WP_CONTENT_DIR . '/' . $key ), 0777, true );
232224
}
233-
}
234225

235-
S3Client::factory()->update_acl( $acl, $key );
236-
} catch ( \Exception $e ) {
237-
// Do nothing
226+
S3Client::factory()->save( WP_CONTENT_DIR . '/' . $key, $key );
227+
} else {
228+
if ( file_exists( WP_CONTENT_DIR . '/' . $key ) ) {
229+
unlink( WP_CONTENT_DIR . '/' . $key );
230+
}
231+
}
238232
}
233+
234+
S3Client::factory()->update_acl( $acl, $key );
239235
}
240236

241237
if ( Utils\get_settings( 's3_serve_from_wp' ) ) {
@@ -740,15 +736,15 @@ public function maybe_show_private_media() {
740736

741737
$private_media = rtrim( $private_media, '/' );
742738

743-
try {
744-
if ( is_numeric( $private_media ) ) {
745-
$result = S3Client::factory()->get( $this->get_object_key( $private_media ) );
746-
} else {
747-
$key = $this->get_object_key( $private_media );
739+
$key = $this->get_object_key( $private_media );
748740

749-
$result = S3Client::factory()->get( $key );
750-
}
751-
} catch ( \Exception $e ) {
741+
if ( empty( $key ) ) {
742+
wp_die( 'Not found.', '', [ 'response' => 404 ] );
743+
}
744+
745+
$result = S3Client::factory()->get( $key );
746+
747+
if ( false === $result ) {
752748
wp_die( 'Not found.', '', [ 'response' => 404 ] );
753749
}
754750

secure-media.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: Secure Media
44
* Description: Store private media securely in WordPress
55
* Author: Taylor Lovett, 10up
6-
* Version: 1.0.3
6+
* Version: 1.0.4
77
* Author URI: https://10up.com
88
*
99
* Work in this plugin is derived from https://github.com/humanmade/S3-Uploads
@@ -19,7 +19,7 @@
1919

2020
define( 'SM_URL', plugin_dir_url( __FILE__ ) );
2121
define( 'SM_PATH', plugin_dir_path( __FILE__ ) );
22-
define( 'SM_VERSION', '1.0.3' );
22+
define( 'SM_VERSION', '1.0.4' );
2323

2424
require_once __DIR__ . '/inc/utils.php';
2525

0 commit comments

Comments
 (0)