-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.php
196 lines (187 loc) · 4.71 KB
/
index.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
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
<?php
/*
{
"require": {
"aws/aws-sdk-php": "^3.55"
}
}
*/
header("Access-Control-Allow-Origin: *");
require_once __DIR__."/vendor/autoload.php";
// You can call the following to erase all pending multipart uploads.
// It's a good idea to set your bucket to do this automatically (via console)
// or set this in a cronjob for every 24-48 hours
// echo abortPendingUploads(bucket());
if (file_exists("keys.php"))
require_once "keys.php";
else
{
function aws_key(){
return 'YOUR_AWS_KEY';
}
function aws_secret(){
return 'YOUR_AWS_SECRET';
}
function bucket() {
return "YOUR_S3_BUCKET";
}
}
/**
* The key perfix in the bucket to put all uploads in
* @return string
*/
function prefix() {
return "upload/";
}
/**
* Easy wrapper around S3 API
* @param string $command the function to call
* @param mixed $args variable args to pass
* @return mixed
*/
function s3($command=null,$args=null)
{
static $s3=null;
if ($s3===null)
$s3 = new Aws\S3\S3Client([
'version' => 'latest',
'region' => 'us-east-1',
'signature_version' => 'v4',
'credentials' => [
'key' => aws_key(),
'secret' => aws_secret(),
]
]);
if ($command===null)
return $s3;
$args=func_get_args();
array_shift($args);
try {
$res=call_user_func_array([$s3,$command],$args);
return $res;
}
catch (AwsException $e)
{
echo $e->getMessage(),PHP_EOL;
}
return null;
}
/**
* Output data as json with proper header
* @param mixed $data
*/
function json_output($data)
{
header('Content-Type: application/json');
die(json_encode($data));
}
/**
* Deletes all multipart uploads that are not completed.
*
* Useful to clear up the clutter from your bucket
* You can also set the bucket to delete them every day
* @return integer number of deleted objects
*/
function abortPendingUploads($bucket)
{
$count=0;
$res=s3("listMultipartUploads",["Bucket"=>bucket()]);
if (is_array($res["Uploads"]))
foreach ($res["Uploads"] as $item)
{
$r=s3("abortMultipartUpload",[
"Bucket"=>$bucket,
"Key"=>$item["Key"],
"UploadId"=>$item["UploadId"],
]);
$count++;
}
return $count;
}
/**
* Enables CORS on bucket
*
* This needs to be called exactly once on a bucket before browser uploads.
* @param string $bucket
*/
function setCORS($bucket)
{
$res=s3("getBucketCors",["Bucket"=>$bucket]);
$res=s3("putBucketCors",
[
"Bucket"=>$bucket,
"CORSConfiguration"=>[
"CORSRules"=>[
[
'AllowedHeaders'=>['*'],
'AllowedMethods'=> ['POST','GET','HEAD','PUT'],
"AllowedOrigins"=>["localhost","*"],
],
],
],
]);
}
if (isset($_POST['command']))
{
$command=$_POST['command'];
if ($command=="create")
{
$res=s3("createMultipartUpload",[
'Bucket' => bucket(),
'Key' => prefix().$_POST['fileInfo']['name'],
'ContentType' => $_REQUEST['fileInfo']['type'],
'Metadata' => $_REQUEST['fileInfo']
]);
json_output(array(
'uploadId' => $res->get('UploadId'),
'key' => $res->get('Key'),
));
}
if ($command=="part")
{
$command=s3("getCommand","UploadPart",[
'Bucket' => bucket(),
'Key' => $_REQUEST['sendBackData']['key'],
'UploadId' => $_REQUEST['sendBackData']['uploadId'],
'PartNumber' => $_REQUEST['partNumber'],
'ContentLength' => $_REQUEST['contentLength']
]);
// Give it at least 24 hours for large uploads
$request=s3("createPresignedRequest",$command,"+48 hours");
json_output([
'url' => (string)$request->getUri(),
]);
}
if ($command=="complete")
{
$partsModel = s3("listParts",[
'Bucket' => bucket(),
'Key' => $_REQUEST['sendBackData']['key'],
'UploadId' => $_REQUEST['sendBackData']['uploadId'],
]);
$model = s3("completeMultipartUpload",[
'Bucket' => bucket(),
'Key' => $_REQUEST['sendBackData']['key'],
'UploadId' => $_REQUEST['sendBackData']['uploadId'],
'MultipartUpload' => [
"Parts"=>$partsModel["Parts"],
],
]);
json_output([
'success' => true
]);
}
if ($command=="abort")
{
$model = s3("abortMultipartUpload",[
'Bucket' => bucket(),
'Key' => $_REQUEST['sendBackData']['key'],
'UploadId' => $_REQUEST['sendBackData']['uploadId']
]);
json_output([
'success' => true
]);
}
exit(0);
}
include "page.htm";