-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3download.php
More file actions
201 lines (172 loc) · 6.51 KB
/
Copy paths3download.php
File metadata and controls
201 lines (172 loc) · 6.51 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
// Check if script runs from concole or browser
$cli = 0;
if (PHP_SAPI === 'cli') $cli=1;
// Current working directory
$current_dir = dirname(__FILE__);
include_once($current_dir.'/aws/S3.php');
include_once($current_dir.'/config.inc.php');
$s3 = new S3($config['amazon']['access_key_id'], $config['amazon']['secret_access_key'], false, $config['amazon']['access_point']);
S3::$useSSL = false;
//Script run from concole and have file name to download
if($cli && isset($argv[1])) {
$timestart = time();
$fileinfo = pathinfo($argv[1]);
$s3->getObject($config['amazon']['bucket'], $fileinfo['basename'], $current_dir.'/'.$fileinfo['basename']);
$filesize = filesize($current_dir.'/'.trim($argv[1]));
$downloadtime = time() - $timestart;
if($downloadtime == 0) $downloadtime = 1;
echo "\nDownloaded \"".trim($argv[1]).'" file size '.FileSizeConvert($filesize, false).' in '.$downloadtime.' seconds ('.FileSizeConvert($filesize / $downloadtime, false)."/sec)\n\n";
die();
}
// Display instructions to download, extract archive, import to MySQL
if(isset($_GET['download'])) {
$result .= '<table cellpadding="0" cellspacing="1">
<thead>
<tr>
<td colspan="2">Run the commands to process</td>
</tr>
</thead>
<tbody>';
$result .= '<tr class="file"><td class="time" nowrap>Download File: </td><td class="name" nowrap> php '.$current_dir.'/s3download.php '.$_GET['download'].'</td></tr>';
$fileinfo = pathinfo($_GET['download']);
$result .= '<tr class="file"><td class="time" nowrap>Extract File: </td><td class="name" nowrap> tar -';
if(strtolower($fileinfo['extension']) == 'gz') $result .= 'z';
elseif(strtolower($fileinfo['extension']) == 'bz2') $result .= 'j';
$result .= 'xf '.$_GET['download'].'</td></tr>';
if(isset($config['database']) && is_array($config['database'])) {
foreach($config['database'] as $id => $db) {
$result .= '<tr class="file"><td class="time" nowrap>Import MySQL: </td><td class="name" nowrap> mysql -u '.$db['username'].' -p'.$db['password'].' '.$db['database'].' < '.$fileinfo['filename'].'</td></tr>';
}
}
$result .= '<tr><td colspan="2"><a href="s3download.php">Back to Listing</a></td></tr></tbody></table>';
displaypage($result);
die();
}
$usecache = true;
$result = '';
// parameter to force listing reload
if(isset($_GET['updatelisting']) && $_GET['updatelisting'] == 1) $usecache = false;
//if we do not have the cache or it's old then we update listing.
if(!is_file('./cache.txt') || (GetCorrectMTime('./cache.txt') + 5*3600 < time())) $usecache = false;
if($usecache) {
//Get listing from cache file
$files = json_decode(file_get_contents('./cache.txt'), true);
} else {
//Get listing from S3 Amazon
$files = $s3->getBucket($config['amazon']['bucket']);
//Save all data to cache file
file_put_contents('./cache.txt', json_encode($files));
}
if(is_array($files)) {
$result .= '<table cellpadding="0" cellspacing="1">
<thead>
<tr>
<td colspan="4">'.($usecache ? 'Cached ':'').'Directory listing for '.$config['amazon']['bucket'].' (<a href="s3download.php?updatelisting=1">reload</a>)</td>
</tr>
</thead>
<tbody>
<tr class="file">
<td class="name">Name (Storage Type)</td>
<td class="size" nowrap>Size</td>
<td class="time" nowrap>Upload Date</td>
<td class="dl" nowrap>Download</td>
</tr>';
foreach($files as $id => $f) {
$result .= '<tr class="file">
<td class="name">'.$f['name'].' ('.$f['storageclass'].')</td>
<td class="size" nowrap>'.FileSizeConvert($f['size']).'</td>
<td class="time" nowrap>'.date("Y-d-m H:i", $f['time']).'</td>
<td class="dl" nowrap><a href="#">PC</a> / <a href="s3download.php?download='.$f['name'].'">Server</a></td>
</tr>';
}
} else
$result .= '<tr><td colspan="4" align="center">No Files</td></tr>';
$result .= '</tbody></table>';
displaypage($result);
/**
* Converts bytes into human readable file size.
*
* @param string $bytes
* @return string human readable file size (2,87 Мб)
* @author Mogilev Arseny
*/
function FileSizeConvert($bytes, $style = true)
{
$bytes = floatval($bytes);
$arBytes = array(
0 => array(
"UNIT" => "TB",
"VALUE" => pow(1024, 4)
),
1 => array(
"UNIT" => "GB",
"VALUE" => pow(1024, 3)
),
2 => array(
"UNIT" => "MB",
"VALUE" => pow(1024, 2)
),
3 => array(
"UNIT" => "KB",
"VALUE" => 1024
),
4 => array(
"UNIT" => "B",
"VALUE" => 1
),
);
foreach($arBytes as $arItem)
{
if($bytes >= $arItem["VALUE"])
{
$result = $bytes / $arItem["VALUE"];
$result = str_replace(".", "," , strval(round($result, 2)));
if($style) $result .= ' <span>'.$arItem["UNIT"].'</span>';
else $result .= ' '.$arItem["UNIT"];
break;
}
}
return $result;
}
function GetCorrectMTime($filePath)
{
$time = filemtime($filePath);
$isDST = (date('I', $time) == 1);
$systemDST = (date('I') == 1);
$adjustment = 0;
if($isDST == false && $systemDST == true)
$adjustment = 3600;
else if($isDST == true && $systemDST == false)
$adjustment = -3600;
else
$adjustment = 0;
return ($time + $adjustment);
}
function displaypage($content) {
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>S3 Amazon files download page</title>
<style type="text/css">
body {font-family: "Lucida Grande",Calibri,Arial; font-size: 9pt; color: #333; background: #f8f8f8;}
a {color: #b00; font-size: 11pt; font-weight: bold; text-decoration: none;}
a:hover {color: #000;}
img {vertical-align: bottom; padding: 0 3px 0 0;}
sup {color: #999;}
table {margin: 0 auto; padding: 0; width: 800px;}
table td {padding: 5px;}
thead td {padding-left: 0; font-family: "Trebuchet MS"; font-size: 11pt; font-weight: bold;}
tbody td.name {width: 99%;}
tbody .folder td {border: solid 1px #f8f8f8;}
tbody .file td {background: #fff; border: solid 1px #ddd;}
tbody tr.file:hover td {background: #ffff9d;}
tbody .file td.size, tbody .file td.time, tbody .file td.dl {white-space: nowrap; padding: 5px 10px;}
tbody .file td.size span {color: #999; font-size: 8pt;}
tbody .file td.time {color: #555;}
</style></head><body>';
echo $content;
echo '</body></html>';
}
?>