-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdreamhost-client.php
109 lines (81 loc) · 2.53 KB
/
dreamhost-client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
require_once MOCHI_PLUGIN_DIR . '/' . 'aws' . '/' . 'aws-autoloader.php';
require_once MOCHI_PLUGIN_DIR . '/' . 'inc' . '/' . 'functions.php';
use Aws\S3\S3Client;
class Dreamhost_Client {
public $client;
public $acl = "authenticated-read";
public function __construct() {
// Dreamhost credentials
define( 'AWS_KEY', 'secret' );
define( 'AWS_SECRET_KEY', 'secret' );
define( 'HOST', 'https://objects-us-east-1.dream.io' );
define( 'REGION', 'us-east-1' );
// bucket and test bucket are now set in mochi.php.
$this->client = new S3Client( [
'version' => '2006-03-01',
'region' => REGION,
'endpoint' => HOST,
'credentials' => [
'key' => AWS_KEY,
'secret' => AWS_SECRET_KEY,
]
] );
}
/**
* Get the file data out of form entry.
*/
public function process( $entry, $new_file_name = '' ) {
// demo: https://www.engagewp.com/send-gravity-forms-file-uploads-to-amazon-s3/
// Bail if there is no file uploaded to the form
if ( empty( $entry[ FILE_UPLOAD_FIELD_ID ] ) ) {
return false;
}
$file_url = $entry[ FILE_UPLOAD_FIELD_ID ];
$file_name = ! empty( $new_file_name) ? $new_file_name : sanitize_file_name( $_FILES[ 'input_' . FILE_UPLOAD_FIELD_ID ]['name'] );
return $this->upload( $file_url, $file_name );
}
/**
* Upload to DreamHost bucket.
*
* @param $file_url
* @param $file_name
*/
private function upload( $file_url, $file_name ) {
// demo: https://docs.aws.amazon.com/code-samples/latest/catalog/php-s3-PutObject.php.html
$url_parts = parse_url( $file_url );
$full_path = $_SERVER['DOCUMENT_ROOT'] . $url_parts['path'];
try {
$result = $this->client->putObject( [
'Bucket' => DH_UPLOAD_BUCKET,
'Key' => $file_name,
'SourceFile' => $full_path,
] );
return $result;
} catch ( \Aws\S3\Exception\S3Exception $e ) {
error_log( $e->getMessage() );
}
return false;
}
/**
* @param $file
*
* @return bool|string
*/
function signed_url( $file ) {
//$extensions = [ 'tif', 'TIF', 'tiff', 'TIFF', 'jpg', 'JPG', 'jpeg', 'JPEG', 'txt', 'TXT', 'pdf' ];
//foreach ( $extensions as $ext ) {
try {
$cmd = $this->client->getCommand('GetObject', [
'Bucket' => DH_DOWNLOAD_BUCKET,
'Key' => $file
]);
$request = $this->client->createPresignedRequest($cmd, '+1 hour');
return $request->getUri();
} catch ( \Aws\S3\Exception\S3Exception $e ) {
error_log( $e->getMessage() );
}
//}
return false;
}
}