forked from catchpoint/WebPageTest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3upload.php
More file actions
152 lines (145 loc) · 5.15 KB
/
Copy paths3upload.php
File metadata and controls
152 lines (145 loc) · 5.15 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
require_once __DIR__ . '/../lib/aws_v3/aws-autoloader.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$settings_file = __DIR__ . '/../settings/s3installers.ini';
$dat_files = array();
if (is_file($settings_file)) {
$settings = parse_ini_file($settings_file, true);
if (isset($settings) && is_array($settings)) {
$files = array();
BuildFileList(__DIR__, $files);
echo count($files) . " installer files found\r\n";
if (isset($settings['buckets'])) {
foreach ($settings['buckets'] as $region => $bucket) {
echo "\r\nUploading to $bucket:\r\n";
foreach ($files as $file) {
echo " Uploading $file...";
UploadFile($file, $region, $bucket, $settings);
echo "\r\n";
}
foreach ($dat_files as $dat_file) {
echo " Uploading dat file $dat_file...";
UploadDatFile($dat_file, $region, $bucket, $settings);
echo "\r\n";
}
}
}
}
}
echo "Done\r\n";
function BuildFileList($dir, &$files) {
global $dat_files;
$entries = scandir($dir);
if (isset($entries) && is_array($entries)) {
foreach ($entries as $entry) {
$path = "$dir/$entry";
if (is_dir($path)) {
if ($entry != '.' && $entry != '..') {
BuildFileList($path, $files);
}
} elseif (is_file($path)) {
$path_parts = pathinfo($path);
if ($path_parts['extension'] == 'dat') {
ProcessDatFile($path, $files);
$pos = strpos($path, 'installers/');
if ($pos > 0) {
$dat_files[] = substr($path, $pos);
}
}
}
}
}
}
function ProcessDatFile($path, &$files) {
$lines = file($path);
if (isset($lines) && is_array($lines)) {
foreach($lines as $line) {
$line = trim($line);
$separator = strpos($line, '=');
if (substr($line, 0, $separator) == 'url') {
$url = substr($line, $separator + 1);
$pos = strpos($url, 'installers/');
if ($pos > 0) {
$file = substr($url, $pos);
if (is_file(__DIR__ . '/../' . $file)) {
$filename_hash = sha1($file);
$files[$filename_hash] = $file;
}
}
}
}
}
}
function UploadFile($file, $region, $bucket, $settings) {
$local_file = __DIR__ . '/../' . $file;
if (isset($settings['config']['key']) && isset($settings['config']['secret'])) {
try {
$s3 = S3Client::factory(array('version' => '2006-03-01',
'region' => $region,
'scheme' => 'http',
'credentials' => array('key' => $settings['config']['key'],
'secret' => $settings['config']['secret'])
));
$exists = false;
try {
if ($s3->headObject(array('Bucket' => $bucket,
'Key' => $file))) {
$exists = true;
}
} catch (\Aws\S3\Exception\S3Exception $e) {
}
if (!$exists) {
if ($s3->putObject(array('Bucket' => $bucket,
'Key' => $file,
'SourceFile' => $local_file,
'ACL' => 'public-read'))) {
$result = 'OK';
} else {
$result = 'ERROR uploading';
}
} else {
$result = 'Skipping, already there';
}
} catch (\Aws\S3\Exception\S3Exception $e) {
$result = 'ERROR: ' . $e->getMessage();
}
} else {
$result = 'ERROR, not configured';
}
echo $result;
}
function UploadDatFile($file, $region, $bucket, $settings) {
$local_file = __DIR__ . '/../' . $file;
if (isset($settings['config']['key']) && isset($settings['config']['secret'])) {
try {
$s3 = S3Client::factory(array('version' => '2006-03-01',
'region' => $region,
'scheme' => 'http',
'credentials' => array('key' => $settings['config']['key'],
'secret' => $settings['config']['secret'])
));
$body = file_get_contents($local_file);
if (isset($body) && $body !== FALSE && strlen($body)) {
// Replace the http://cdn.webpagetest.org/ URL prefix with the
// correct URL for this bucket
$bucket_url = "http://$bucket.s3.amazonaws.com/";
$body = str_replace('http://cdn.webpagetest.org/', $bucket_url, $body);
if ($s3->putObject(array('Bucket' => $bucket,
'Key' => $file,
'Body' => $body,
'ACL' => 'public-read'))) {
$result = 'OK';
} else {
$result = 'ERROR uploading';
}
}
} catch (\Aws\S3\Exception\S3Exception $e) {
$result = 'ERROR: ' . $e->getMessage();
}
} else {
$result = 'ERROR, not configured';
}
echo $result;
}
?>