· 5 years ago · Oct 26, 2020, 06:00 AM
1<?php
2// {
3// "require": {
4// "aws/aws-sdk-php": "^3.55"
5// }
6// }
7header("Access-Control-Allow-Origin: *");
8require_once __DIR__."/vendor/autoload.php";
9// You can call the following to erase all pending multipart uploads.
10// It's a good idea to set your bucket to do this automatically (via console)
11// or set this in a cronjob for every 24-48 hours
12// echo abortPendingUploads(bucket());
13
14if (file_exists("keys.php"))
15 require_once "keys.php";
16else
17{
18 function aws_key(){
19 return 'AKIAZZ6BLC5DGT76OLP6';
20 }
21 function aws_secret(){
22 return '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';
23 }
24 function bucket() {
25 return "tokopop";
26 }
27}
28/**
29 * The key perfix in the bucket to put all uploads in
30 * @return string
31 */
32function prefix() {
33 return "upload/";
34}
35/**
36 * Easy wrapper around S3 API
37 * @param string $command the function to call
38 * @param mixed $args variable args to pass
39 * @return mixed
40 */
41function s3($command=null,$args=null)
42{
43 static $s3=null;
44 if ($s3===null)
45 $s3 = new Aws\S3\S3Client([
46 'version' => 'latest',
47 'region' => 'ap-southeast-1',
48 'signature_version' => 'v4',
49 'credentials' => [
50 'key' => aws_key(),
51 'secret' => aws_secret(),
52 ]
53 ]);
54 if ($command===null)
55 return $s3;
56 $args=func_get_args();
57 array_shift($args);
58 try {
59 $res=call_user_func_array([$s3,$command],$args);
60 return $res;
61 }
62 catch (AwsException $e)
63 {
64 echo $e->getMessage(),PHP_EOL;
65 }
66 return null;
67}
68/**
69 * Output data as json with proper header
70 * @param mixed $data
71 */
72function json_output($data)
73{
74 header('Content-Type: application/json');
75 die(json_encode($data));
76}
77/**
78 * Deletes all multipart uploads that are not completed.
79 *
80 * Useful to clear up the clutter from your bucket
81 * You can also set the bucket to delete them every day
82 * @return integer number of deleted objects
83 */
84function abortPendingUploads($bucket)
85{
86 $count=0;
87 $res=s3("listMultipartUploads",["Bucket"=>bucket()]);
88 if (is_array($res["Uploads"]))
89 foreach ($res["Uploads"] as $item)
90 {
91
92 $r=s3("abortMultipartUpload",[
93 "Bucket"=>$bucket,
94 "Key"=>$item["Key"],
95 "UploadId"=>$item["UploadId"],
96 ]);
97 $count++;
98 }
99 return $count;
100}
101/**
102 * Enables CORS on bucket
103 *
104 * This needs to be called exactly once on a bucket before browser uploads.
105 * @param string $bucket
106 */
107function setCORS($bucket)
108{
109 $res=s3("getBucketCors",["Bucket"=>$bucket]);
110 $res=s3("putBucketCors",
111 [
112 "Bucket"=>$bucket,
113 "CORSConfiguration"=>[
114 "CORSRules"=>[
115 [
116 'AllowedHeaders'=>['*'],
117 'AllowedMethods'=> ['POST','GET','HEAD','PUT'],
118 "AllowedOrigins"=>["localhost","*"],
119 ],
120 ],
121 ],
122 ]);
123}
124
125if (isset($_POST['command']))
126{
127 $command=$_POST['command'];
128 if ($command=="create")
129 {
130 $res=s3("createMultipartUpload",[
131 'Bucket' => bucket(),
132 'Key' => prefix().$_POST['fileInfo']['name'],
133 'ContentType' => $_REQUEST['fileInfo']['type'],
134 'Metadata' => $_REQUEST['fileInfo']
135 ]);
136 json_output(array(
137 'uploadId' => $res->get('UploadId'),
138 'key' => $res->get('Key'),
139 ));
140 }
141
142 if ($command=="part")
143 {
144 $command=s3("getCommand","UploadPart",[
145 'Bucket' => bucket(),
146 'Key' => $_REQUEST['sendBackData']['key'],
147 'UploadId' => $_REQUEST['sendBackData']['uploadId'],
148 'PartNumber' => $_REQUEST['partNumber'],
149 'ContentLength' => $_REQUEST['contentLength']
150 ]);
151
152 // Give it at least 24 hours for large uploads
153 $request=s3("createPresignedRequest",$command,"+48 hours");
154 json_output([
155 'url' => (string)$request->getUri(),
156 ]);
157 }
158
159 if ($command=="complete")
160 {
161 $partsModel = s3("listParts",[
162 'Bucket' => bucket(),
163 'Key' => $_REQUEST['sendBackData']['key'],
164 'UploadId' => $_REQUEST['sendBackData']['uploadId'],
165 ]);
166 $model = s3("completeMultipartUpload",[
167 'Bucket' => bucket(),
168 'Key' => $_REQUEST['sendBackData']['key'],
169 'UploadId' => $_REQUEST['sendBackData']['uploadId'],
170 'MultipartUpload' => [
171 "Parts"=>$partsModel["Parts"],
172 ],
173 ]);
174 json_output([
175 'success' => true
176 ]);
177 }
178 if ($command=="abort")
179 {
180 $model = s3("abortMultipartUpload",[
181 'Bucket' => bucket(),
182 'Key' => $_REQUEST['sendBackData']['key'],
183 'UploadId' => $_REQUEST['sendBackData']['uploadId']
184 ]);
185 json_output([
186 'success' => true
187 ]);
188 }
189
190 exit(0);
191}
192
193
194include "page.htm";