@@ -12,238 +12,238 @@ |
||
12 | 12 | |
13 | 13 | class AlibabaImage |
14 | 14 | { |
15 | - private $upload_client; |
|
16 | - private $manage_client; |
|
17 | - private $ak; |
|
18 | - private $sk; |
|
19 | - private $namespace; |
|
20 | - private $type; // "TOP"和"CLOUD"两种模式 |
|
21 | - |
|
22 | - public static $run_level = Conf::RUN_LEVEL_RELEASE; //设置SDK运行级别 |
|
23 | - |
|
24 | - /** |
|
25 | - * 构造函数 |
|
26 | - * |
|
27 | - * @param string $ak 云存储公钥 |
|
28 | - * @param string $sk 云存储私钥 |
|
29 | - * @param string $type 可选,兼容TOP与tea云的 ak/sk |
|
30 | - * @throws \Exception |
|
31 | - */ |
|
32 | - public function __construct($ak, $sk, $namespace, $type = Conf::TYPE_TOP) |
|
33 | - { |
|
34 | - $this->ak = $ak; |
|
35 | - $this->sk = $sk; |
|
36 | - $this->namespace = $namespace; |
|
37 | - $this->type = $type; |
|
38 | - $this->upload_client = new UploadClient($ak, $sk, $type); |
|
39 | - $this->manage_client = new ManageClient($ak, $sk, $type); |
|
40 | - } |
|
41 | - |
|
42 | - public function getUploadToken($option) |
|
43 | - { |
|
44 | - if (!isset($option['name']) || !$option['name']) { |
|
45 | - $option['name'] = '${uuid}.${ext}'; |
|
46 | - } |
|
47 | - if ($option['name']) { |
|
48 | - $option['dir'] = preg_replace('/^\./', '', dirname($option['name'])); |
|
49 | - $option['name'] = last(explode("/", $option['name'])); |
|
50 | - } |
|
51 | - $option['namespace'] = $this->namespace; |
|
52 | - |
|
53 | - $policy = new UploadPolicy($option); |
|
54 | - |
|
55 | - return $this->upload_client->getUploadToken($policy); |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * 直接上传文件,适合文件比较小的情况 |
|
60 | - */ |
|
61 | - public function upload($filePath, UploadPolicy $uploadPolicy, UploadOption $uploadOption = null) |
|
62 | - { |
|
63 | - return $this->upload_client->upload($filePath, $uploadPolicy, $uploadOption); |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * 直接上传文件数据,适合数据量比较小的情况 |
|
68 | - */ |
|
69 | - public function uploadData($data, UploadPolicy $uploadPolicy, UploadOption $uploadOption = null) |
|
70 | - { |
|
71 | - return $this->upload_client->uploadData($data, $uploadPolicy, $uploadOption); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * 创建分片上传任务,指定待上传的文件。即初始化分片上传 |
|
76 | - */ |
|
77 | - public function multipartInit($filePath, UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
78 | - { |
|
79 | - return $this->upload_client->multipartInit($filePath, $uploadPolicy, $uploadOption); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * 创建分片上传任务,指定初始化分片任务的数据,即第一块数据 |
|
84 | - */ |
|
85 | - public function multipartInitByData($data, UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
86 | - { |
|
87 | - return $this->upload_client->multipartInitByData($data, $uploadPolicy, $uploadOption); |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * 分片上传,指定待上传的文件。需要指定UploadOption中文件块编号 |
|
92 | - */ |
|
93 | - public function multipartUpload($filePath, UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
94 | - { |
|
95 | - return $this->upload_client->multipartUpload($filePath, $uploadPolicy, $uploadOption); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * 分片上传,指定待上传的数据。需要指定UploadOption中文件块编号 |
|
100 | - */ |
|
101 | - public function multipartUploadByData($blockData, UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
102 | - { |
|
103 | - return $this->upload_client->multipartUploadByData($blockData, $uploadPolicy, $uploadOption); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * 完成分片上传任务。需要指定UploadOption中整个文件的md5值 |
|
108 | - */ |
|
109 | - public function multipartComplete(UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
110 | - { |
|
111 | - return $this->upload_client->multipartComplete($uploadPolicy, $uploadOption); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * 取消分片上传任务。需要保证UploadOption中有分片任务的uploadId和id |
|
116 | - */ |
|
117 | - public function multipartCancel(UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
118 | - { |
|
119 | - return $this->upload_client->multipartCancel($uploadPolicy, $uploadOption); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * 查看文件是否存在 |
|
124 | - */ |
|
125 | - public function existsFile($namespace, $dir, $filename) |
|
126 | - { |
|
127 | - return $this->manage_client->existsFile($namespace, $dir, $filename); |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * 获取文件的元信息(meta信息) |
|
132 | - */ |
|
133 | - public function getFileInfo($namespace, $dir, $filename) |
|
134 | - { |
|
135 | - return $this->manage_client->getFileInfo($namespace, $dir, $filename); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * 重命名文件 |
|
140 | - */ |
|
141 | - public function renameFile($namespace, $dir, $filename, $newDir, $newName) |
|
142 | - { |
|
143 | - return $this->manage_client->renameFile($namespace, $dir, $filename, $newDir, $newName); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * 获取指定目录下的文件列表。dir为空,表示根目录。page指定页数,pageSize指定每页显示数量 |
|
148 | - */ |
|
149 | - public function listFiles($namespace, $dir, $page = 1, $pageSize = 100) |
|
150 | - { |
|
151 | - return $this->manage_client->listFiles($namespace, $dir, $page, $pageSize); |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * 删除文件 |
|
156 | - */ |
|
157 | - public function deleteFile($namespace, $dir, $filename) |
|
158 | - { |
|
159 | - return $this->manage_client->deleteFile($namespace, $dir, $filename); |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * 查看文件夹是否存在 |
|
164 | - */ |
|
165 | - public function existsFolder($namespace, $dir) |
|
166 | - { |
|
167 | - return $this->manage_client->existsFolder($namespace, $dir); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * 创建文件夹 |
|
172 | - */ |
|
173 | - public function createDir($namespace, $dir) |
|
174 | - { |
|
175 | - return $this->manage_client->createDir($namespace, $dir); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * 获取子文件夹列表。dir为空,表示根目录。page指定页数,pageSize指定每页显示数量 |
|
180 | - */ |
|
181 | - public function listDirs($namespace, $dir, $page = 1, $pageSize = 100) |
|
182 | - { |
|
183 | - return $this->manage_client->listDirs($namespace, $dir, $page, $pageSize); |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * 删除文件夹 |
|
188 | - */ |
|
189 | - public function deleteDir($namespace, $dir) |
|
190 | - { |
|
191 | - return $this->manage_client->deleteDir($namespace, $dir); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * 黄图扫描接口 |
|
196 | - */ |
|
197 | - public function scanPorn(ManageOption $resInfos) |
|
198 | - { |
|
199 | - return $this->manage_client->scanPorn($resInfos); |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * 鉴黄反馈feedback接口 |
|
204 | - */ |
|
205 | - public function pornFeedback(ManageOption $pornFbInfos) |
|
206 | - { |
|
207 | - return $this->manage_client->pornFeedback($pornFbInfos); |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * 多媒体转码接口 |
|
212 | - */ |
|
213 | - public function mediaEncode(MediaEncodeOption $encodeOption) |
|
214 | - { |
|
215 | - return $this->manage_client->mediaEncode($encodeOption); |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * 多媒体转码任务查询接口 |
|
220 | - */ |
|
221 | - public function mediaEncodeQuery($taskId) |
|
222 | - { |
|
223 | - return $this->manage_client->mediaEncodeQuery($taskId); |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * 视频截图接口 |
|
228 | - */ |
|
229 | - public function videoSnapshot(SnapShotOption $snapshotOption) |
|
230 | - { |
|
231 | - return $this->manage_client->videoSnapshot($snapshotOption); |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * 视频截图结果查询接口 |
|
236 | - */ |
|
237 | - public function vSnapshotQuery($taskId) |
|
238 | - { |
|
239 | - return $this->manage_client->vSnapshotQuery($taskId); |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * 广告图扫描接口(beta) |
|
244 | - */ |
|
245 | - public function scanAdvertising(ManageOption $resInfos) |
|
246 | - { |
|
247 | - return $this->manage_client->scanAdvertising($resInfos); |
|
248 | - } |
|
15 | + private $upload_client; |
|
16 | + private $manage_client; |
|
17 | + private $ak; |
|
18 | + private $sk; |
|
19 | + private $namespace; |
|
20 | + private $type; // "TOP"和"CLOUD"两种模式 |
|
21 | + |
|
22 | + public static $run_level = Conf::RUN_LEVEL_RELEASE; //设置SDK运行级别 |
|
23 | + |
|
24 | + /** |
|
25 | + * 构造函数 |
|
26 | + * |
|
27 | + * @param string $ak 云存储公钥 |
|
28 | + * @param string $sk 云存储私钥 |
|
29 | + * @param string $type 可选,兼容TOP与tea云的 ak/sk |
|
30 | + * @throws \Exception |
|
31 | + */ |
|
32 | + public function __construct($ak, $sk, $namespace, $type = Conf::TYPE_TOP) |
|
33 | + { |
|
34 | + $this->ak = $ak; |
|
35 | + $this->sk = $sk; |
|
36 | + $this->namespace = $namespace; |
|
37 | + $this->type = $type; |
|
38 | + $this->upload_client = new UploadClient($ak, $sk, $type); |
|
39 | + $this->manage_client = new ManageClient($ak, $sk, $type); |
|
40 | + } |
|
41 | + |
|
42 | + public function getUploadToken($option) |
|
43 | + { |
|
44 | + if (!isset($option['name']) || !$option['name']) { |
|
45 | + $option['name'] = '${uuid}.${ext}'; |
|
46 | + } |
|
47 | + if ($option['name']) { |
|
48 | + $option['dir'] = preg_replace('/^\./', '', dirname($option['name'])); |
|
49 | + $option['name'] = last(explode("/", $option['name'])); |
|
50 | + } |
|
51 | + $option['namespace'] = $this->namespace; |
|
52 | + |
|
53 | + $policy = new UploadPolicy($option); |
|
54 | + |
|
55 | + return $this->upload_client->getUploadToken($policy); |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * 直接上传文件,适合文件比较小的情况 |
|
60 | + */ |
|
61 | + public function upload($filePath, UploadPolicy $uploadPolicy, UploadOption $uploadOption = null) |
|
62 | + { |
|
63 | + return $this->upload_client->upload($filePath, $uploadPolicy, $uploadOption); |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * 直接上传文件数据,适合数据量比较小的情况 |
|
68 | + */ |
|
69 | + public function uploadData($data, UploadPolicy $uploadPolicy, UploadOption $uploadOption = null) |
|
70 | + { |
|
71 | + return $this->upload_client->uploadData($data, $uploadPolicy, $uploadOption); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * 创建分片上传任务,指定待上传的文件。即初始化分片上传 |
|
76 | + */ |
|
77 | + public function multipartInit($filePath, UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
78 | + { |
|
79 | + return $this->upload_client->multipartInit($filePath, $uploadPolicy, $uploadOption); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * 创建分片上传任务,指定初始化分片任务的数据,即第一块数据 |
|
84 | + */ |
|
85 | + public function multipartInitByData($data, UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
86 | + { |
|
87 | + return $this->upload_client->multipartInitByData($data, $uploadPolicy, $uploadOption); |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * 分片上传,指定待上传的文件。需要指定UploadOption中文件块编号 |
|
92 | + */ |
|
93 | + public function multipartUpload($filePath, UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
94 | + { |
|
95 | + return $this->upload_client->multipartUpload($filePath, $uploadPolicy, $uploadOption); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * 分片上传,指定待上传的数据。需要指定UploadOption中文件块编号 |
|
100 | + */ |
|
101 | + public function multipartUploadByData($blockData, UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
102 | + { |
|
103 | + return $this->upload_client->multipartUploadByData($blockData, $uploadPolicy, $uploadOption); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * 完成分片上传任务。需要指定UploadOption中整个文件的md5值 |
|
108 | + */ |
|
109 | + public function multipartComplete(UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
110 | + { |
|
111 | + return $this->upload_client->multipartComplete($uploadPolicy, $uploadOption); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * 取消分片上传任务。需要保证UploadOption中有分片任务的uploadId和id |
|
116 | + */ |
|
117 | + public function multipartCancel(UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
118 | + { |
|
119 | + return $this->upload_client->multipartCancel($uploadPolicy, $uploadOption); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * 查看文件是否存在 |
|
124 | + */ |
|
125 | + public function existsFile($namespace, $dir, $filename) |
|
126 | + { |
|
127 | + return $this->manage_client->existsFile($namespace, $dir, $filename); |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * 获取文件的元信息(meta信息) |
|
132 | + */ |
|
133 | + public function getFileInfo($namespace, $dir, $filename) |
|
134 | + { |
|
135 | + return $this->manage_client->getFileInfo($namespace, $dir, $filename); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * 重命名文件 |
|
140 | + */ |
|
141 | + public function renameFile($namespace, $dir, $filename, $newDir, $newName) |
|
142 | + { |
|
143 | + return $this->manage_client->renameFile($namespace, $dir, $filename, $newDir, $newName); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * 获取指定目录下的文件列表。dir为空,表示根目录。page指定页数,pageSize指定每页显示数量 |
|
148 | + */ |
|
149 | + public function listFiles($namespace, $dir, $page = 1, $pageSize = 100) |
|
150 | + { |
|
151 | + return $this->manage_client->listFiles($namespace, $dir, $page, $pageSize); |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * 删除文件 |
|
156 | + */ |
|
157 | + public function deleteFile($namespace, $dir, $filename) |
|
158 | + { |
|
159 | + return $this->manage_client->deleteFile($namespace, $dir, $filename); |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * 查看文件夹是否存在 |
|
164 | + */ |
|
165 | + public function existsFolder($namespace, $dir) |
|
166 | + { |
|
167 | + return $this->manage_client->existsFolder($namespace, $dir); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * 创建文件夹 |
|
172 | + */ |
|
173 | + public function createDir($namespace, $dir) |
|
174 | + { |
|
175 | + return $this->manage_client->createDir($namespace, $dir); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * 获取子文件夹列表。dir为空,表示根目录。page指定页数,pageSize指定每页显示数量 |
|
180 | + */ |
|
181 | + public function listDirs($namespace, $dir, $page = 1, $pageSize = 100) |
|
182 | + { |
|
183 | + return $this->manage_client->listDirs($namespace, $dir, $page, $pageSize); |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * 删除文件夹 |
|
188 | + */ |
|
189 | + public function deleteDir($namespace, $dir) |
|
190 | + { |
|
191 | + return $this->manage_client->deleteDir($namespace, $dir); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * 黄图扫描接口 |
|
196 | + */ |
|
197 | + public function scanPorn(ManageOption $resInfos) |
|
198 | + { |
|
199 | + return $this->manage_client->scanPorn($resInfos); |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * 鉴黄反馈feedback接口 |
|
204 | + */ |
|
205 | + public function pornFeedback(ManageOption $pornFbInfos) |
|
206 | + { |
|
207 | + return $this->manage_client->pornFeedback($pornFbInfos); |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * 多媒体转码接口 |
|
212 | + */ |
|
213 | + public function mediaEncode(MediaEncodeOption $encodeOption) |
|
214 | + { |
|
215 | + return $this->manage_client->mediaEncode($encodeOption); |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * 多媒体转码任务查询接口 |
|
220 | + */ |
|
221 | + public function mediaEncodeQuery($taskId) |
|
222 | + { |
|
223 | + return $this->manage_client->mediaEncodeQuery($taskId); |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * 视频截图接口 |
|
228 | + */ |
|
229 | + public function videoSnapshot(SnapShotOption $snapshotOption) |
|
230 | + { |
|
231 | + return $this->manage_client->videoSnapshot($snapshotOption); |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * 视频截图结果查询接口 |
|
236 | + */ |
|
237 | + public function vSnapshotQuery($taskId) |
|
238 | + { |
|
239 | + return $this->manage_client->vSnapshotQuery($taskId); |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * 广告图扫描接口(beta) |
|
244 | + */ |
|
245 | + public function scanAdvertising(ManageOption $resInfos) |
|
246 | + { |
|
247 | + return $this->manage_client->scanAdvertising($resInfos); |
|
248 | + } |
|
249 | 249 | } |
@@ -19,7 +19,7 @@ |
||
19 | 19 | private $namespace; |
20 | 20 | private $type; // "TOP"和"CLOUD"两种模式 |
21 | 21 | |
22 | - public static $run_level = Conf::RUN_LEVEL_RELEASE; //设置SDK运行级别 |
|
22 | + public static $run_level = Conf::RUN_LEVEL_RELEASE; //设置SDK运行级别 |
|
23 | 23 | |
24 | 24 | /** |
25 | 25 | * 构造函数 |
@@ -11,128 +11,128 @@ discard block |
||
11 | 11 | |
12 | 12 | class UploadClient |
13 | 13 | { |
14 | - private $upload_host; |
|
15 | - private $ak; |
|
16 | - private $sk; |
|
17 | - private $type; // "CLOUD" or "TOP"; |
|
14 | + private $upload_host; |
|
15 | + private $ak; |
|
16 | + private $sk; |
|
17 | + private $type; // "CLOUD" or "TOP"; |
|
18 | 18 | |
19 | - public function __construct($ak, $sk, $type = Conf::TYPE_TOP) |
|
20 | - { |
|
21 | - $this->ak = $ak; |
|
22 | - $this->sk = $sk; |
|
23 | - $this->type = $type; |
|
24 | - $this->upload_host = Conf::UPLOAD_HOST_MEDIA; |
|
25 | - } |
|
19 | + public function __construct($ak, $sk, $type = Conf::TYPE_TOP) |
|
20 | + { |
|
21 | + $this->ak = $ak; |
|
22 | + $this->sk = $sk; |
|
23 | + $this->type = $type; |
|
24 | + $this->upload_host = Conf::UPLOAD_HOST_MEDIA; |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * 上传文件。根据文件大小判断是否进行分片 |
|
29 | - * @param string $filePath 文件路径 |
|
30 | - * @param UploadPolicy $uploadPolicy 上传策略 |
|
31 | - * @param UploadOption $uploadOption 上传选项 |
|
32 | - * @return array |
|
33 | - */ |
|
34 | - public function upload($filePath, UploadPolicy $uploadPolicy, UploadOption $uploadOption = null) |
|
35 | - { |
|
36 | - $encodePath = iconv('UTF-8', 'GB2312', $filePath); //中文需要转换成gb2312,file_exist等函数才能识别 |
|
37 | - if (!file_exists($encodePath)) { |
|
38 | - return $this->errorResponse("FileNotExist", "file not exist"); |
|
39 | - } |
|
40 | - if (empty($uploadOption)) { |
|
41 | - $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
42 | - } |
|
43 | - if (empty($uploadOption->name)) { |
|
44 | - $uploadOption->name = basename($filePath); //如果用户没有设置name属性,则使用上传时的文件名 |
|
45 | - } |
|
46 | - // UploadPolicy 和 UploadOption检查 |
|
47 | - list($isValid, $message) = $this->checkUploadInfo($uploadPolicy, $uploadOption); |
|
48 | - if (!$isValid) { |
|
49 | - return $this->errorResponse("ErrorUploadInfo", "error upload policy or option:" . $message); |
|
50 | - } |
|
51 | - $fileSize = filesize($encodePath); |
|
52 | - // 文件大于设定的分片大小(默认2M),则进行分片上传uploadSuperfile() |
|
53 | - if ($fileSize > ($uploadOption->blockSize)) { |
|
54 | - return $this->uploadSuperFile($encodePath, $uploadPolicy, $uploadOption); |
|
55 | - } |
|
56 | - // 文件不大于设定的分片大小(默认2M),则直接上传uploadMiniFile() |
|
57 | - return $this->uploadMiniFile($encodePath, $uploadPolicy, $uploadOption); |
|
58 | - } |
|
27 | + /** |
|
28 | + * 上传文件。根据文件大小判断是否进行分片 |
|
29 | + * @param string $filePath 文件路径 |
|
30 | + * @param UploadPolicy $uploadPolicy 上传策略 |
|
31 | + * @param UploadOption $uploadOption 上传选项 |
|
32 | + * @return array |
|
33 | + */ |
|
34 | + public function upload($filePath, UploadPolicy $uploadPolicy, UploadOption $uploadOption = null) |
|
35 | + { |
|
36 | + $encodePath = iconv('UTF-8', 'GB2312', $filePath); //中文需要转换成gb2312,file_exist等函数才能识别 |
|
37 | + if (!file_exists($encodePath)) { |
|
38 | + return $this->errorResponse("FileNotExist", "file not exist"); |
|
39 | + } |
|
40 | + if (empty($uploadOption)) { |
|
41 | + $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
42 | + } |
|
43 | + if (empty($uploadOption->name)) { |
|
44 | + $uploadOption->name = basename($filePath); //如果用户没有设置name属性,则使用上传时的文件名 |
|
45 | + } |
|
46 | + // UploadPolicy 和 UploadOption检查 |
|
47 | + list($isValid, $message) = $this->checkUploadInfo($uploadPolicy, $uploadOption); |
|
48 | + if (!$isValid) { |
|
49 | + return $this->errorResponse("ErrorUploadInfo", "error upload policy or option:" . $message); |
|
50 | + } |
|
51 | + $fileSize = filesize($encodePath); |
|
52 | + // 文件大于设定的分片大小(默认2M),则进行分片上传uploadSuperfile() |
|
53 | + if ($fileSize > ($uploadOption->blockSize)) { |
|
54 | + return $this->uploadSuperFile($encodePath, $uploadPolicy, $uploadOption); |
|
55 | + } |
|
56 | + // 文件不大于设定的分片大小(默认2M),则直接上传uploadMiniFile() |
|
57 | + return $this->uploadMiniFile($encodePath, $uploadPolicy, $uploadOption); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * 上传小文件 |
|
62 | - * @param string $filePath 文件路径 |
|
63 | - * @param UploadPolicy $uploadPolicy 上传策略 |
|
64 | - * @param UploadOption $uploadOption 上传选项 |
|
65 | - * @return array |
|
66 | - */ |
|
67 | - protected function uploadMiniFile($filePath, UploadPolicy $uploadPolicy, UploadOption $uploadOption = null) |
|
68 | - { |
|
69 | - $data = file_get_contents($filePath); |
|
70 | - $uploadOption->setContent($data); |
|
71 | - $url = $this->upload_host . Conf::UPLOAD_API_UPLOAD; //普通上传的API |
|
72 | - return $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
73 | - } |
|
60 | + /** |
|
61 | + * 上传小文件 |
|
62 | + * @param string $filePath 文件路径 |
|
63 | + * @param UploadPolicy $uploadPolicy 上传策略 |
|
64 | + * @param UploadOption $uploadOption 上传选项 |
|
65 | + * @return array |
|
66 | + */ |
|
67 | + protected function uploadMiniFile($filePath, UploadPolicy $uploadPolicy, UploadOption $uploadOption = null) |
|
68 | + { |
|
69 | + $data = file_get_contents($filePath); |
|
70 | + $uploadOption->setContent($data); |
|
71 | + $url = $this->upload_host . Conf::UPLOAD_API_UPLOAD; //普通上传的API |
|
72 | + return $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * 分片上传大文件 |
|
77 | - * @param string $filePath 文件路径 |
|
78 | - * @param UploadPolicy $uploadPolicy 上传策略 |
|
79 | - * @param UploadOption $uploadOption 上传选项 |
|
80 | - * @return array |
|
81 | - */ |
|
82 | - protected function uploadSuperFile($filePath, UploadPolicy $uploadPolicy, UploadOption $uploadOption = null) |
|
83 | - { |
|
84 | - $fileSize = filesize($filePath); // 文件大小 |
|
85 | - $blockSize = $uploadOption->blockSize; // 文件分片大小 |
|
86 | - $blockNum = intval(ceil($fileSize / $blockSize)); // 文件分片后的块数 |
|
87 | - for ($i = 0; $i < $blockNum; $i++) { |
|
88 | - $currentSize = $blockSize; // 当前文件块的大小 |
|
89 | - if (($i + 1) === $blockNum) { |
|
90 | - $currentSize = ($fileSize - ($blockNum - 1) * $blockSize); // 计算最后一个块的大小 |
|
91 | - } |
|
92 | - $offset = $i * $blockSize; // 当前文件块相对于文件开头的偏移量(块的起始位置) |
|
93 | - $blockData = file_get_contents($filePath, 0, null, $offset, $currentSize); //当前文件块的数据 |
|
94 | - $uploadOption->setContent($blockData); // 设置待上传的文件块 |
|
95 | - $httpRes = null; |
|
96 | - if (0 == $i) { |
|
97 | - // 分片初始化阶段 |
|
98 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_INIT; //初始化分片上传的API |
|
99 | - $uploadOption->optionType = UpOptionType::BLOCK_INIT_UPLOAD; //初始化分片时的Option类型 |
|
100 | - $httpRes = $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
101 | - $uploadId = isset($httpRes['uploadId']) ? $httpRes['uploadId'] : null; // 分片上传ID(OSS用于区分上传的id) |
|
102 | - $id = isset($httpRes['id']) ? $httpRes['id'] : null; // 上传唯一ID(多媒体服务用于区分上传的id) |
|
103 | - $uploadOption->setUploadId($uploadId); |
|
104 | - $uploadOption->setUniqueIdId($id); |
|
105 | - } else { |
|
106 | - // 分片上传过程中 |
|
107 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_UPLOAD; //分片上传过程中的API |
|
108 | - $uploadOption->optionType = UpOptionType::BLOCK_RUN_UPLOAD; //分片上传过程中的Option类型 |
|
109 | - $uploadOption->setPartNumber($i + 1); // |
|
110 | - $httpRes = $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
111 | - } |
|
112 | - // 如果分片上传失败,则取消cancel分片上传任务,然后返回错误信息 |
|
113 | - if (!$httpRes['isSuccess']) { |
|
114 | - if ($uploadOption->checkMutipartParas()) { |
|
115 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_CANCEL; //取消分片任务的API |
|
116 | - $uploadOption->optionType = UpOptionType::BLOCK_CANCEL_UPLOAD; //取消分片任务时的Option类型 |
|
117 | - $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); // 不判断取消分片任务返回的结果 |
|
118 | - } |
|
119 | - return $httpRes; |
|
120 | - // $message = isset( $httpRes ['message'] ) ? $httpRes ['message'] : null; |
|
121 | - // $code = isset( $httpRes ['code'] ) ? $httpRes ['code'] : null; |
|
122 | - // $requestId = isset( $httpRes ['requestId'] ) ? $httpRes ['requestId'] : null; |
|
123 | - // return $this->errorResponse ( $code, "fail upload block file:" . $message, $requestId ); |
|
124 | - } |
|
125 | - // 保存 块编号partNumber 和 标记ETag,用于分片完成时的参数设置 |
|
126 | - $uploadOption->addPartNumberAndETag($httpRes['partNumber'], $httpRes['eTag']); |
|
127 | - } |
|
128 | - // 分片上传完成 |
|
129 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_COMPLETE; //完成分片上传任务的API |
|
130 | - $uploadOption->optionType = UpOptionType::BLOCK_COMPLETE_UPLOAD; //完成分片上传任务时的Option类型 |
|
131 | - $uploadOption->setMd5(md5_file($filePath)); //文件Md5 |
|
132 | - return $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
133 | - } |
|
75 | + /** |
|
76 | + * 分片上传大文件 |
|
77 | + * @param string $filePath 文件路径 |
|
78 | + * @param UploadPolicy $uploadPolicy 上传策略 |
|
79 | + * @param UploadOption $uploadOption 上传选项 |
|
80 | + * @return array |
|
81 | + */ |
|
82 | + protected function uploadSuperFile($filePath, UploadPolicy $uploadPolicy, UploadOption $uploadOption = null) |
|
83 | + { |
|
84 | + $fileSize = filesize($filePath); // 文件大小 |
|
85 | + $blockSize = $uploadOption->blockSize; // 文件分片大小 |
|
86 | + $blockNum = intval(ceil($fileSize / $blockSize)); // 文件分片后的块数 |
|
87 | + for ($i = 0; $i < $blockNum; $i++) { |
|
88 | + $currentSize = $blockSize; // 当前文件块的大小 |
|
89 | + if (($i + 1) === $blockNum) { |
|
90 | + $currentSize = ($fileSize - ($blockNum - 1) * $blockSize); // 计算最后一个块的大小 |
|
91 | + } |
|
92 | + $offset = $i * $blockSize; // 当前文件块相对于文件开头的偏移量(块的起始位置) |
|
93 | + $blockData = file_get_contents($filePath, 0, null, $offset, $currentSize); //当前文件块的数据 |
|
94 | + $uploadOption->setContent($blockData); // 设置待上传的文件块 |
|
95 | + $httpRes = null; |
|
96 | + if (0 == $i) { |
|
97 | + // 分片初始化阶段 |
|
98 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_INIT; //初始化分片上传的API |
|
99 | + $uploadOption->optionType = UpOptionType::BLOCK_INIT_UPLOAD; //初始化分片时的Option类型 |
|
100 | + $httpRes = $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
101 | + $uploadId = isset($httpRes['uploadId']) ? $httpRes['uploadId'] : null; // 分片上传ID(OSS用于区分上传的id) |
|
102 | + $id = isset($httpRes['id']) ? $httpRes['id'] : null; // 上传唯一ID(多媒体服务用于区分上传的id) |
|
103 | + $uploadOption->setUploadId($uploadId); |
|
104 | + $uploadOption->setUniqueIdId($id); |
|
105 | + } else { |
|
106 | + // 分片上传过程中 |
|
107 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_UPLOAD; //分片上传过程中的API |
|
108 | + $uploadOption->optionType = UpOptionType::BLOCK_RUN_UPLOAD; //分片上传过程中的Option类型 |
|
109 | + $uploadOption->setPartNumber($i + 1); // |
|
110 | + $httpRes = $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
111 | + } |
|
112 | + // 如果分片上传失败,则取消cancel分片上传任务,然后返回错误信息 |
|
113 | + if (!$httpRes['isSuccess']) { |
|
114 | + if ($uploadOption->checkMutipartParas()) { |
|
115 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_CANCEL; //取消分片任务的API |
|
116 | + $uploadOption->optionType = UpOptionType::BLOCK_CANCEL_UPLOAD; //取消分片任务时的Option类型 |
|
117 | + $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); // 不判断取消分片任务返回的结果 |
|
118 | + } |
|
119 | + return $httpRes; |
|
120 | + // $message = isset( $httpRes ['message'] ) ? $httpRes ['message'] : null; |
|
121 | + // $code = isset( $httpRes ['code'] ) ? $httpRes ['code'] : null; |
|
122 | + // $requestId = isset( $httpRes ['requestId'] ) ? $httpRes ['requestId'] : null; |
|
123 | + // return $this->errorResponse ( $code, "fail upload block file:" . $message, $requestId ); |
|
124 | + } |
|
125 | + // 保存 块编号partNumber 和 标记ETag,用于分片完成时的参数设置 |
|
126 | + $uploadOption->addPartNumberAndETag($httpRes['partNumber'], $httpRes['eTag']); |
|
127 | + } |
|
128 | + // 分片上传完成 |
|
129 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_COMPLETE; //完成分片上传任务的API |
|
130 | + $uploadOption->optionType = UpOptionType::BLOCK_COMPLETE_UPLOAD; //完成分片上传任务时的Option类型 |
|
131 | + $uploadOption->setMd5(md5_file($filePath)); //文件Md5 |
|
132 | + return $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
133 | + } |
|
134 | 134 | |
135 | - /**上传字符串/二进制数据 |
|
135 | + /**上传字符串/二进制数据 |
|
136 | 136 | * @param string $data |
137 | 137 | * 文件数据 |
138 | 138 | * @param UploadPolicy $uploadPolicy |
@@ -141,450 +141,450 @@ discard block |
||
141 | 141 | * 上传选项 |
142 | 142 | * @return array |
143 | 143 | */ |
144 | - public function uploadData($data, UploadPolicy $uploadPolicy, UploadOption $uploadOption = null) |
|
145 | - { |
|
146 | - if (empty($uploadOption)) { |
|
147 | - $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
148 | - } |
|
149 | - // UploadPolicy 和 UploadOption检查 |
|
150 | - list($isValid, $message) = $this->checkUploadInfo($uploadPolicy, $uploadOption); |
|
151 | - if (!$isValid) { |
|
152 | - return $this->errorResponse("ErrorUploadInfo", "error upload policy or option:" . $message); |
|
153 | - } |
|
154 | - $dataSize = strlen($data); |
|
155 | - // 文件大于设定的分片大小(默认2M),则进行分片上传uploadSuperfile() |
|
156 | - if ($dataSize > ($uploadOption->blockSize)) { |
|
157 | - return $this->uploadSuperData($data, $uploadPolicy, $uploadOption); |
|
158 | - } |
|
159 | - // 文件不大于设定的分片大小(默认2M),则直接上传uploadMiniFile() |
|
160 | - $uploadOption->setContent($data); |
|
161 | - $url = $this->upload_host . Conf::UPLOAD_API_UPLOAD; //普通上传的API |
|
162 | - return $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
163 | - } |
|
144 | + public function uploadData($data, UploadPolicy $uploadPolicy, UploadOption $uploadOption = null) |
|
145 | + { |
|
146 | + if (empty($uploadOption)) { |
|
147 | + $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
148 | + } |
|
149 | + // UploadPolicy 和 UploadOption检查 |
|
150 | + list($isValid, $message) = $this->checkUploadInfo($uploadPolicy, $uploadOption); |
|
151 | + if (!$isValid) { |
|
152 | + return $this->errorResponse("ErrorUploadInfo", "error upload policy or option:" . $message); |
|
153 | + } |
|
154 | + $dataSize = strlen($data); |
|
155 | + // 文件大于设定的分片大小(默认2M),则进行分片上传uploadSuperfile() |
|
156 | + if ($dataSize > ($uploadOption->blockSize)) { |
|
157 | + return $this->uploadSuperData($data, $uploadPolicy, $uploadOption); |
|
158 | + } |
|
159 | + // 文件不大于设定的分片大小(默认2M),则直接上传uploadMiniFile() |
|
160 | + $uploadOption->setContent($data); |
|
161 | + $url = $this->upload_host . Conf::UPLOAD_API_UPLOAD; //普通上传的API |
|
162 | + return $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
163 | + } |
|
164 | 164 | |
165 | - /** |
|
166 | - * 分片上传大文件数据 |
|
167 | - * @param string $data 文件数据 |
|
168 | - * @param UploadPolicy $uploadPolicy 上传策略 |
|
169 | - * @param UploadOption $uploadOption 上传选项 |
|
170 | - * @return array |
|
171 | - */ |
|
172 | - protected function uploadSuperData($data, UploadPolicy $uploadPolicy, UploadOption $uploadOption = null) |
|
173 | - { |
|
174 | - $dataSize = strlen($data); // 文件大小 |
|
175 | - $blockSize = $uploadOption->blockSize; // 文件分片大小 |
|
176 | - $blockNum = intval(ceil($dataSize / $blockSize)); // 文件分片后的块数 |
|
177 | - for ($i = 0; $i < $blockNum; $i++) { |
|
178 | - $currentSize = $blockSize; // 当前文件块的大小 |
|
179 | - if (($i + 1) === $blockNum) { |
|
180 | - $currentSize = ($dataSize - ($blockNum - 1) * $blockSize); // 计算最后一个块的大小 |
|
181 | - } |
|
182 | - $offset = $i * $blockSize; // 当前文件块相对于文件开头的偏移量(块的起始位置) |
|
183 | - $blockData = substr($data, $offset, $currentSize); //当前文件块的数据 |
|
184 | - $uploadOption->setContent($blockData); // 设置待上传的文件块 |
|
185 | - $httpRes = null; |
|
186 | - if (0 == $i) { |
|
187 | - // 分片初始化阶段 |
|
188 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_INIT; //初始化分片上传的API |
|
189 | - $uploadOption->optionType = UpOptionType::BLOCK_INIT_UPLOAD; //初始化分片时的Option类型 |
|
190 | - $httpRes = $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
191 | - $uploadId = isset($httpRes['uploadId']) ? $httpRes['uploadId'] : null; // 分片上传ID(OSS用于区分上传的id) |
|
192 | - $id = isset($httpRes['id']) ? $httpRes['id'] : null; // 上传唯一ID(多媒体服务用于区分上传的id) |
|
193 | - $uploadOption->setUploadId($uploadId); |
|
194 | - $uploadOption->setUniqueIdId($id); |
|
195 | - } else { |
|
196 | - // 分片上传过程中 |
|
197 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_UPLOAD; //分片上传过程中的API |
|
198 | - $uploadOption->optionType = UpOptionType::BLOCK_RUN_UPLOAD; //分片上传过程中的Option类型 |
|
199 | - $uploadOption->setPartNumber($i + 1); // |
|
200 | - $httpRes = $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
201 | - } |
|
202 | - // 如果分片上传失败,则取消cancel分片上传任务,然后返回错误信息 |
|
203 | - if (!$httpRes['isSuccess']) { |
|
204 | - if ($uploadOption->checkMutipartParas()) { |
|
205 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_CANCEL; //取消分片任务的API |
|
206 | - $uploadOption->optionType = UpOptionType::BLOCK_CANCEL_UPLOAD; //取消分片任务时的Option类型 |
|
207 | - $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); // 不判断取消分片任务返回的结果 |
|
208 | - } |
|
209 | - return $httpRes; |
|
210 | - } |
|
211 | - // 保存 块编号partNumber 和 标记ETag,用于分片完成时的参数设置 |
|
212 | - $uploadOption->addPartNumberAndETag($httpRes['partNumber'], $httpRes['eTag']); |
|
213 | - } |
|
214 | - // 分片上传完成 |
|
215 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_COMPLETE; //完成分片上传任务的API |
|
216 | - $uploadOption->optionType = UpOptionType::BLOCK_COMPLETE_UPLOAD; //完成分片上传任务时的Option类型 |
|
217 | - $uploadOption->setMd5(md5($data)); //文件Md5 |
|
218 | - return $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
219 | - } |
|
165 | + /** |
|
166 | + * 分片上传大文件数据 |
|
167 | + * @param string $data 文件数据 |
|
168 | + * @param UploadPolicy $uploadPolicy 上传策略 |
|
169 | + * @param UploadOption $uploadOption 上传选项 |
|
170 | + * @return array |
|
171 | + */ |
|
172 | + protected function uploadSuperData($data, UploadPolicy $uploadPolicy, UploadOption $uploadOption = null) |
|
173 | + { |
|
174 | + $dataSize = strlen($data); // 文件大小 |
|
175 | + $blockSize = $uploadOption->blockSize; // 文件分片大小 |
|
176 | + $blockNum = intval(ceil($dataSize / $blockSize)); // 文件分片后的块数 |
|
177 | + for ($i = 0; $i < $blockNum; $i++) { |
|
178 | + $currentSize = $blockSize; // 当前文件块的大小 |
|
179 | + if (($i + 1) === $blockNum) { |
|
180 | + $currentSize = ($dataSize - ($blockNum - 1) * $blockSize); // 计算最后一个块的大小 |
|
181 | + } |
|
182 | + $offset = $i * $blockSize; // 当前文件块相对于文件开头的偏移量(块的起始位置) |
|
183 | + $blockData = substr($data, $offset, $currentSize); //当前文件块的数据 |
|
184 | + $uploadOption->setContent($blockData); // 设置待上传的文件块 |
|
185 | + $httpRes = null; |
|
186 | + if (0 == $i) { |
|
187 | + // 分片初始化阶段 |
|
188 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_INIT; //初始化分片上传的API |
|
189 | + $uploadOption->optionType = UpOptionType::BLOCK_INIT_UPLOAD; //初始化分片时的Option类型 |
|
190 | + $httpRes = $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
191 | + $uploadId = isset($httpRes['uploadId']) ? $httpRes['uploadId'] : null; // 分片上传ID(OSS用于区分上传的id) |
|
192 | + $id = isset($httpRes['id']) ? $httpRes['id'] : null; // 上传唯一ID(多媒体服务用于区分上传的id) |
|
193 | + $uploadOption->setUploadId($uploadId); |
|
194 | + $uploadOption->setUniqueIdId($id); |
|
195 | + } else { |
|
196 | + // 分片上传过程中 |
|
197 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_UPLOAD; //分片上传过程中的API |
|
198 | + $uploadOption->optionType = UpOptionType::BLOCK_RUN_UPLOAD; //分片上传过程中的Option类型 |
|
199 | + $uploadOption->setPartNumber($i + 1); // |
|
200 | + $httpRes = $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
201 | + } |
|
202 | + // 如果分片上传失败,则取消cancel分片上传任务,然后返回错误信息 |
|
203 | + if (!$httpRes['isSuccess']) { |
|
204 | + if ($uploadOption->checkMutipartParas()) { |
|
205 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_CANCEL; //取消分片任务的API |
|
206 | + $uploadOption->optionType = UpOptionType::BLOCK_CANCEL_UPLOAD; //取消分片任务时的Option类型 |
|
207 | + $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); // 不判断取消分片任务返回的结果 |
|
208 | + } |
|
209 | + return $httpRes; |
|
210 | + } |
|
211 | + // 保存 块编号partNumber 和 标记ETag,用于分片完成时的参数设置 |
|
212 | + $uploadOption->addPartNumberAndETag($httpRes['partNumber'], $httpRes['eTag']); |
|
213 | + } |
|
214 | + // 分片上传完成 |
|
215 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_COMPLETE; //完成分片上传任务的API |
|
216 | + $uploadOption->optionType = UpOptionType::BLOCK_COMPLETE_UPLOAD; //完成分片上传任务时的Option类型 |
|
217 | + $uploadOption->setMd5(md5($data)); //文件Md5 |
|
218 | + return $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
219 | + } |
|
220 | 220 | |
221 | - /** |
|
222 | - * 创建分片上传任务,指定待上传的文件。即初始化分片上传 |
|
223 | - * @param string $filePath 文件路径 |
|
224 | - * @param UploadPolicy $uploadPolicy 上传策略 |
|
225 | - * @param UploadOption $uploadOption 上传选项 |
|
226 | - * @return array 初始化分片上传的结果 |
|
227 | - */ |
|
228 | - public function multipartInit($filePath, UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
229 | - { |
|
230 | - $encodePath = iconv('UTF-8', 'GB2312', $filePath); //中文需要转换成gb2312,file_exist等函数才能识别 |
|
231 | - if (!file_exists($encodePath)) { |
|
232 | - return $this->errorResponse("FileNotExist", "file not exist"); |
|
233 | - } |
|
234 | - if (empty($uploadOption)) { |
|
235 | - $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
236 | - } |
|
237 | - if (empty($uploadOption->name)) { |
|
238 | - $uploadOption->name = basename($filePath); //如果用户没有设置name属性,则使用上传时的文件名 |
|
239 | - } |
|
240 | - $blockData = file_get_contents($encodePath, 0, null, 0, $uploadOption->blockSize); |
|
241 | - return $this->multipartInitByData($blockData, $uploadPolicy, $uploadOption); |
|
242 | - } |
|
221 | + /** |
|
222 | + * 创建分片上传任务,指定待上传的文件。即初始化分片上传 |
|
223 | + * @param string $filePath 文件路径 |
|
224 | + * @param UploadPolicy $uploadPolicy 上传策略 |
|
225 | + * @param UploadOption $uploadOption 上传选项 |
|
226 | + * @return array 初始化分片上传的结果 |
|
227 | + */ |
|
228 | + public function multipartInit($filePath, UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
229 | + { |
|
230 | + $encodePath = iconv('UTF-8', 'GB2312', $filePath); //中文需要转换成gb2312,file_exist等函数才能识别 |
|
231 | + if (!file_exists($encodePath)) { |
|
232 | + return $this->errorResponse("FileNotExist", "file not exist"); |
|
233 | + } |
|
234 | + if (empty($uploadOption)) { |
|
235 | + $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
236 | + } |
|
237 | + if (empty($uploadOption->name)) { |
|
238 | + $uploadOption->name = basename($filePath); //如果用户没有设置name属性,则使用上传时的文件名 |
|
239 | + } |
|
240 | + $blockData = file_get_contents($encodePath, 0, null, 0, $uploadOption->blockSize); |
|
241 | + return $this->multipartInitByData($blockData, $uploadPolicy, $uploadOption); |
|
242 | + } |
|
243 | 243 | |
244 | - /** |
|
245 | - * 创建分片上传任务,指定初始化分片任务的数据,即第一块数据 |
|
246 | - * @param string $blockData 文件数据 |
|
247 | - * @param UploadPolicy $uploadPolicy 上传策略 |
|
248 | - * @param UploadOption $uploadOption 上传选项 |
|
249 | - * @return array 初始化分片上传的结果 |
|
250 | - */ |
|
251 | - public function multipartInitByData($blockData, UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
252 | - { |
|
253 | - if (empty($uploadOption)) { |
|
254 | - $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
255 | - } |
|
256 | - // UploadPolicy 和 UploadOption检查 |
|
257 | - list($isValid, $message) = $this->checkUploadInfo($uploadPolicy, $uploadOption); |
|
258 | - if (!$isValid) { |
|
259 | - return $this->errorResponse("ErrorUploadInfo", "error upload policy or option:" . $message); |
|
260 | - } |
|
261 | - // 数据大小不等于设定的分片大小(默认2M),则无法完成初始化 |
|
262 | - $dataSize = strlen($blockData); // 数据文件大小 |
|
263 | - if ($dataSize != ($uploadOption->blockSize)) { |
|
264 | - return $this->errorResponse("MultipartInitError", "UploadOption's blockSize is not equal to data's size"); |
|
265 | - } |
|
266 | - $uploadOption->setContent($blockData); // 设置待上传的文件块 |
|
267 | - $uploadOption->optionType = UpOptionType::BLOCK_INIT_UPLOAD; //初始化分片时的Option类型 |
|
268 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_INIT; //初始化分片上传的API |
|
269 | - $httpRes = $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
270 | - //若成功返回,则保存初始化成功的uploadId、id 以及 partNumber、eTag |
|
271 | - $uploadId = isset($httpRes['uploadId']) ? $httpRes['uploadId'] : null; // 分片上传ID(OSS用于区分上传的id) |
|
272 | - $id = isset($httpRes['id']) ? $httpRes['id'] : null; // 上传唯一ID(多媒体服务用于区分上传的id) |
|
273 | - $uploadOption->setUploadId($uploadId); |
|
274 | - $uploadOption->setUniqueIdId($id); |
|
275 | - if (isset($httpRes['partNumber']) && isset($httpRes['eTag'])) { |
|
276 | - $uploadOption->addPartNumberAndETag($httpRes['partNumber'], $httpRes['eTag']); |
|
277 | - } |
|
278 | - return $httpRes; |
|
279 | - } |
|
244 | + /** |
|
245 | + * 创建分片上传任务,指定初始化分片任务的数据,即第一块数据 |
|
246 | + * @param string $blockData 文件数据 |
|
247 | + * @param UploadPolicy $uploadPolicy 上传策略 |
|
248 | + * @param UploadOption $uploadOption 上传选项 |
|
249 | + * @return array 初始化分片上传的结果 |
|
250 | + */ |
|
251 | + public function multipartInitByData($blockData, UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
252 | + { |
|
253 | + if (empty($uploadOption)) { |
|
254 | + $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
255 | + } |
|
256 | + // UploadPolicy 和 UploadOption检查 |
|
257 | + list($isValid, $message) = $this->checkUploadInfo($uploadPolicy, $uploadOption); |
|
258 | + if (!$isValid) { |
|
259 | + return $this->errorResponse("ErrorUploadInfo", "error upload policy or option:" . $message); |
|
260 | + } |
|
261 | + // 数据大小不等于设定的分片大小(默认2M),则无法完成初始化 |
|
262 | + $dataSize = strlen($blockData); // 数据文件大小 |
|
263 | + if ($dataSize != ($uploadOption->blockSize)) { |
|
264 | + return $this->errorResponse("MultipartInitError", "UploadOption's blockSize is not equal to data's size"); |
|
265 | + } |
|
266 | + $uploadOption->setContent($blockData); // 设置待上传的文件块 |
|
267 | + $uploadOption->optionType = UpOptionType::BLOCK_INIT_UPLOAD; //初始化分片时的Option类型 |
|
268 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_INIT; //初始化分片上传的API |
|
269 | + $httpRes = $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
270 | + //若成功返回,则保存初始化成功的uploadId、id 以及 partNumber、eTag |
|
271 | + $uploadId = isset($httpRes['uploadId']) ? $httpRes['uploadId'] : null; // 分片上传ID(OSS用于区分上传的id) |
|
272 | + $id = isset($httpRes['id']) ? $httpRes['id'] : null; // 上传唯一ID(多媒体服务用于区分上传的id) |
|
273 | + $uploadOption->setUploadId($uploadId); |
|
274 | + $uploadOption->setUniqueIdId($id); |
|
275 | + if (isset($httpRes['partNumber']) && isset($httpRes['eTag'])) { |
|
276 | + $uploadOption->addPartNumberAndETag($httpRes['partNumber'], $httpRes['eTag']); |
|
277 | + } |
|
278 | + return $httpRes; |
|
279 | + } |
|
280 | 280 | |
281 | - /**分片上传,指定待上传的文件。需要指定UploadOption中文件块编号 |
|
281 | + /**分片上传,指定待上传的文件。需要指定UploadOption中文件块编号 |
|
282 | 282 | * @param string $filePath 文件路径 |
283 | 283 | * @param UploadPolicy $uploadPolicy 上传策略 |
284 | 284 | * @param UploadOption $uploadOption 上传选项 |
285 | 285 | * @return array 初始化分片上传的结果 |
286 | 286 | */ |
287 | - public function multipartUpload($filePath, UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
288 | - { |
|
289 | - $encodePath = iconv('UTF-8', 'GB2312', $filePath); //中文需要转换成gb2312,file_exist等函数才能识别 |
|
290 | - if (!file_exists($encodePath)) { |
|
291 | - return $this->errorResponse("FileNotExist", "file not exist"); |
|
292 | - } |
|
293 | - $fileSize = filesize($encodePath); // 文件大小 |
|
294 | - $blockSize = $uploadOption->blockSize; // 文件分片大小 |
|
295 | - $blockNum = intval(ceil($fileSize / $blockSize)); // 文件分片后的块数 |
|
296 | - $currentSize = $blockSize; // 当前文件块的大小 |
|
297 | - if ($uploadOption->getPartNumber() == $blockNum) { |
|
298 | - $currentSize = ($fileSize - ($blockNum - 1) * $blockSize); // 计算最后一个块的大小 |
|
299 | - } |
|
300 | - $offset = ($uploadOption->getPartNumber() - 1) * $blockSize; // 当前文件块相对于文件开头的偏移量(块的起始位置) |
|
301 | - $blockData = file_get_contents($encodePath, 0, null, $offset, $currentSize); |
|
302 | - return $this->multipartUploadByData($blockData, $uploadPolicy, $uploadOption); |
|
303 | - } |
|
287 | + public function multipartUpload($filePath, UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
288 | + { |
|
289 | + $encodePath = iconv('UTF-8', 'GB2312', $filePath); //中文需要转换成gb2312,file_exist等函数才能识别 |
|
290 | + if (!file_exists($encodePath)) { |
|
291 | + return $this->errorResponse("FileNotExist", "file not exist"); |
|
292 | + } |
|
293 | + $fileSize = filesize($encodePath); // 文件大小 |
|
294 | + $blockSize = $uploadOption->blockSize; // 文件分片大小 |
|
295 | + $blockNum = intval(ceil($fileSize / $blockSize)); // 文件分片后的块数 |
|
296 | + $currentSize = $blockSize; // 当前文件块的大小 |
|
297 | + if ($uploadOption->getPartNumber() == $blockNum) { |
|
298 | + $currentSize = ($fileSize - ($blockNum - 1) * $blockSize); // 计算最后一个块的大小 |
|
299 | + } |
|
300 | + $offset = ($uploadOption->getPartNumber() - 1) * $blockSize; // 当前文件块相对于文件开头的偏移量(块的起始位置) |
|
301 | + $blockData = file_get_contents($encodePath, 0, null, $offset, $currentSize); |
|
302 | + return $this->multipartUploadByData($blockData, $uploadPolicy, $uploadOption); |
|
303 | + } |
|
304 | 304 | |
305 | - /** |
|
306 | - * 分片上传,指定待上传的数据。需要指定UploadOption中文件块编号 |
|
307 | - * @param string $filePath 文件路径 |
|
308 | - * @param UploadPolicy $uploadPolicy 上传策略 |
|
309 | - * @param UploadOption $uploadOption 上传选项 |
|
310 | - * @return array 分片上传的结果 |
|
311 | - */ |
|
312 | - public function multipartUploadByData($blockData, UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
313 | - { |
|
314 | - $partNumber = $uploadOption->getPartNumber(); //php 5.3的版本使用empty()传递函数返回值会报错。所以为了兼容,增加临时变量 |
|
315 | - // 检查分片上传所需的参数是否设置正确 |
|
316 | - if (!$uploadOption->checkMutipartParas() || empty($partNumber)) { |
|
317 | - return $this->errorResponse("MultipartUploadError", "multipart upload's parameters(id,uploadId,partNumber) error"); |
|
318 | - } |
|
319 | - $uploadOption->setContent($blockData); // 设置待上传的文件块 |
|
320 | - $uploadOption->optionType = UpOptionType::BLOCK_RUN_UPLOAD; //分片上传过程中的Option类型 |
|
321 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_UPLOAD; //分片上传过程中的API |
|
322 | - $httpRes = $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
323 | - if (isset($httpRes['partNumber']) && isset($httpRes['eTag'])) { |
|
324 | - $uploadOption->addPartNumberAndETag($httpRes['partNumber'], $httpRes['eTag']); |
|
325 | - } |
|
326 | - return $httpRes; |
|
327 | - } |
|
305 | + /** |
|
306 | + * 分片上传,指定待上传的数据。需要指定UploadOption中文件块编号 |
|
307 | + * @param string $filePath 文件路径 |
|
308 | + * @param UploadPolicy $uploadPolicy 上传策略 |
|
309 | + * @param UploadOption $uploadOption 上传选项 |
|
310 | + * @return array 分片上传的结果 |
|
311 | + */ |
|
312 | + public function multipartUploadByData($blockData, UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
313 | + { |
|
314 | + $partNumber = $uploadOption->getPartNumber(); //php 5.3的版本使用empty()传递函数返回值会报错。所以为了兼容,增加临时变量 |
|
315 | + // 检查分片上传所需的参数是否设置正确 |
|
316 | + if (!$uploadOption->checkMutipartParas() || empty($partNumber)) { |
|
317 | + return $this->errorResponse("MultipartUploadError", "multipart upload's parameters(id,uploadId,partNumber) error"); |
|
318 | + } |
|
319 | + $uploadOption->setContent($blockData); // 设置待上传的文件块 |
|
320 | + $uploadOption->optionType = UpOptionType::BLOCK_RUN_UPLOAD; //分片上传过程中的Option类型 |
|
321 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_UPLOAD; //分片上传过程中的API |
|
322 | + $httpRes = $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
323 | + if (isset($httpRes['partNumber']) && isset($httpRes['eTag'])) { |
|
324 | + $uploadOption->addPartNumberAndETag($httpRes['partNumber'], $httpRes['eTag']); |
|
325 | + } |
|
326 | + return $httpRes; |
|
327 | + } |
|
328 | 328 | |
329 | - /** |
|
330 | - * 完成分片上传任务。需要指定UploadOption中整个文件的md5值 |
|
331 | - * @param UploadPolicy $uploadPolicy 上传策略 |
|
332 | - * @param UploadOption $uploadOption 上传选项 |
|
333 | - * @return array 分片上传完成的结果 |
|
334 | - */ |
|
335 | - public function multipartComplete(UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
336 | - { |
|
329 | + /** |
|
330 | + * 完成分片上传任务。需要指定UploadOption中整个文件的md5值 |
|
331 | + * @param UploadPolicy $uploadPolicy 上传策略 |
|
332 | + * @param UploadOption $uploadOption 上传选项 |
|
333 | + * @return array 分片上传完成的结果 |
|
334 | + */ |
|
335 | + public function multipartComplete(UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
336 | + { |
|
337 | 337 | // 检查分片上传所需的参数是否设置正确 |
338 | - $fileMd5 = $uploadOption->getMd5(); //php 5.3的版本使用empty()传递函数返回值会报错。所以为了兼容,增加临时变量 |
|
339 | - if (!$uploadOption->checkMutipartParas() || empty($fileMd5)) { |
|
340 | - return $this->errorResponse("MultipartCompleteError", "multipart upload's parameters(id,uploadId,md5) error"); |
|
341 | - } |
|
342 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_COMPLETE; //完成分片上传任务的API |
|
343 | - $uploadOption->optionType = UpOptionType::BLOCK_COMPLETE_UPLOAD; //完成分片上传任务时的Option类型 |
|
344 | - return $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
345 | - } |
|
338 | + $fileMd5 = $uploadOption->getMd5(); //php 5.3的版本使用empty()传递函数返回值会报错。所以为了兼容,增加临时变量 |
|
339 | + if (!$uploadOption->checkMutipartParas() || empty($fileMd5)) { |
|
340 | + return $this->errorResponse("MultipartCompleteError", "multipart upload's parameters(id,uploadId,md5) error"); |
|
341 | + } |
|
342 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_COMPLETE; //完成分片上传任务的API |
|
343 | + $uploadOption->optionType = UpOptionType::BLOCK_COMPLETE_UPLOAD; //完成分片上传任务时的Option类型 |
|
344 | + return $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
345 | + } |
|
346 | 346 | |
347 | - /** |
|
348 | - * 取消分片上传任务。需要保证UploadOption中有分片任务的uploadId和id |
|
349 | - * @param UploadPolicy $uploadPolicy 上传策略 |
|
350 | - * @param UploadOption $uploadOption 上传选项 |
|
351 | - * @return array 分片上传完成的结果 |
|
352 | - */ |
|
353 | - public function multipartCancel(UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
354 | - { |
|
355 | - if (!$uploadOption->checkMutipartParas()) { |
|
356 | - return $this->errorResponse("MultipartCancelError", "multipart upload's parameters(id,uploadId) error"); |
|
357 | - } |
|
358 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_CANCEL; //取消分片任务的API |
|
359 | - $uploadOption->optionType = UpOptionType::BLOCK_CANCEL_UPLOAD; //取消分片任务时的Option类型 |
|
360 | - return $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
361 | - } |
|
347 | + /** |
|
348 | + * 取消分片上传任务。需要保证UploadOption中有分片任务的uploadId和id |
|
349 | + * @param UploadPolicy $uploadPolicy 上传策略 |
|
350 | + * @param UploadOption $uploadOption 上传选项 |
|
351 | + * @return array 分片上传完成的结果 |
|
352 | + */ |
|
353 | + public function multipartCancel(UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
354 | + { |
|
355 | + if (!$uploadOption->checkMutipartParas()) { |
|
356 | + return $this->errorResponse("MultipartCancelError", "multipart upload's parameters(id,uploadId) error"); |
|
357 | + } |
|
358 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_CANCEL; //取消分片任务的API |
|
359 | + $uploadOption->optionType = UpOptionType::BLOCK_CANCEL_UPLOAD; //取消分片任务时的Option类型 |
|
360 | + return $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
|
361 | + } |
|
362 | 362 | |
363 | - /** |
|
364 | - * 调用curl利用http上传数据 |
|
365 | - * @param string $method |
|
366 | - * @param string $url |
|
367 | - * @param UploadPolicy $uploadPolicy |
|
368 | - * @param UploadOption $uploadOption |
|
369 | - * @return array (isSuccess, ...) |
|
370 | - */ |
|
371 | - protected function sendRequest($method, $url, UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
372 | - { |
|
373 | - $success = false; |
|
374 | - $http_code = 0; |
|
375 | - $result = array(); |
|
376 | - /** |
|
377 | - * @var resource $ch |
|
378 | - */ |
|
379 | - $ch = curl_init(); |
|
380 | - try { |
|
381 | - //构建Http请求头和请求体 |
|
382 | - $_headers = array('Expect:'); |
|
383 | - $token = $this->_getUploadToken($uploadPolicy); |
|
384 | - array_push($_headers, "Authorization: {$token}"); |
|
385 | - array_push($_headers, "User-Agent: {$this->getUserAgent()}"); |
|
386 | - $length = 0; |
|
387 | - if (!empty($uploadOption)) { |
|
388 | - list($contentType, $httpBody) = $this->buildMultipartForm($uploadOption); |
|
389 | - $length = @strlen($httpBody); |
|
390 | - array_push($_headers, "Content-Type: {$contentType}"); |
|
391 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $httpBody); //请求体 |
|
392 | - } |
|
393 | - array_push($_headers, "Content-Length: {$length}"); |
|
394 | - curl_setopt($ch, CURLOPT_HEADER, 1); //设置头部 |
|
395 | - curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers); //请求头 |
|
396 | - curl_setopt($ch, CURLOPT_TIMEOUT, $uploadOption->timeout); //持续时长 |
|
397 | - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); //连接超时时长 |
|
398 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //成功,只返回结果,不自动输出任何内容。如果失败返回FALSE |
|
399 | - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); |
|
400 | - curl_setopt($ch, CURLOPT_URL, $url); |
|
401 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //自定义请求 |
|
402 | - //设置请求方式(GET或POST等) |
|
403 | - if ($method == 'PUT' || $method == 'POST') { |
|
404 | - curl_setopt($ch, CURLOPT_POST, 1); |
|
405 | - } else { |
|
406 | - curl_setopt($ch, CURLOPT_POST, 0); |
|
407 | - } |
|
408 | - //执行上传,然后获取服务端返回,决定是否重试 |
|
409 | - $response = curl_exec($ch); |
|
410 | - if ($response === false && $uploadOption->httpReTry != 0) { |
|
411 | - //如果执行curl失败,且需要重试,则进行重试 |
|
412 | - $this->recordCurlErrorLog($ch); //记录最近一次curl执行错误日志 |
|
413 | - $response = curl_exec($ch); |
|
414 | - } |
|
415 | - if ($response === false) { |
|
416 | - $this->recordCurlErrorLog($ch); //记录最近一次curl执行错误日志 |
|
417 | - $result = $this->errorResponse("curl error", "curl request failed"); |
|
418 | - $result['errno'] = curl_errno($ch); //错误码 |
|
419 | - } else { |
|
420 | - //解析返回结果,并判断是否上传成功 |
|
421 | - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
422 | - $success = ($http_code == 200) ? true : false; //判断是否上传成功 |
|
423 | - $resStr = explode("\r\n\r\n", $response, 2); |
|
424 | - $resBody = isset($resStr[1]) ? $resStr[1] : ''; |
|
425 | - $resArray = json_decode($resBody, true); //解析得到结果 |
|
426 | - $result = (empty($resArray) ? $result : $resArray); |
|
427 | - } |
|
428 | - } catch (Exception $e) { |
|
429 | - $result = $this->errorResponse("HTTPRequestException#" . $e->getLine(), $e->getMessage()); |
|
430 | - } |
|
363 | + /** |
|
364 | + * 调用curl利用http上传数据 |
|
365 | + * @param string $method |
|
366 | + * @param string $url |
|
367 | + * @param UploadPolicy $uploadPolicy |
|
368 | + * @param UploadOption $uploadOption |
|
369 | + * @return array (isSuccess, ...) |
|
370 | + */ |
|
371 | + protected function sendRequest($method, $url, UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
372 | + { |
|
373 | + $success = false; |
|
374 | + $http_code = 0; |
|
375 | + $result = array(); |
|
376 | + /** |
|
377 | + * @var resource $ch |
|
378 | + */ |
|
379 | + $ch = curl_init(); |
|
380 | + try { |
|
381 | + //构建Http请求头和请求体 |
|
382 | + $_headers = array('Expect:'); |
|
383 | + $token = $this->_getUploadToken($uploadPolicy); |
|
384 | + array_push($_headers, "Authorization: {$token}"); |
|
385 | + array_push($_headers, "User-Agent: {$this->getUserAgent()}"); |
|
386 | + $length = 0; |
|
387 | + if (!empty($uploadOption)) { |
|
388 | + list($contentType, $httpBody) = $this->buildMultipartForm($uploadOption); |
|
389 | + $length = @strlen($httpBody); |
|
390 | + array_push($_headers, "Content-Type: {$contentType}"); |
|
391 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $httpBody); //请求体 |
|
392 | + } |
|
393 | + array_push($_headers, "Content-Length: {$length}"); |
|
394 | + curl_setopt($ch, CURLOPT_HEADER, 1); //设置头部 |
|
395 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers); //请求头 |
|
396 | + curl_setopt($ch, CURLOPT_TIMEOUT, $uploadOption->timeout); //持续时长 |
|
397 | + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); //连接超时时长 |
|
398 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //成功,只返回结果,不自动输出任何内容。如果失败返回FALSE |
|
399 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); |
|
400 | + curl_setopt($ch, CURLOPT_URL, $url); |
|
401 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //自定义请求 |
|
402 | + //设置请求方式(GET或POST等) |
|
403 | + if ($method == 'PUT' || $method == 'POST') { |
|
404 | + curl_setopt($ch, CURLOPT_POST, 1); |
|
405 | + } else { |
|
406 | + curl_setopt($ch, CURLOPT_POST, 0); |
|
407 | + } |
|
408 | + //执行上传,然后获取服务端返回,决定是否重试 |
|
409 | + $response = curl_exec($ch); |
|
410 | + if ($response === false && $uploadOption->httpReTry != 0) { |
|
411 | + //如果执行curl失败,且需要重试,则进行重试 |
|
412 | + $this->recordCurlErrorLog($ch); //记录最近一次curl执行错误日志 |
|
413 | + $response = curl_exec($ch); |
|
414 | + } |
|
415 | + if ($response === false) { |
|
416 | + $this->recordCurlErrorLog($ch); //记录最近一次curl执行错误日志 |
|
417 | + $result = $this->errorResponse("curl error", "curl request failed"); |
|
418 | + $result['errno'] = curl_errno($ch); //错误码 |
|
419 | + } else { |
|
420 | + //解析返回结果,并判断是否上传成功 |
|
421 | + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
422 | + $success = ($http_code == 200) ? true : false; //判断是否上传成功 |
|
423 | + $resStr = explode("\r\n\r\n", $response, 2); |
|
424 | + $resBody = isset($resStr[1]) ? $resStr[1] : ''; |
|
425 | + $resArray = json_decode($resBody, true); //解析得到结果 |
|
426 | + $result = (empty($resArray) ? $result : $resArray); |
|
427 | + } |
|
428 | + } catch (Exception $e) { |
|
429 | + $result = $this->errorResponse("HTTPRequestException#" . $e->getLine(), $e->getMessage()); |
|
430 | + } |
|
431 | 431 | |
432 | - curl_close($ch); //PHP5.3中不支持finally关键字。因此,为了兼容,这里取消finally |
|
433 | - if (!$success) { |
|
434 | - throw new UploadException($result['message'], $http_code); |
|
435 | - } |
|
436 | - $result['isSuccess'] = $success; |
|
437 | - return $result; |
|
438 | - } |
|
432 | + curl_close($ch); //PHP5.3中不支持finally关键字。因此,为了兼容,这里取消finally |
|
433 | + if (!$success) { |
|
434 | + throw new UploadException($result['message'], $http_code); |
|
435 | + } |
|
436 | + $result['isSuccess'] = $success; |
|
437 | + return $result; |
|
438 | + } |
|
439 | 439 | |
440 | - /** |
|
441 | - * uploadPolicy和uploadOption 合法性检查 |
|
442 | - * @param UploadPolicy $uploadPolicy 上传策略 |
|
443 | - * @param UploadOption $uploadOption 上传选项 |
|
444 | - * @return array($isValid, $message) |
|
445 | - */ |
|
446 | - protected function checkUploadInfo(UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
447 | - { |
|
448 | - $isValid = true; |
|
449 | - $message = null; |
|
450 | - // 1:判断是否设置空间名 |
|
451 | - if (empty($uploadPolicy->bucket) && empty($uploadPolicy->namespace)) { |
|
452 | - $isValid = false; |
|
453 | - $message = 'namespace or bucket is empty'; |
|
454 | - } elseif (empty($uploadPolicy->name)) { |
|
455 | - // 2:优先使用uploadPolicy中的name,如果为空,则使用uploadOption中的name |
|
456 | - if (empty($uploadOption->name)) { |
|
457 | - $isValid = false; |
|
458 | - $message = "file's name is empty"; // 如果uploadPolicy和uploadOption中的文件名name都为空,则返回错误信息 |
|
459 | - } |
|
460 | - } |
|
461 | - if (true === $isValid) { |
|
462 | - // 3:优先使用uploadPolicy中的dir |
|
463 | - if (!empty($uploadPolicy->dir)) { |
|
464 | - if (strpos($uploadPolicy->dir, '/') !== 0) { |
|
465 | - $uploadPolicy->dir = '/' . $uploadPolicy->dir; //如果dir不为空,且其前面没有以"/"开头,则为其添加 |
|
466 | - } |
|
467 | - } |
|
468 | - // 4:如果uploadPolicy中的dir为空,则使用uploadOption中的dir |
|
469 | - if (!empty($uploadOption->dir)) { |
|
470 | - if (strpos($uploadOption->dir, '/') !== 0) { |
|
471 | - $uploadOption->dir = '/' . $uploadOption->dir; //如果dir不为空,且其前面没有以"/"开头,则为其添加 |
|
472 | - } |
|
473 | - } |
|
474 | - // 5:判断用户设置的文件分块大小,是否在指定的范围内。如果不在,则设置为默认Conf::BLOCK_DEFF_SIZE = 2M |
|
475 | - if (($uploadOption->blockSize > Conf::BLOCK_MAX_SIZE) || ($uploadOption->blockSize < Conf::BLOCK_MIN_SIZE)) { |
|
476 | - $uploadOption->blockSize = Conf::BLOCK_DEFF_SIZE; |
|
477 | - } |
|
478 | - } |
|
479 | - return array($isValid, $message); |
|
480 | - } |
|
440 | + /** |
|
441 | + * uploadPolicy和uploadOption 合法性检查 |
|
442 | + * @param UploadPolicy $uploadPolicy 上传策略 |
|
443 | + * @param UploadOption $uploadOption 上传选项 |
|
444 | + * @return array($isValid, $message) |
|
445 | + */ |
|
446 | + protected function checkUploadInfo(UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
|
447 | + { |
|
448 | + $isValid = true; |
|
449 | + $message = null; |
|
450 | + // 1:判断是否设置空间名 |
|
451 | + if (empty($uploadPolicy->bucket) && empty($uploadPolicy->namespace)) { |
|
452 | + $isValid = false; |
|
453 | + $message = 'namespace or bucket is empty'; |
|
454 | + } elseif (empty($uploadPolicy->name)) { |
|
455 | + // 2:优先使用uploadPolicy中的name,如果为空,则使用uploadOption中的name |
|
456 | + if (empty($uploadOption->name)) { |
|
457 | + $isValid = false; |
|
458 | + $message = "file's name is empty"; // 如果uploadPolicy和uploadOption中的文件名name都为空,则返回错误信息 |
|
459 | + } |
|
460 | + } |
|
461 | + if (true === $isValid) { |
|
462 | + // 3:优先使用uploadPolicy中的dir |
|
463 | + if (!empty($uploadPolicy->dir)) { |
|
464 | + if (strpos($uploadPolicy->dir, '/') !== 0) { |
|
465 | + $uploadPolicy->dir = '/' . $uploadPolicy->dir; //如果dir不为空,且其前面没有以"/"开头,则为其添加 |
|
466 | + } |
|
467 | + } |
|
468 | + // 4:如果uploadPolicy中的dir为空,则使用uploadOption中的dir |
|
469 | + if (!empty($uploadOption->dir)) { |
|
470 | + if (strpos($uploadOption->dir, '/') !== 0) { |
|
471 | + $uploadOption->dir = '/' . $uploadOption->dir; //如果dir不为空,且其前面没有以"/"开头,则为其添加 |
|
472 | + } |
|
473 | + } |
|
474 | + // 5:判断用户设置的文件分块大小,是否在指定的范围内。如果不在,则设置为默认Conf::BLOCK_DEFF_SIZE = 2M |
|
475 | + if (($uploadOption->blockSize > Conf::BLOCK_MAX_SIZE) || ($uploadOption->blockSize < Conf::BLOCK_MIN_SIZE)) { |
|
476 | + $uploadOption->blockSize = Conf::BLOCK_DEFF_SIZE; |
|
477 | + } |
|
478 | + } |
|
479 | + return array($isValid, $message); |
|
480 | + } |
|
481 | 481 | |
482 | - /** |
|
483 | - * 构建Http请求的Body |
|
484 | - * @param UploadOption $uploadOption |
|
485 | - * @return array (contentType,httpBody) |
|
486 | - */ |
|
487 | - protected function buildMultipartForm(UploadOption $uploadOption) |
|
488 | - { |
|
489 | - $bodyArray = array(); |
|
490 | - $mimeBoundary = md5(microtime()); |
|
491 | - $paraArray = $uploadOption->getParaArray(); |
|
492 | - foreach ($paraArray as $name => $val) { |
|
493 | - if ($name != 'content') { |
|
494 | - array_push($bodyArray, '--' . $mimeBoundary); |
|
495 | - array_push($bodyArray, "Content-Disposition: form-data; name=\"$name\""); |
|
496 | - array_push($bodyArray, 'Content-Type: text/plain; charset=UTF-8'); |
|
497 | - array_push($bodyArray, ''); |
|
498 | - array_push($bodyArray, $val); |
|
499 | - } |
|
500 | - } |
|
501 | - if (isset($paraArray['content'])) { |
|
502 | - array_push($bodyArray, '--' . $mimeBoundary); |
|
503 | - $fileName = empty($uploadOption->name) ? "temp" : $uploadOption->name; |
|
504 | - array_push($bodyArray, "Content-Disposition: form-data; name=\"content\"; filename=\"{$fileName}\""); |
|
505 | - array_push($bodyArray, "Content-Type: application/octet-stream"); |
|
506 | - array_push($bodyArray, ''); |
|
507 | - array_push($bodyArray, $paraArray['content']); |
|
508 | - } |
|
482 | + /** |
|
483 | + * 构建Http请求的Body |
|
484 | + * @param UploadOption $uploadOption |
|
485 | + * @return array (contentType,httpBody) |
|
486 | + */ |
|
487 | + protected function buildMultipartForm(UploadOption $uploadOption) |
|
488 | + { |
|
489 | + $bodyArray = array(); |
|
490 | + $mimeBoundary = md5(microtime()); |
|
491 | + $paraArray = $uploadOption->getParaArray(); |
|
492 | + foreach ($paraArray as $name => $val) { |
|
493 | + if ($name != 'content') { |
|
494 | + array_push($bodyArray, '--' . $mimeBoundary); |
|
495 | + array_push($bodyArray, "Content-Disposition: form-data; name=\"$name\""); |
|
496 | + array_push($bodyArray, 'Content-Type: text/plain; charset=UTF-8'); |
|
497 | + array_push($bodyArray, ''); |
|
498 | + array_push($bodyArray, $val); |
|
499 | + } |
|
500 | + } |
|
501 | + if (isset($paraArray['content'])) { |
|
502 | + array_push($bodyArray, '--' . $mimeBoundary); |
|
503 | + $fileName = empty($uploadOption->name) ? "temp" : $uploadOption->name; |
|
504 | + array_push($bodyArray, "Content-Disposition: form-data; name=\"content\"; filename=\"{$fileName}\""); |
|
505 | + array_push($bodyArray, "Content-Type: application/octet-stream"); |
|
506 | + array_push($bodyArray, ''); |
|
507 | + array_push($bodyArray, $paraArray['content']); |
|
508 | + } |
|
509 | 509 | |
510 | - array_push($bodyArray, '--' . $mimeBoundary . '--'); |
|
511 | - array_push($bodyArray, ''); |
|
510 | + array_push($bodyArray, '--' . $mimeBoundary . '--'); |
|
511 | + array_push($bodyArray, ''); |
|
512 | 512 | |
513 | - $httpBody = implode("\r\n", $bodyArray); |
|
514 | - $contentType = 'multipart/form-data; boundary=' . $mimeBoundary; |
|
515 | - return array( |
|
516 | - $contentType, |
|
517 | - $httpBody |
|
518 | - ); |
|
519 | - } |
|
513 | + $httpBody = implode("\r\n", $bodyArray); |
|
514 | + $contentType = 'multipart/form-data; boundary=' . $mimeBoundary; |
|
515 | + return array( |
|
516 | + $contentType, |
|
517 | + $httpBody |
|
518 | + ); |
|
519 | + } |
|
520 | 520 | |
521 | - /** |
|
522 | - * UserAgent用户代理 |
|
523 | - */ |
|
524 | - protected function getUserAgent() |
|
525 | - { |
|
526 | - if ($this->type == "TOP") { |
|
527 | - return "ALIMEDIASDK_PHP_TAE/" . Conf::SDK_VERSION; |
|
528 | - } else { |
|
529 | - return "ALIMEDIASDK_PHP_CLOUD/" . Conf::SDK_VERSION; |
|
530 | - } |
|
531 | - } |
|
521 | + /** |
|
522 | + * UserAgent用户代理 |
|
523 | + */ |
|
524 | + protected function getUserAgent() |
|
525 | + { |
|
526 | + if ($this->type == "TOP") { |
|
527 | + return "ALIMEDIASDK_PHP_TAE/" . Conf::SDK_VERSION; |
|
528 | + } else { |
|
529 | + return "ALIMEDIASDK_PHP_CLOUD/" . Conf::SDK_VERSION; |
|
530 | + } |
|
531 | + } |
|
532 | 532 | |
533 | - /** |
|
534 | - * 生成上传凭证 |
|
535 | - * @param UploadPolicy $uploadPolicy |
|
536 | - * @return string 上传时的凭证token |
|
537 | - */ |
|
538 | - public function getUploadToken(UploadPolicy $uploadPolicy) |
|
539 | - { |
|
540 | - $encodedPolicy = EncodeUtils::encodeWithURLSafeBase64(json_encode($uploadPolicy->toArray())); |
|
541 | - $signed = hash_hmac('sha1', $encodedPolicy, $this->sk); |
|
542 | - $tempStr = $this->ak . ":" . $encodedPolicy . ":" . $signed; |
|
543 | - $token = "UPLOAD_AK_" . $this->type . " " . EncodeUtils::encodeWithURLSafeBase64($tempStr); |
|
533 | + /** |
|
534 | + * 生成上传凭证 |
|
535 | + * @param UploadPolicy $uploadPolicy |
|
536 | + * @return string 上传时的凭证token |
|
537 | + */ |
|
538 | + public function getUploadToken(UploadPolicy $uploadPolicy) |
|
539 | + { |
|
540 | + $encodedPolicy = EncodeUtils::encodeWithURLSafeBase64(json_encode($uploadPolicy->toArray())); |
|
541 | + $signed = hash_hmac('sha1', $encodedPolicy, $this->sk); |
|
542 | + $tempStr = $this->ak . ":" . $encodedPolicy . ":" . $signed; |
|
543 | + $token = "UPLOAD_AK_" . $this->type . " " . EncodeUtils::encodeWithURLSafeBase64($tempStr); |
|
544 | 544 | |
545 | - return $token; |
|
546 | - } |
|
545 | + return $token; |
|
546 | + } |
|
547 | 547 | |
548 | - /** |
|
549 | - * 反馈错误信息 |
|
550 | - */ |
|
551 | - protected function errorResponse($code = "UnknownError", $message = "unkonown error", $requestId = null) |
|
552 | - { |
|
553 | - return array( |
|
554 | - "isSuccess" => false, |
|
555 | - "code" => $code, |
|
556 | - "message" => $message, |
|
557 | - "requestId" => $requestId |
|
558 | - ); |
|
559 | - } |
|
548 | + /** |
|
549 | + * 反馈错误信息 |
|
550 | + */ |
|
551 | + protected function errorResponse($code = "UnknownError", $message = "unkonown error", $requestId = null) |
|
552 | + { |
|
553 | + return array( |
|
554 | + "isSuccess" => false, |
|
555 | + "code" => $code, |
|
556 | + "message" => $message, |
|
557 | + "requestId" => $requestId |
|
558 | + ); |
|
559 | + } |
|
560 | 560 | |
561 | - /** |
|
562 | - * 记录curl错误日志 |
|
563 | - * @param curl-handle $ch curl句柄 |
|
564 | - */ |
|
565 | - protected function recordCurlErrorLog($ch) |
|
566 | - { |
|
567 | - if (AlibabaImage::$run_level == Conf::RUN_LEVEL_DEBUG) { |
|
568 | - $errno = curl_errno($ch); //错误码 |
|
569 | - $info = curl_getinfo($ch); //curl连接资源句柄的信息 |
|
570 | - $info['errno'] = $errno; //添加到连接句柄信息中 |
|
571 | - $content = date("Y-m-d H:i:s") . json_encode($info) . "\n"; |
|
572 | - $logPath = dirname(__FILE__) . "/" . Conf::CURL_ERR_LOG; |
|
573 | - $this->putContentToFile($content, $logPath); |
|
574 | - } |
|
575 | - } |
|
561 | + /** |
|
562 | + * 记录curl错误日志 |
|
563 | + * @param curl-handle $ch curl句柄 |
|
564 | + */ |
|
565 | + protected function recordCurlErrorLog($ch) |
|
566 | + { |
|
567 | + if (AlibabaImage::$run_level == Conf::RUN_LEVEL_DEBUG) { |
|
568 | + $errno = curl_errno($ch); //错误码 |
|
569 | + $info = curl_getinfo($ch); //curl连接资源句柄的信息 |
|
570 | + $info['errno'] = $errno; //添加到连接句柄信息中 |
|
571 | + $content = date("Y-m-d H:i:s") . json_encode($info) . "\n"; |
|
572 | + $logPath = dirname(__FILE__) . "/" . Conf::CURL_ERR_LOG; |
|
573 | + $this->putContentToFile($content, $logPath); |
|
574 | + } |
|
575 | + } |
|
576 | 576 | |
577 | - /** |
|
578 | - * 将信息追加写到文件 |
|
579 | - * @param string $message 日志内容。 |
|
580 | - * @return void |
|
581 | - */ |
|
582 | - protected function putContentToFile($content, $filepath) |
|
583 | - { |
|
584 | - if (!file_exists($filepath)) { |
|
585 | - $handle = fopen($filepath, 'w'); |
|
586 | - fclose($handle); |
|
587 | - } |
|
588 | - file_put_contents($filepath, $content, FILE_APPEND); |
|
589 | - } |
|
577 | + /** |
|
578 | + * 将信息追加写到文件 |
|
579 | + * @param string $message 日志内容。 |
|
580 | + * @return void |
|
581 | + */ |
|
582 | + protected function putContentToFile($content, $filepath) |
|
583 | + { |
|
584 | + if (!file_exists($filepath)) { |
|
585 | + $handle = fopen($filepath, 'w'); |
|
586 | + fclose($handle); |
|
587 | + } |
|
588 | + file_put_contents($filepath, $content, FILE_APPEND); |
|
589 | + } |
|
590 | 590 | } |
@@ -38,10 +38,10 @@ discard block |
||
38 | 38 | return $this->errorResponse("FileNotExist", "file not exist"); |
39 | 39 | } |
40 | 40 | if (empty($uploadOption)) { |
41 | - $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
41 | + $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
42 | 42 | } |
43 | 43 | if (empty($uploadOption->name)) { |
44 | - $uploadOption->name = basename($filePath); //如果用户没有设置name属性,则使用上传时的文件名 |
|
44 | + $uploadOption->name = basename($filePath); //如果用户没有设置name属性,则使用上传时的文件名 |
|
45 | 45 | } |
46 | 46 | // UploadPolicy 和 UploadOption检查 |
47 | 47 | list($isValid, $message) = $this->checkUploadInfo($uploadPolicy, $uploadOption); |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | { |
69 | 69 | $data = file_get_contents($filePath); |
70 | 70 | $uploadOption->setContent($data); |
71 | - $url = $this->upload_host . Conf::UPLOAD_API_UPLOAD; //普通上传的API |
|
71 | + $url = $this->upload_host . Conf::UPLOAD_API_UPLOAD; //普通上传的API |
|
72 | 72 | return $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
73 | 73 | } |
74 | 74 | |
@@ -81,8 +81,8 @@ discard block |
||
81 | 81 | */ |
82 | 82 | protected function uploadSuperFile($filePath, UploadPolicy $uploadPolicy, UploadOption $uploadOption = null) |
83 | 83 | { |
84 | - $fileSize = filesize($filePath); // 文件大小 |
|
85 | - $blockSize = $uploadOption->blockSize; // 文件分片大小 |
|
84 | + $fileSize = filesize($filePath); // 文件大小 |
|
85 | + $blockSize = $uploadOption->blockSize; // 文件分片大小 |
|
86 | 86 | $blockNum = intval(ceil($fileSize / $blockSize)); // 文件分片后的块数 |
87 | 87 | for ($i = 0; $i < $blockNum; $i++) { |
88 | 88 | $currentSize = $blockSize; // 当前文件块的大小 |
@@ -95,8 +95,8 @@ discard block |
||
95 | 95 | $httpRes = null; |
96 | 96 | if (0 == $i) { |
97 | 97 | // 分片初始化阶段 |
98 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_INIT; //初始化分片上传的API |
|
99 | - $uploadOption->optionType = UpOptionType::BLOCK_INIT_UPLOAD; //初始化分片时的Option类型 |
|
98 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_INIT; //初始化分片上传的API |
|
99 | + $uploadOption->optionType = UpOptionType::BLOCK_INIT_UPLOAD; //初始化分片时的Option类型 |
|
100 | 100 | $httpRes = $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
101 | 101 | $uploadId = isset($httpRes['uploadId']) ? $httpRes['uploadId'] : null; // 分片上传ID(OSS用于区分上传的id) |
102 | 102 | $id = isset($httpRes['id']) ? $httpRes['id'] : null; // 上传唯一ID(多媒体服务用于区分上传的id) |
@@ -104,16 +104,16 @@ discard block |
||
104 | 104 | $uploadOption->setUniqueIdId($id); |
105 | 105 | } else { |
106 | 106 | // 分片上传过程中 |
107 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_UPLOAD; //分片上传过程中的API |
|
108 | - $uploadOption->optionType = UpOptionType::BLOCK_RUN_UPLOAD; //分片上传过程中的Option类型 |
|
109 | - $uploadOption->setPartNumber($i + 1); // |
|
107 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_UPLOAD; //分片上传过程中的API |
|
108 | + $uploadOption->optionType = UpOptionType::BLOCK_RUN_UPLOAD; //分片上传过程中的Option类型 |
|
109 | + $uploadOption->setPartNumber($i + 1); // |
|
110 | 110 | $httpRes = $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
111 | 111 | } |
112 | 112 | // 如果分片上传失败,则取消cancel分片上传任务,然后返回错误信息 |
113 | 113 | if (!$httpRes['isSuccess']) { |
114 | 114 | if ($uploadOption->checkMutipartParas()) { |
115 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_CANCEL; //取消分片任务的API |
|
116 | - $uploadOption->optionType = UpOptionType::BLOCK_CANCEL_UPLOAD; //取消分片任务时的Option类型 |
|
115 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_CANCEL; //取消分片任务的API |
|
116 | + $uploadOption->optionType = UpOptionType::BLOCK_CANCEL_UPLOAD; //取消分片任务时的Option类型 |
|
117 | 117 | $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); // 不判断取消分片任务返回的结果 |
118 | 118 | } |
119 | 119 | return $httpRes; |
@@ -126,9 +126,9 @@ discard block |
||
126 | 126 | $uploadOption->addPartNumberAndETag($httpRes['partNumber'], $httpRes['eTag']); |
127 | 127 | } |
128 | 128 | // 分片上传完成 |
129 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_COMPLETE; //完成分片上传任务的API |
|
130 | - $uploadOption->optionType = UpOptionType::BLOCK_COMPLETE_UPLOAD; //完成分片上传任务时的Option类型 |
|
131 | - $uploadOption->setMd5(md5_file($filePath)); //文件Md5 |
|
129 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_COMPLETE; //完成分片上传任务的API |
|
130 | + $uploadOption->optionType = UpOptionType::BLOCK_COMPLETE_UPLOAD; //完成分片上传任务时的Option类型 |
|
131 | + $uploadOption->setMd5(md5_file($filePath)); //文件Md5 |
|
132 | 132 | return $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
133 | 133 | } |
134 | 134 | |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | public function uploadData($data, UploadPolicy $uploadPolicy, UploadOption $uploadOption = null) |
145 | 145 | { |
146 | 146 | if (empty($uploadOption)) { |
147 | - $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
147 | + $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
148 | 148 | } |
149 | 149 | // UploadPolicy 和 UploadOption检查 |
150 | 150 | list($isValid, $message) = $this->checkUploadInfo($uploadPolicy, $uploadOption); |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | } |
159 | 159 | // 文件不大于设定的分片大小(默认2M),则直接上传uploadMiniFile() |
160 | 160 | $uploadOption->setContent($data); |
161 | - $url = $this->upload_host . Conf::UPLOAD_API_UPLOAD; //普通上传的API |
|
161 | + $url = $this->upload_host . Conf::UPLOAD_API_UPLOAD; //普通上传的API |
|
162 | 162 | return $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
163 | 163 | } |
164 | 164 | |
@@ -171,8 +171,8 @@ discard block |
||
171 | 171 | */ |
172 | 172 | protected function uploadSuperData($data, UploadPolicy $uploadPolicy, UploadOption $uploadOption = null) |
173 | 173 | { |
174 | - $dataSize = strlen($data); // 文件大小 |
|
175 | - $blockSize = $uploadOption->blockSize; // 文件分片大小 |
|
174 | + $dataSize = strlen($data); // 文件大小 |
|
175 | + $blockSize = $uploadOption->blockSize; // 文件分片大小 |
|
176 | 176 | $blockNum = intval(ceil($dataSize / $blockSize)); // 文件分片后的块数 |
177 | 177 | for ($i = 0; $i < $blockNum; $i++) { |
178 | 178 | $currentSize = $blockSize; // 当前文件块的大小 |
@@ -185,8 +185,8 @@ discard block |
||
185 | 185 | $httpRes = null; |
186 | 186 | if (0 == $i) { |
187 | 187 | // 分片初始化阶段 |
188 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_INIT; //初始化分片上传的API |
|
189 | - $uploadOption->optionType = UpOptionType::BLOCK_INIT_UPLOAD; //初始化分片时的Option类型 |
|
188 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_INIT; //初始化分片上传的API |
|
189 | + $uploadOption->optionType = UpOptionType::BLOCK_INIT_UPLOAD; //初始化分片时的Option类型 |
|
190 | 190 | $httpRes = $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
191 | 191 | $uploadId = isset($httpRes['uploadId']) ? $httpRes['uploadId'] : null; // 分片上传ID(OSS用于区分上传的id) |
192 | 192 | $id = isset($httpRes['id']) ? $httpRes['id'] : null; // 上传唯一ID(多媒体服务用于区分上传的id) |
@@ -194,16 +194,16 @@ discard block |
||
194 | 194 | $uploadOption->setUniqueIdId($id); |
195 | 195 | } else { |
196 | 196 | // 分片上传过程中 |
197 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_UPLOAD; //分片上传过程中的API |
|
198 | - $uploadOption->optionType = UpOptionType::BLOCK_RUN_UPLOAD; //分片上传过程中的Option类型 |
|
199 | - $uploadOption->setPartNumber($i + 1); // |
|
197 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_UPLOAD; //分片上传过程中的API |
|
198 | + $uploadOption->optionType = UpOptionType::BLOCK_RUN_UPLOAD; //分片上传过程中的Option类型 |
|
199 | + $uploadOption->setPartNumber($i + 1); // |
|
200 | 200 | $httpRes = $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
201 | 201 | } |
202 | 202 | // 如果分片上传失败,则取消cancel分片上传任务,然后返回错误信息 |
203 | 203 | if (!$httpRes['isSuccess']) { |
204 | 204 | if ($uploadOption->checkMutipartParas()) { |
205 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_CANCEL; //取消分片任务的API |
|
206 | - $uploadOption->optionType = UpOptionType::BLOCK_CANCEL_UPLOAD; //取消分片任务时的Option类型 |
|
205 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_CANCEL; //取消分片任务的API |
|
206 | + $uploadOption->optionType = UpOptionType::BLOCK_CANCEL_UPLOAD; //取消分片任务时的Option类型 |
|
207 | 207 | $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); // 不判断取消分片任务返回的结果 |
208 | 208 | } |
209 | 209 | return $httpRes; |
@@ -212,9 +212,9 @@ discard block |
||
212 | 212 | $uploadOption->addPartNumberAndETag($httpRes['partNumber'], $httpRes['eTag']); |
213 | 213 | } |
214 | 214 | // 分片上传完成 |
215 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_COMPLETE; //完成分片上传任务的API |
|
216 | - $uploadOption->optionType = UpOptionType::BLOCK_COMPLETE_UPLOAD; //完成分片上传任务时的Option类型 |
|
217 | - $uploadOption->setMd5(md5($data)); //文件Md5 |
|
215 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_COMPLETE; //完成分片上传任务的API |
|
216 | + $uploadOption->optionType = UpOptionType::BLOCK_COMPLETE_UPLOAD; //完成分片上传任务时的Option类型 |
|
217 | + $uploadOption->setMd5(md5($data)); //文件Md5 |
|
218 | 218 | return $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
219 | 219 | } |
220 | 220 | |
@@ -232,10 +232,10 @@ discard block |
||
232 | 232 | return $this->errorResponse("FileNotExist", "file not exist"); |
233 | 233 | } |
234 | 234 | if (empty($uploadOption)) { |
235 | - $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
235 | + $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
236 | 236 | } |
237 | 237 | if (empty($uploadOption->name)) { |
238 | - $uploadOption->name = basename($filePath); //如果用户没有设置name属性,则使用上传时的文件名 |
|
238 | + $uploadOption->name = basename($filePath); //如果用户没有设置name属性,则使用上传时的文件名 |
|
239 | 239 | } |
240 | 240 | $blockData = file_get_contents($encodePath, 0, null, 0, $uploadOption->blockSize); |
241 | 241 | return $this->multipartInitByData($blockData, $uploadPolicy, $uploadOption); |
@@ -251,7 +251,7 @@ discard block |
||
251 | 251 | public function multipartInitByData($blockData, UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
252 | 252 | { |
253 | 253 | if (empty($uploadOption)) { |
254 | - $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
254 | + $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
255 | 255 | } |
256 | 256 | // UploadPolicy 和 UploadOption检查 |
257 | 257 | list($isValid, $message) = $this->checkUploadInfo($uploadPolicy, $uploadOption); |
@@ -259,13 +259,13 @@ discard block |
||
259 | 259 | return $this->errorResponse("ErrorUploadInfo", "error upload policy or option:" . $message); |
260 | 260 | } |
261 | 261 | // 数据大小不等于设定的分片大小(默认2M),则无法完成初始化 |
262 | - $dataSize = strlen($blockData); // 数据文件大小 |
|
262 | + $dataSize = strlen($blockData); // 数据文件大小 |
|
263 | 263 | if ($dataSize != ($uploadOption->blockSize)) { |
264 | 264 | return $this->errorResponse("MultipartInitError", "UploadOption's blockSize is not equal to data's size"); |
265 | 265 | } |
266 | 266 | $uploadOption->setContent($blockData); // 设置待上传的文件块 |
267 | - $uploadOption->optionType = UpOptionType::BLOCK_INIT_UPLOAD; //初始化分片时的Option类型 |
|
268 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_INIT; //初始化分片上传的API |
|
267 | + $uploadOption->optionType = UpOptionType::BLOCK_INIT_UPLOAD; //初始化分片时的Option类型 |
|
268 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_INIT; //初始化分片上传的API |
|
269 | 269 | $httpRes = $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
270 | 270 | //若成功返回,则保存初始化成功的uploadId、id 以及 partNumber、eTag |
271 | 271 | $uploadId = isset($httpRes['uploadId']) ? $httpRes['uploadId'] : null; // 分片上传ID(OSS用于区分上传的id) |
@@ -290,8 +290,8 @@ discard block |
||
290 | 290 | if (!file_exists($encodePath)) { |
291 | 291 | return $this->errorResponse("FileNotExist", "file not exist"); |
292 | 292 | } |
293 | - $fileSize = filesize($encodePath); // 文件大小 |
|
294 | - $blockSize = $uploadOption->blockSize; // 文件分片大小 |
|
293 | + $fileSize = filesize($encodePath); // 文件大小 |
|
294 | + $blockSize = $uploadOption->blockSize; // 文件分片大小 |
|
295 | 295 | $blockNum = intval(ceil($fileSize / $blockSize)); // 文件分片后的块数 |
296 | 296 | $currentSize = $blockSize; // 当前文件块的大小 |
297 | 297 | if ($uploadOption->getPartNumber() == $blockNum) { |
@@ -317,8 +317,8 @@ discard block |
||
317 | 317 | return $this->errorResponse("MultipartUploadError", "multipart upload's parameters(id,uploadId,partNumber) error"); |
318 | 318 | } |
319 | 319 | $uploadOption->setContent($blockData); // 设置待上传的文件块 |
320 | - $uploadOption->optionType = UpOptionType::BLOCK_RUN_UPLOAD; //分片上传过程中的Option类型 |
|
321 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_UPLOAD; //分片上传过程中的API |
|
320 | + $uploadOption->optionType = UpOptionType::BLOCK_RUN_UPLOAD; //分片上传过程中的Option类型 |
|
321 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_UPLOAD; //分片上传过程中的API |
|
322 | 322 | $httpRes = $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
323 | 323 | if (isset($httpRes['partNumber']) && isset($httpRes['eTag'])) { |
324 | 324 | $uploadOption->addPartNumberAndETag($httpRes['partNumber'], $httpRes['eTag']); |
@@ -339,8 +339,8 @@ discard block |
||
339 | 339 | if (!$uploadOption->checkMutipartParas() || empty($fileMd5)) { |
340 | 340 | return $this->errorResponse("MultipartCompleteError", "multipart upload's parameters(id,uploadId,md5) error"); |
341 | 341 | } |
342 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_COMPLETE; //完成分片上传任务的API |
|
343 | - $uploadOption->optionType = UpOptionType::BLOCK_COMPLETE_UPLOAD; //完成分片上传任务时的Option类型 |
|
342 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_COMPLETE; //完成分片上传任务的API |
|
343 | + $uploadOption->optionType = UpOptionType::BLOCK_COMPLETE_UPLOAD; //完成分片上传任务时的Option类型 |
|
344 | 344 | return $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
345 | 345 | } |
346 | 346 | |
@@ -355,8 +355,8 @@ discard block |
||
355 | 355 | if (!$uploadOption->checkMutipartParas()) { |
356 | 356 | return $this->errorResponse("MultipartCancelError", "multipart upload's parameters(id,uploadId) error"); |
357 | 357 | } |
358 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_CANCEL; //取消分片任务的API |
|
359 | - $uploadOption->optionType = UpOptionType::BLOCK_CANCEL_UPLOAD; //取消分片任务时的Option类型 |
|
358 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_CANCEL; //取消分片任务的API |
|
359 | + $uploadOption->optionType = UpOptionType::BLOCK_CANCEL_UPLOAD; //取消分片任务时的Option类型 |
|
360 | 360 | return $this->sendRequest('POST', $url, $uploadPolicy, $uploadOption); |
361 | 361 | } |
362 | 362 | |
@@ -388,17 +388,17 @@ discard block |
||
388 | 388 | list($contentType, $httpBody) = $this->buildMultipartForm($uploadOption); |
389 | 389 | $length = @strlen($httpBody); |
390 | 390 | array_push($_headers, "Content-Type: {$contentType}"); |
391 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $httpBody); //请求体 |
|
391 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $httpBody); //请求体 |
|
392 | 392 | } |
393 | 393 | array_push($_headers, "Content-Length: {$length}"); |
394 | - curl_setopt($ch, CURLOPT_HEADER, 1); //设置头部 |
|
395 | - curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers); //请求头 |
|
396 | - curl_setopt($ch, CURLOPT_TIMEOUT, $uploadOption->timeout); //持续时长 |
|
397 | - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); //连接超时时长 |
|
394 | + curl_setopt($ch, CURLOPT_HEADER, 1); //设置头部 |
|
395 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers); //请求头 |
|
396 | + curl_setopt($ch, CURLOPT_TIMEOUT, $uploadOption->timeout); //持续时长 |
|
397 | + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); //连接超时时长 |
|
398 | 398 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //成功,只返回结果,不自动输出任何内容。如果失败返回FALSE |
399 | 399 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); |
400 | 400 | curl_setopt($ch, CURLOPT_URL, $url); |
401 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //自定义请求 |
|
401 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //自定义请求 |
|
402 | 402 | //设置请求方式(GET或POST等) |
403 | 403 | if ($method == 'PUT' || $method == 'POST') { |
404 | 404 | curl_setopt($ch, CURLOPT_POST, 1); |
@@ -419,10 +419,10 @@ discard block |
||
419 | 419 | } else { |
420 | 420 | //解析返回结果,并判断是否上传成功 |
421 | 421 | $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
422 | - $success = ($http_code == 200) ? true : false; //判断是否上传成功 |
|
422 | + $success = ($http_code == 200) ? true : false; //判断是否上传成功 |
|
423 | 423 | $resStr = explode("\r\n\r\n", $response, 2); |
424 | 424 | $resBody = isset($resStr[1]) ? $resStr[1] : ''; |
425 | - $resArray = json_decode($resBody, true); //解析得到结果 |
|
425 | + $resArray = json_decode($resBody, true); //解析得到结果 |
|
426 | 426 | $result = (empty($resArray) ? $result : $resArray); |
427 | 427 | } |
428 | 428 | } catch (Exception $e) { |
@@ -10,461 +10,461 @@ |
||
10 | 10 | |
11 | 11 | class ManageClient |
12 | 12 | { |
13 | - private $manage_host; |
|
14 | - private $ak; |
|
15 | - private $sk; |
|
16 | - private $type; |
|
17 | - public function __construct($ak, $sk, $type = Conf::TYPE_TOP) |
|
18 | - { |
|
19 | - $this->ak = $ak; |
|
20 | - $this->sk = $sk; |
|
21 | - $this->type = $type; |
|
22 | - $this->manage_host = Conf::MANAGE_HOST_MEDIA; |
|
23 | - } |
|
13 | + private $manage_host; |
|
14 | + private $ak; |
|
15 | + private $sk; |
|
16 | + private $type; |
|
17 | + public function __construct($ak, $sk, $type = Conf::TYPE_TOP) |
|
18 | + { |
|
19 | + $this->ak = $ak; |
|
20 | + $this->sk = $sk; |
|
21 | + $this->type = $type; |
|
22 | + $this->manage_host = Conf::MANAGE_HOST_MEDIA; |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * 文件是否存在 |
|
27 | - * @param string $namespace 空间名,必须 |
|
28 | - * @param string $dir 路径 |
|
29 | - * @param string $filename 文件名 |
|
30 | - * @return array |
|
31 | - */ |
|
32 | - public function existsFile($namespace, $dir, $filename) |
|
33 | - { |
|
34 | - $resourceInfo = new ResourceInfo($namespace, $dir, $filename); |
|
35 | - list($isValid, $message) = $resourceInfo->checkResourceInfo(true, true); |
|
36 | - if (!$isValid) { |
|
37 | - return $this->errorResponse("InvalidArgument", $message); |
|
38 | - } |
|
39 | - $resourceId = $resourceInfo->buildResourceId(); //得到资源ID |
|
40 | - $uri = '/' . Conf::MANAGE_API_VERSION . '/files/' . $resourceId . '/exist'; |
|
41 | - return $this->sendRequest('GET', $uri); |
|
42 | - } |
|
25 | + /** |
|
26 | + * 文件是否存在 |
|
27 | + * @param string $namespace 空间名,必须 |
|
28 | + * @param string $dir 路径 |
|
29 | + * @param string $filename 文件名 |
|
30 | + * @return array |
|
31 | + */ |
|
32 | + public function existsFile($namespace, $dir, $filename) |
|
33 | + { |
|
34 | + $resourceInfo = new ResourceInfo($namespace, $dir, $filename); |
|
35 | + list($isValid, $message) = $resourceInfo->checkResourceInfo(true, true); |
|
36 | + if (!$isValid) { |
|
37 | + return $this->errorResponse("InvalidArgument", $message); |
|
38 | + } |
|
39 | + $resourceId = $resourceInfo->buildResourceId(); //得到资源ID |
|
40 | + $uri = '/' . Conf::MANAGE_API_VERSION . '/files/' . $resourceId . '/exist'; |
|
41 | + return $this->sendRequest('GET', $uri); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * 获取文件的元信息(meta信息) |
|
46 | - * @param string $namespace 空间名,必须 |
|
47 | - * @param string $dir 路径 |
|
48 | - * @param string $filename 文件名 |
|
49 | - * @return array |
|
50 | - */ |
|
51 | - public function getFileInfo($namespace, $dir, $filename) |
|
52 | - { |
|
53 | - $resourceInfo = new ResourceInfo($namespace, $dir, $filename); |
|
54 | - list($isValid, $message) = $resourceInfo->checkResourceInfo(true, true); |
|
55 | - if (!$isValid) { |
|
56 | - return $this->errorResponse("InvalidArgument", $message); |
|
57 | - } |
|
58 | - $resourceId = $resourceInfo->buildResourceId(); //得到资源ID |
|
59 | - $uri = '/' . Conf::MANAGE_API_VERSION . '/files/' . $resourceId; |
|
60 | - return $this->sendRequest('GET', $uri); |
|
61 | - } |
|
44 | + /** |
|
45 | + * 获取文件的元信息(meta信息) |
|
46 | + * @param string $namespace 空间名,必须 |
|
47 | + * @param string $dir 路径 |
|
48 | + * @param string $filename 文件名 |
|
49 | + * @return array |
|
50 | + */ |
|
51 | + public function getFileInfo($namespace, $dir, $filename) |
|
52 | + { |
|
53 | + $resourceInfo = new ResourceInfo($namespace, $dir, $filename); |
|
54 | + list($isValid, $message) = $resourceInfo->checkResourceInfo(true, true); |
|
55 | + if (!$isValid) { |
|
56 | + return $this->errorResponse("InvalidArgument", $message); |
|
57 | + } |
|
58 | + $resourceId = $resourceInfo->buildResourceId(); //得到资源ID |
|
59 | + $uri = '/' . Conf::MANAGE_API_VERSION . '/files/' . $resourceId; |
|
60 | + return $this->sendRequest('GET', $uri); |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * 重命名文件 |
|
65 | - * @param string $namespace 空间名,必须 |
|
66 | - * @param string $dir 路径 |
|
67 | - * @param string $filename 文件名 |
|
68 | - * @param string $newDir 新的路径 |
|
69 | - * @param string $newName 新的文件名 |
|
70 | - * @return array |
|
71 | - */ |
|
72 | - public function renameFile($namespace, $dir, $filename, $newDir, $newName) |
|
73 | - { |
|
74 | - $resourceInfo = new ResourceInfo($namespace, $dir, $filename); //老的资源 |
|
75 | - list($isValid, $message) = $resourceInfo->checkResourceInfo(true, true); |
|
76 | - if (!$isValid) { |
|
77 | - return $this->errorResponse("InvalidArgument", $message); |
|
78 | - } |
|
79 | - $newResourceInfo = new ResourceInfo($namespace, $newDir, $newName); //新的资源 |
|
80 | - list($isValid, $message) = $newResourceInfo->checkResourceInfo(true, true); |
|
81 | - if (!$isValid) { |
|
82 | - return $this->errorResponse("InvalidArgument", $message); |
|
83 | - } |
|
84 | - $resourceId = $resourceInfo->buildResourceId(); //老资源ID |
|
85 | - $newResourceId = $newResourceInfo->buildResourceId(); //新资源ID |
|
86 | - $uri = '/' . Conf::MANAGE_API_VERSION . '/files/' . $resourceId . "/rename/" . $newResourceId; |
|
87 | - return $this->sendRequest('POST', $uri); |
|
88 | - } |
|
63 | + /** |
|
64 | + * 重命名文件 |
|
65 | + * @param string $namespace 空间名,必须 |
|
66 | + * @param string $dir 路径 |
|
67 | + * @param string $filename 文件名 |
|
68 | + * @param string $newDir 新的路径 |
|
69 | + * @param string $newName 新的文件名 |
|
70 | + * @return array |
|
71 | + */ |
|
72 | + public function renameFile($namespace, $dir, $filename, $newDir, $newName) |
|
73 | + { |
|
74 | + $resourceInfo = new ResourceInfo($namespace, $dir, $filename); //老的资源 |
|
75 | + list($isValid, $message) = $resourceInfo->checkResourceInfo(true, true); |
|
76 | + if (!$isValid) { |
|
77 | + return $this->errorResponse("InvalidArgument", $message); |
|
78 | + } |
|
79 | + $newResourceInfo = new ResourceInfo($namespace, $newDir, $newName); //新的资源 |
|
80 | + list($isValid, $message) = $newResourceInfo->checkResourceInfo(true, true); |
|
81 | + if (!$isValid) { |
|
82 | + return $this->errorResponse("InvalidArgument", $message); |
|
83 | + } |
|
84 | + $resourceId = $resourceInfo->buildResourceId(); //老资源ID |
|
85 | + $newResourceId = $newResourceInfo->buildResourceId(); //新资源ID |
|
86 | + $uri = '/' . Conf::MANAGE_API_VERSION . '/files/' . $resourceId . "/rename/" . $newResourceId; |
|
87 | + return $this->sendRequest('POST', $uri); |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * 获取指定目录下的文件列表 |
|
92 | - * @param string $namespace 空间名,必须 |
|
93 | - * @param string $dir 路径 |
|
94 | - * @param number $page 页数 |
|
95 | - * @param number $pageSize 每页显示的记录数 |
|
96 | - */ |
|
97 | - public function listFiles($namespace, $dir, $page = 1, $pageSize = 100) |
|
98 | - { |
|
99 | - $manageOption = new ManageOption($namespace); |
|
100 | - $manageOption->setDir($dir)->setCurrentPage($page)->setPageSize($pageSize); |
|
101 | - list($isValid, $message) = $manageOption->checkResourceInfo(true); |
|
102 | - if ($page <= 0 || $pageSize <= 0) { |
|
103 | - $isValid = false; |
|
104 | - $message = "Invalid parameters page or pageSize"; |
|
105 | - } |
|
106 | - if (!$isValid) { |
|
107 | - return $this->errorResponse("InvalidArgument", $message); |
|
108 | - } |
|
109 | - $queryParas = $manageOption->buildListFilesParas(); //查询query参数 |
|
110 | - $uri = '/' . Conf::MANAGE_API_VERSION . '/files?' . $queryParas; |
|
111 | - return $this->sendRequest('GET', $uri); |
|
112 | - } |
|
90 | + /** |
|
91 | + * 获取指定目录下的文件列表 |
|
92 | + * @param string $namespace 空间名,必须 |
|
93 | + * @param string $dir 路径 |
|
94 | + * @param number $page 页数 |
|
95 | + * @param number $pageSize 每页显示的记录数 |
|
96 | + */ |
|
97 | + public function listFiles($namespace, $dir, $page = 1, $pageSize = 100) |
|
98 | + { |
|
99 | + $manageOption = new ManageOption($namespace); |
|
100 | + $manageOption->setDir($dir)->setCurrentPage($page)->setPageSize($pageSize); |
|
101 | + list($isValid, $message) = $manageOption->checkResourceInfo(true); |
|
102 | + if ($page <= 0 || $pageSize <= 0) { |
|
103 | + $isValid = false; |
|
104 | + $message = "Invalid parameters page or pageSize"; |
|
105 | + } |
|
106 | + if (!$isValid) { |
|
107 | + return $this->errorResponse("InvalidArgument", $message); |
|
108 | + } |
|
109 | + $queryParas = $manageOption->buildListFilesParas(); //查询query参数 |
|
110 | + $uri = '/' . Conf::MANAGE_API_VERSION . '/files?' . $queryParas; |
|
111 | + return $this->sendRequest('GET', $uri); |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * 删除文件 |
|
116 | - * @param string $namespace 空间名,必须 |
|
117 | - * @param string $dir 路径 |
|
118 | - * @param string $filename 文件名 |
|
119 | - * @return array |
|
120 | - */ |
|
121 | - public function deleteFile($namespace, $dir, $filename) |
|
122 | - { |
|
123 | - $resourceInfo = new ResourceInfo($namespace, $dir, $filename); |
|
124 | - list($isValid, $message) = $resourceInfo->checkResourceInfo(true, true); |
|
125 | - if (!$isValid) { |
|
126 | - return $this->errorResponse("InvalidArgument", $message); |
|
127 | - } |
|
128 | - $resourceId = $resourceInfo->buildResourceId(); //得到资源ID |
|
129 | - $uri = '/' . Conf::MANAGE_API_VERSION . '/files/' . $resourceId; |
|
130 | - return $this->sendRequest('DELETE', $uri); |
|
131 | - } |
|
114 | + /** |
|
115 | + * 删除文件 |
|
116 | + * @param string $namespace 空间名,必须 |
|
117 | + * @param string $dir 路径 |
|
118 | + * @param string $filename 文件名 |
|
119 | + * @return array |
|
120 | + */ |
|
121 | + public function deleteFile($namespace, $dir, $filename) |
|
122 | + { |
|
123 | + $resourceInfo = new ResourceInfo($namespace, $dir, $filename); |
|
124 | + list($isValid, $message) = $resourceInfo->checkResourceInfo(true, true); |
|
125 | + if (!$isValid) { |
|
126 | + return $this->errorResponse("InvalidArgument", $message); |
|
127 | + } |
|
128 | + $resourceId = $resourceInfo->buildResourceId(); //得到资源ID |
|
129 | + $uri = '/' . Conf::MANAGE_API_VERSION . '/files/' . $resourceId; |
|
130 | + return $this->sendRequest('DELETE', $uri); |
|
131 | + } |
|
132 | 132 | |
133 | - /** |
|
134 | - * 文件夹是否存在 |
|
135 | - * @param string $namespace 空间名,必须 |
|
136 | - * @param string $dir 路径,即文件夹 |
|
137 | - * @return array |
|
138 | - */ |
|
139 | - public function existsFolder($namespace, $dir) |
|
140 | - { |
|
141 | - if (empty($namespace) || empty($dir)) { |
|
142 | - return $this->errorResponse("InvalidArgument", "namespace or dir is empty"); |
|
143 | - } |
|
144 | - if (strpos($dir, '/') !== 0) { |
|
145 | - $dir = '/' . $dir; |
|
146 | - } |
|
147 | - $resourceInfo = new ResourceInfo($namespace, $dir); |
|
148 | - $resourceId = $resourceInfo->buildResourceId(); //得到资源ID |
|
149 | - $uri = '/' . Conf::MANAGE_API_VERSION . '/folders/' . $resourceId . '/exist'; |
|
150 | - return $this->sendRequest('GET', $uri); |
|
151 | - } |
|
133 | + /** |
|
134 | + * 文件夹是否存在 |
|
135 | + * @param string $namespace 空间名,必须 |
|
136 | + * @param string $dir 路径,即文件夹 |
|
137 | + * @return array |
|
138 | + */ |
|
139 | + public function existsFolder($namespace, $dir) |
|
140 | + { |
|
141 | + if (empty($namespace) || empty($dir)) { |
|
142 | + return $this->errorResponse("InvalidArgument", "namespace or dir is empty"); |
|
143 | + } |
|
144 | + if (strpos($dir, '/') !== 0) { |
|
145 | + $dir = '/' . $dir; |
|
146 | + } |
|
147 | + $resourceInfo = new ResourceInfo($namespace, $dir); |
|
148 | + $resourceId = $resourceInfo->buildResourceId(); //得到资源ID |
|
149 | + $uri = '/' . Conf::MANAGE_API_VERSION . '/folders/' . $resourceId . '/exist'; |
|
150 | + return $this->sendRequest('GET', $uri); |
|
151 | + } |
|
152 | 152 | |
153 | - /** |
|
154 | - * 创建文件夹 |
|
155 | - * @param string $namespace 空间名,必须 |
|
156 | - * @param string $dir 路径,即文件夹 |
|
157 | - * @return array |
|
158 | - */ |
|
159 | - public function createDir($namespace, $dir) |
|
160 | - { |
|
161 | - if (empty($namespace) || empty($dir)) { |
|
162 | - return $this->errorResponse("InvalidArgument", "namespace or dir is empty"); |
|
163 | - } |
|
164 | - if (strpos($dir, '/') !== 0) { |
|
165 | - $dir = '/' . $dir; |
|
166 | - } |
|
167 | - $resourceInfo = new ResourceInfo($namespace, $dir); |
|
168 | - $resourceId = $resourceInfo->buildResourceId(); //得到资源ID |
|
169 | - $uri = '/' . Conf::MANAGE_API_VERSION . '/folders/' . $resourceId; |
|
170 | - return $this->sendRequest('POST', $uri); |
|
171 | - } |
|
153 | + /** |
|
154 | + * 创建文件夹 |
|
155 | + * @param string $namespace 空间名,必须 |
|
156 | + * @param string $dir 路径,即文件夹 |
|
157 | + * @return array |
|
158 | + */ |
|
159 | + public function createDir($namespace, $dir) |
|
160 | + { |
|
161 | + if (empty($namespace) || empty($dir)) { |
|
162 | + return $this->errorResponse("InvalidArgument", "namespace or dir is empty"); |
|
163 | + } |
|
164 | + if (strpos($dir, '/') !== 0) { |
|
165 | + $dir = '/' . $dir; |
|
166 | + } |
|
167 | + $resourceInfo = new ResourceInfo($namespace, $dir); |
|
168 | + $resourceId = $resourceInfo->buildResourceId(); //得到资源ID |
|
169 | + $uri = '/' . Conf::MANAGE_API_VERSION . '/folders/' . $resourceId; |
|
170 | + return $this->sendRequest('POST', $uri); |
|
171 | + } |
|
172 | 172 | |
173 | - /** |
|
174 | - * 获取指定目录下的文件夹列表 |
|
175 | - * @param string $namespace 空间名,必须 |
|
176 | - * @param string $dir 路径,指定目录 |
|
177 | - * @param number $page 页数 |
|
178 | - * @param number $pageSize 每页显示的记录数 |
|
179 | - */ |
|
180 | - public function listDirs($namespace, $dir, $page = 1, $pageSize = 100) |
|
181 | - { |
|
182 | - $manageOption = new ManageOption($namespace); |
|
183 | - $manageOption->setDir($dir)->setCurrentPage($page)->setPageSize($pageSize); |
|
184 | - list($isValid, $message) = $manageOption->checkResourceInfo(true); |
|
185 | - if ($page <= 0 || $pageSize <= 0) { |
|
186 | - $isValid = false; |
|
187 | - $message = "Invalid parameters page or pageSize"; |
|
188 | - } |
|
189 | - if (!$isValid) { |
|
190 | - return $this->errorResponse("InvalidArgument", $message); |
|
191 | - } |
|
192 | - $queryParas = $manageOption->buildListFilesParas(); //查询query参数 |
|
193 | - $uri = '/' . Conf::MANAGE_API_VERSION . '/folders?' . $queryParas; |
|
194 | - return $this->sendRequest('GET', $uri); |
|
195 | - } |
|
173 | + /** |
|
174 | + * 获取指定目录下的文件夹列表 |
|
175 | + * @param string $namespace 空间名,必须 |
|
176 | + * @param string $dir 路径,指定目录 |
|
177 | + * @param number $page 页数 |
|
178 | + * @param number $pageSize 每页显示的记录数 |
|
179 | + */ |
|
180 | + public function listDirs($namespace, $dir, $page = 1, $pageSize = 100) |
|
181 | + { |
|
182 | + $manageOption = new ManageOption($namespace); |
|
183 | + $manageOption->setDir($dir)->setCurrentPage($page)->setPageSize($pageSize); |
|
184 | + list($isValid, $message) = $manageOption->checkResourceInfo(true); |
|
185 | + if ($page <= 0 || $pageSize <= 0) { |
|
186 | + $isValid = false; |
|
187 | + $message = "Invalid parameters page or pageSize"; |
|
188 | + } |
|
189 | + if (!$isValid) { |
|
190 | + return $this->errorResponse("InvalidArgument", $message); |
|
191 | + } |
|
192 | + $queryParas = $manageOption->buildListFilesParas(); //查询query参数 |
|
193 | + $uri = '/' . Conf::MANAGE_API_VERSION . '/folders?' . $queryParas; |
|
194 | + return $this->sendRequest('GET', $uri); |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * 删除文件夹 |
|
199 | - * @param string $namespace 空间名,必须 |
|
200 | - * @param string $dir 路径,即文件夹 |
|
201 | - * @return array |
|
202 | - */ |
|
203 | - public function deleteDir($namespace, $dir) |
|
204 | - { |
|
205 | - if (empty($namespace) || empty($dir)) { |
|
206 | - return $this->errorResponse("InvalidArgument", "namespace or dir is empty"); |
|
207 | - } |
|
208 | - if (strpos($dir, '/') !== 0) { |
|
209 | - $dir = '/' . $dir; |
|
210 | - } |
|
211 | - $resourceInfo = new ResourceInfo($namespace, $dir); |
|
212 | - $resourceId = $resourceInfo->buildResourceId(); //得到资源ID |
|
213 | - $uri = '/' . Conf::MANAGE_API_VERSION . '/folders/' . $resourceId; |
|
214 | - return $this->sendRequest('DELETE', $uri); |
|
215 | - } |
|
216 | - /*######################################华丽的分界线#######################################*/ |
|
217 | - /*#######################上面是文件或文件夹的管理,下面是特色服务接口########################*/ |
|
218 | - /*########################################################################################*/ |
|
219 | - /**黄图扫描接口 |
|
197 | + /** |
|
198 | + * 删除文件夹 |
|
199 | + * @param string $namespace 空间名,必须 |
|
200 | + * @param string $dir 路径,即文件夹 |
|
201 | + * @return array |
|
202 | + */ |
|
203 | + public function deleteDir($namespace, $dir) |
|
204 | + { |
|
205 | + if (empty($namespace) || empty($dir)) { |
|
206 | + return $this->errorResponse("InvalidArgument", "namespace or dir is empty"); |
|
207 | + } |
|
208 | + if (strpos($dir, '/') !== 0) { |
|
209 | + $dir = '/' . $dir; |
|
210 | + } |
|
211 | + $resourceInfo = new ResourceInfo($namespace, $dir); |
|
212 | + $resourceId = $resourceInfo->buildResourceId(); //得到资源ID |
|
213 | + $uri = '/' . Conf::MANAGE_API_VERSION . '/folders/' . $resourceId; |
|
214 | + return $this->sendRequest('DELETE', $uri); |
|
215 | + } |
|
216 | + /*######################################华丽的分界线#######################################*/ |
|
217 | + /*#######################上面是文件或文件夹的管理,下面是特色服务接口########################*/ |
|
218 | + /*########################################################################################*/ |
|
219 | + /**黄图扫描接口 |
|
220 | 220 | * @param ManageOption $resInfos 待扫描图片资源 |
221 | 221 | * @return array |
222 | 222 | */ |
223 | - public function scanPorn(ManageOption $resInfos) |
|
224 | - { |
|
225 | - $uri = '/' . Conf::SCAN_PORN_VERSION . '/scanPorn'; |
|
226 | - list($isValid, $message, $bodyArray) = $resInfos->checkFilesAndUrls(); //检测并得到黄图扫描所需参数 |
|
227 | - if (!$isValid) { |
|
228 | - return $this->errorResponse("InvalidArgument", $message); |
|
229 | - } |
|
230 | - $httpBody = $this->createHttpBody($bodyArray); //http body字符串信息 |
|
231 | - return $this->sendRequest('POST', $uri, $httpBody); |
|
232 | - } |
|
223 | + public function scanPorn(ManageOption $resInfos) |
|
224 | + { |
|
225 | + $uri = '/' . Conf::SCAN_PORN_VERSION . '/scanPorn'; |
|
226 | + list($isValid, $message, $bodyArray) = $resInfos->checkFilesAndUrls(); //检测并得到黄图扫描所需参数 |
|
227 | + if (!$isValid) { |
|
228 | + return $this->errorResponse("InvalidArgument", $message); |
|
229 | + } |
|
230 | + $httpBody = $this->createHttpBody($bodyArray); //http body字符串信息 |
|
231 | + return $this->sendRequest('POST', $uri, $httpBody); |
|
232 | + } |
|
233 | 233 | |
234 | - /** |
|
235 | - * 鉴黄反馈feedback接口 |
|
236 | - * @param ManageOption $pornFbInfos 反馈信息 |
|
237 | - * @return array |
|
238 | - */ |
|
239 | - public function pornFeedback(ManageOption $pornFbInfos) |
|
240 | - { |
|
241 | - $uri = '/' . Conf::SCAN_PORN_VERSION . '/feedback'; |
|
242 | - list($isValid, $message, $httpBody) = $pornFbInfos->checkPornFeedbackInfos(); |
|
243 | - if (!$isValid) { |
|
244 | - return $this->errorResponse("InvalidArgument", $message); |
|
245 | - } |
|
246 | - return $this->sendRequest('POST', $uri, $httpBody); |
|
247 | - } |
|
234 | + /** |
|
235 | + * 鉴黄反馈feedback接口 |
|
236 | + * @param ManageOption $pornFbInfos 反馈信息 |
|
237 | + * @return array |
|
238 | + */ |
|
239 | + public function pornFeedback(ManageOption $pornFbInfos) |
|
240 | + { |
|
241 | + $uri = '/' . Conf::SCAN_PORN_VERSION . '/feedback'; |
|
242 | + list($isValid, $message, $httpBody) = $pornFbInfos->checkPornFeedbackInfos(); |
|
243 | + if (!$isValid) { |
|
244 | + return $this->errorResponse("InvalidArgument", $message); |
|
245 | + } |
|
246 | + return $this->sendRequest('POST', $uri, $httpBody); |
|
247 | + } |
|
248 | 248 | |
249 | - /** |
|
250 | - * 多媒体(音视频)转码服务接口 |
|
251 | - * @param MediaResOption $encodeOption 转码参数选项 |
|
252 | - * @return array |
|
253 | - */ |
|
254 | - public function mediaEncode(MediaResOption $encodeOption) |
|
255 | - { |
|
256 | - $uri = '/' . Conf::MEDIA_ENCODE_VERSION . '/mediaEncode'; |
|
257 | - list($isValid, $message, $httpBody) = $encodeOption->checkOptionParameters(); |
|
258 | - if (!$isValid) { |
|
259 | - return $this->errorResponse("InvalidArgument", $message); |
|
260 | - } |
|
261 | - return $this->sendRequest('POST', $uri, $httpBody); |
|
262 | - } |
|
249 | + /** |
|
250 | + * 多媒体(音视频)转码服务接口 |
|
251 | + * @param MediaResOption $encodeOption 转码参数选项 |
|
252 | + * @return array |
|
253 | + */ |
|
254 | + public function mediaEncode(MediaResOption $encodeOption) |
|
255 | + { |
|
256 | + $uri = '/' . Conf::MEDIA_ENCODE_VERSION . '/mediaEncode'; |
|
257 | + list($isValid, $message, $httpBody) = $encodeOption->checkOptionParameters(); |
|
258 | + if (!$isValid) { |
|
259 | + return $this->errorResponse("InvalidArgument", $message); |
|
260 | + } |
|
261 | + return $this->sendRequest('POST', $uri, $httpBody); |
|
262 | + } |
|
263 | 263 | |
264 | - /** |
|
265 | - * 多媒体转码任务查询接口 |
|
266 | - * @param string $taskId 转码任务ID |
|
267 | - */ |
|
268 | - public function mediaEncodeQuery($taskId) |
|
269 | - { |
|
270 | - if (empty($taskId)) { |
|
271 | - return $this->errorResponse("InvalidArgument", "taskId is empty"); |
|
272 | - } |
|
273 | - $uri = '/' . Conf::MEDIA_ENCODE_VERSION . '/mediaEncodeResult/' . $taskId; |
|
274 | - return $this->sendRequest('GET', $uri); |
|
275 | - } |
|
264 | + /** |
|
265 | + * 多媒体转码任务查询接口 |
|
266 | + * @param string $taskId 转码任务ID |
|
267 | + */ |
|
268 | + public function mediaEncodeQuery($taskId) |
|
269 | + { |
|
270 | + if (empty($taskId)) { |
|
271 | + return $this->errorResponse("InvalidArgument", "taskId is empty"); |
|
272 | + } |
|
273 | + $uri = '/' . Conf::MEDIA_ENCODE_VERSION . '/mediaEncodeResult/' . $taskId; |
|
274 | + return $this->sendRequest('GET', $uri); |
|
275 | + } |
|
276 | 276 | |
277 | - /** |
|
278 | - * 视频截图接口 |
|
279 | - * @param MediaResOption $snapshotOption 截图参数选项 |
|
280 | - * @return array |
|
281 | - */ |
|
282 | - public function videoSnapshot(MediaResOption $snapshotOption) |
|
283 | - { |
|
284 | - $uri = '/' . Conf::MANAGE_API_VERSION . '/snapshot'; |
|
285 | - list($isValid, $message, $httpBody) = $snapshotOption->checkOptionParameters(); |
|
286 | - if (!$isValid) { |
|
287 | - return $this->errorResponse("InvalidArgument", $message); |
|
288 | - } |
|
289 | - return $this->sendRequest('POST', $uri, $httpBody); |
|
290 | - } |
|
277 | + /** |
|
278 | + * 视频截图接口 |
|
279 | + * @param MediaResOption $snapshotOption 截图参数选项 |
|
280 | + * @return array |
|
281 | + */ |
|
282 | + public function videoSnapshot(MediaResOption $snapshotOption) |
|
283 | + { |
|
284 | + $uri = '/' . Conf::MANAGE_API_VERSION . '/snapshot'; |
|
285 | + list($isValid, $message, $httpBody) = $snapshotOption->checkOptionParameters(); |
|
286 | + if (!$isValid) { |
|
287 | + return $this->errorResponse("InvalidArgument", $message); |
|
288 | + } |
|
289 | + return $this->sendRequest('POST', $uri, $httpBody); |
|
290 | + } |
|
291 | 291 | |
292 | - /** |
|
293 | - * 视频截图结果查询接口 |
|
294 | - * @param string $taskId 转码任务ID |
|
295 | - */ |
|
296 | - public function vSnapshotQuery($taskId) |
|
297 | - { |
|
298 | - if (empty($taskId)) { |
|
299 | - return $this->errorResponse("InvalidArgument", "taskId is empty"); |
|
300 | - } |
|
301 | - $uri = '/' . Conf::MANAGE_API_VERSION . '/snapshotResult/' . $taskId; |
|
302 | - return $this->sendRequest('GET', $uri); |
|
303 | - } |
|
292 | + /** |
|
293 | + * 视频截图结果查询接口 |
|
294 | + * @param string $taskId 转码任务ID |
|
295 | + */ |
|
296 | + public function vSnapshotQuery($taskId) |
|
297 | + { |
|
298 | + if (empty($taskId)) { |
|
299 | + return $this->errorResponse("InvalidArgument", "taskId is empty"); |
|
300 | + } |
|
301 | + $uri = '/' . Conf::MANAGE_API_VERSION . '/snapshotResult/' . $taskId; |
|
302 | + return $this->sendRequest('GET', $uri); |
|
303 | + } |
|
304 | 304 | |
305 | - /** |
|
306 | - * |
|
307 | - * 广告图扫描接口(beta) |
|
308 | - * @param ManageOption $resInfos 待扫描图片资源 |
|
309 | - * @return array |
|
310 | - */ |
|
311 | - public function scanAdvertising(ManageOption $resInfos) |
|
312 | - { |
|
313 | - $uri = '/3.1/scanAdvertising'; |
|
314 | - list($isValid, $message, $bodyArray) = $resInfos->checkFilesAndUrls(); //检测并得到广告图图扫描所需参数 |
|
315 | - if (!$isValid) { |
|
316 | - return $this->errorResponse("InvalidArgument", $message); |
|
317 | - } |
|
318 | - $httpBody = $this->createHttpBody($bodyArray); //http body字符串信息 |
|
319 | - return $this->sendRequest('POST', $uri, $httpBody); |
|
320 | - } |
|
305 | + /** |
|
306 | + * |
|
307 | + * 广告图扫描接口(beta) |
|
308 | + * @param ManageOption $resInfos 待扫描图片资源 |
|
309 | + * @return array |
|
310 | + */ |
|
311 | + public function scanAdvertising(ManageOption $resInfos) |
|
312 | + { |
|
313 | + $uri = '/3.1/scanAdvertising'; |
|
314 | + list($isValid, $message, $bodyArray) = $resInfos->checkFilesAndUrls(); //检测并得到广告图图扫描所需参数 |
|
315 | + if (!$isValid) { |
|
316 | + return $this->errorResponse("InvalidArgument", $message); |
|
317 | + } |
|
318 | + $httpBody = $this->createHttpBody($bodyArray); //http body字符串信息 |
|
319 | + return $this->sendRequest('POST', $uri, $httpBody); |
|
320 | + } |
|
321 | 321 | |
322 | - /** |
|
323 | - * 调用curl利用http上传数据 |
|
324 | - * @param string $method |
|
325 | - * @param string $uri |
|
326 | - * @param array $bodyArray |
|
327 | - * @param array $headers |
|
328 | - * @return array (isSuccess, ...) |
|
329 | - */ |
|
330 | - protected function sendRequest($method, $uri, $httpBody = null, $headers = null) |
|
331 | - { |
|
332 | - $success = false; |
|
333 | - $result = null; |
|
334 | - //构建Http请求头 |
|
335 | - $_headers = array('Expect:'); |
|
336 | - $date = $this->currentMilliSecond(); //得到当前的时间戳,毫秒 |
|
337 | - array_push($_headers, "Date: {$date}"); |
|
338 | - $authorization = $this->_getAuthorization($uri, $date, $httpBody); //Http的Body需要加入管理鉴权 |
|
339 | - array_push($_headers, "Authorization: {$authorization}"); |
|
340 | - array_push($_headers, "User-Agent: {$this->getUserAgent()}"); |
|
341 | - if (!is_null($headers) && is_array($headers)) { |
|
342 | - foreach ($headers as $k => $v) { |
|
343 | - array_push($_headers, "{$k}: {$v}"); |
|
344 | - } |
|
345 | - } |
|
346 | - $url = $this->getManageUrl($uri); //根据管理接口uri拼接成URL |
|
347 | - $ch = curl_init($url); |
|
348 | - try { |
|
349 | - //构建http请求体,并设置header部分属性 |
|
350 | - $length = 0; |
|
351 | - if (!empty($httpBody)) { |
|
352 | - $length = @strlen($httpBody); |
|
353 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $httpBody); |
|
354 | - array_push($_headers, "Content-Type: application/x-www-form-urlencoded"); |
|
355 | - } |
|
356 | - array_push($_headers, "Content-Length: {$length}"); |
|
357 | - curl_setopt($ch, CURLOPT_HEADER, 1); //设置头部 |
|
358 | - curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers); //请求头 |
|
359 | - curl_setopt($ch, CURLOPT_TIMEOUT, Conf::HTTP_TIMEOUT); //超时时长 |
|
360 | - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); //连接超时时长 |
|
361 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //成功,只返回结果,不自动输出任何内容。如果失败返回FALSE |
|
362 | - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); |
|
363 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //自定义请求 |
|
364 | - //设置请求方式(GET或POST等) |
|
365 | - if ($method == 'PUT' || $method == 'POST') { |
|
366 | - curl_setopt($ch, CURLOPT_POST, 1); |
|
367 | - } else { |
|
368 | - curl_setopt($ch, CURLOPT_POST, 0); |
|
369 | - } |
|
370 | - //执行请求,然后获取服务端返回 |
|
371 | - $response = curl_exec($ch); |
|
372 | - if ($response == false) { |
|
373 | - $result = $this->errorResponse("curl error", "curl request failed"); |
|
374 | - $result['errno'] = curl_errno($ch); //错误码 |
|
375 | - } else { |
|
376 | - //解析返回结果,并判断是否上传成功 |
|
377 | - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
378 | - $success = ($http_code == 200) ? true : false; //判断是否上传成功 |
|
379 | - $resStr = explode("\r\n\r\n", $response); |
|
380 | - $resBody = isset($resStr[1]) ? $resStr[1] : ''; |
|
381 | - $resArray = json_decode($resBody, true); //解析得到结果 |
|
382 | - $result = (empty($resArray)) ? array() : $resArray; |
|
383 | - } |
|
384 | - } catch (Exception $e) { |
|
385 | - $success = false; |
|
386 | - $result = $this->errorResponse("HTTPRequestException#" . $e->getCode(), $e->getMessage()); |
|
387 | - } |
|
388 | - curl_close($ch); //PHP5.3中不支持finally关键字。因此,为了兼容,这里取消finally |
|
389 | - $result['isSuccess'] = $success; |
|
390 | - return $result; |
|
391 | - } |
|
322 | + /** |
|
323 | + * 调用curl利用http上传数据 |
|
324 | + * @param string $method |
|
325 | + * @param string $uri |
|
326 | + * @param array $bodyArray |
|
327 | + * @param array $headers |
|
328 | + * @return array (isSuccess, ...) |
|
329 | + */ |
|
330 | + protected function sendRequest($method, $uri, $httpBody = null, $headers = null) |
|
331 | + { |
|
332 | + $success = false; |
|
333 | + $result = null; |
|
334 | + //构建Http请求头 |
|
335 | + $_headers = array('Expect:'); |
|
336 | + $date = $this->currentMilliSecond(); //得到当前的时间戳,毫秒 |
|
337 | + array_push($_headers, "Date: {$date}"); |
|
338 | + $authorization = $this->_getAuthorization($uri, $date, $httpBody); //Http的Body需要加入管理鉴权 |
|
339 | + array_push($_headers, "Authorization: {$authorization}"); |
|
340 | + array_push($_headers, "User-Agent: {$this->getUserAgent()}"); |
|
341 | + if (!is_null($headers) && is_array($headers)) { |
|
342 | + foreach ($headers as $k => $v) { |
|
343 | + array_push($_headers, "{$k}: {$v}"); |
|
344 | + } |
|
345 | + } |
|
346 | + $url = $this->getManageUrl($uri); //根据管理接口uri拼接成URL |
|
347 | + $ch = curl_init($url); |
|
348 | + try { |
|
349 | + //构建http请求体,并设置header部分属性 |
|
350 | + $length = 0; |
|
351 | + if (!empty($httpBody)) { |
|
352 | + $length = @strlen($httpBody); |
|
353 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $httpBody); |
|
354 | + array_push($_headers, "Content-Type: application/x-www-form-urlencoded"); |
|
355 | + } |
|
356 | + array_push($_headers, "Content-Length: {$length}"); |
|
357 | + curl_setopt($ch, CURLOPT_HEADER, 1); //设置头部 |
|
358 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers); //请求头 |
|
359 | + curl_setopt($ch, CURLOPT_TIMEOUT, Conf::HTTP_TIMEOUT); //超时时长 |
|
360 | + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); //连接超时时长 |
|
361 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //成功,只返回结果,不自动输出任何内容。如果失败返回FALSE |
|
362 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); |
|
363 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //自定义请求 |
|
364 | + //设置请求方式(GET或POST等) |
|
365 | + if ($method == 'PUT' || $method == 'POST') { |
|
366 | + curl_setopt($ch, CURLOPT_POST, 1); |
|
367 | + } else { |
|
368 | + curl_setopt($ch, CURLOPT_POST, 0); |
|
369 | + } |
|
370 | + //执行请求,然后获取服务端返回 |
|
371 | + $response = curl_exec($ch); |
|
372 | + if ($response == false) { |
|
373 | + $result = $this->errorResponse("curl error", "curl request failed"); |
|
374 | + $result['errno'] = curl_errno($ch); //错误码 |
|
375 | + } else { |
|
376 | + //解析返回结果,并判断是否上传成功 |
|
377 | + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
378 | + $success = ($http_code == 200) ? true : false; //判断是否上传成功 |
|
379 | + $resStr = explode("\r\n\r\n", $response); |
|
380 | + $resBody = isset($resStr[1]) ? $resStr[1] : ''; |
|
381 | + $resArray = json_decode($resBody, true); //解析得到结果 |
|
382 | + $result = (empty($resArray)) ? array() : $resArray; |
|
383 | + } |
|
384 | + } catch (Exception $e) { |
|
385 | + $success = false; |
|
386 | + $result = $this->errorResponse("HTTPRequestException#" . $e->getCode(), $e->getMessage()); |
|
387 | + } |
|
388 | + curl_close($ch); //PHP5.3中不支持finally关键字。因此,为了兼容,这里取消finally |
|
389 | + $result['isSuccess'] = $success; |
|
390 | + return $result; |
|
391 | + } |
|
392 | 392 | |
393 | - /** |
|
394 | - * 根据$bodyArray构建http请求体。多个字段之间用&号分割 |
|
395 | - */ |
|
396 | - protected function createHttpBody($bodyArray) |
|
397 | - { |
|
398 | - $bodyStr = ''; |
|
399 | - foreach ($bodyArray as $key => $value) { |
|
400 | - $bodyStr .= (empty($bodyStr) ? null : '&'); //添加分隔符 |
|
401 | - $bodyStr .= "{$key}=" . urlencode($value); |
|
402 | - } |
|
403 | - return $bodyStr; |
|
404 | - } |
|
393 | + /** |
|
394 | + * 根据$bodyArray构建http请求体。多个字段之间用&号分割 |
|
395 | + */ |
|
396 | + protected function createHttpBody($bodyArray) |
|
397 | + { |
|
398 | + $bodyStr = ''; |
|
399 | + foreach ($bodyArray as $key => $value) { |
|
400 | + $bodyStr .= (empty($bodyStr) ? null : '&'); //添加分隔符 |
|
401 | + $bodyStr .= "{$key}=" . urlencode($value); |
|
402 | + } |
|
403 | + return $bodyStr; |
|
404 | + } |
|
405 | 405 | |
406 | - /** |
|
407 | - * UserAgent用户代理 |
|
408 | - */ |
|
409 | - protected function getUserAgent() |
|
410 | - { |
|
411 | - if ($this->type == "TOP") { |
|
412 | - return "ALIMEDIASDK_PHP_TAE/" . Conf::SDK_VERSION; |
|
413 | - } else { |
|
414 | - return "ALIMEDIASDK_PHP_CLOUD/" . Conf::SDK_VERSION; |
|
415 | - } |
|
416 | - } |
|
406 | + /** |
|
407 | + * UserAgent用户代理 |
|
408 | + */ |
|
409 | + protected function getUserAgent() |
|
410 | + { |
|
411 | + if ($this->type == "TOP") { |
|
412 | + return "ALIMEDIASDK_PHP_TAE/" . Conf::SDK_VERSION; |
|
413 | + } else { |
|
414 | + return "ALIMEDIASDK_PHP_CLOUD/" . Conf::SDK_VERSION; |
|
415 | + } |
|
416 | + } |
|
417 | 417 | |
418 | - /** |
|
419 | - * 根据管理接口uri拼接成完整的URL |
|
420 | - */ |
|
421 | - protected function getManageUrl($uri) |
|
422 | - { |
|
423 | - return Conf::MANAGE_HOST_MEDIA . $uri; |
|
424 | - } |
|
425 | - protected function getNamespaceKey() |
|
426 | - { |
|
427 | - if ($this->type == "TOP") { |
|
428 | - return "namespace"; |
|
429 | - } else { |
|
430 | - return "bucketName"; |
|
431 | - } |
|
432 | - } |
|
418 | + /** |
|
419 | + * 根据管理接口uri拼接成完整的URL |
|
420 | + */ |
|
421 | + protected function getManageUrl($uri) |
|
422 | + { |
|
423 | + return Conf::MANAGE_HOST_MEDIA . $uri; |
|
424 | + } |
|
425 | + protected function getNamespaceKey() |
|
426 | + { |
|
427 | + if ($this->type == "TOP") { |
|
428 | + return "namespace"; |
|
429 | + } else { |
|
430 | + return "bucketName"; |
|
431 | + } |
|
432 | + } |
|
433 | 433 | |
434 | - /** |
|
435 | - * 获取管理鉴权信息 |
|
436 | - */ |
|
437 | - protected function getAuthorization($uri, $date, $httpBody) |
|
438 | - { |
|
439 | - $stringBeforeSign = "{$uri}\n{$httpBody}\n{$date}"; //1.生成待加签的原始字符串 |
|
440 | - $signStr = hash_hmac('sha1', $stringBeforeSign, $this->sk); //2.使用SK对字符串计算HMAC-SHA1签名 |
|
441 | - $preenCode = $this->ak . ":" . $signStr; //3.将签名与AK进行拼接 |
|
442 | - $encodedStr = EncodeUtils::encodeWithURLSafeBase64($preenCode); //4.对拼接后的结果进行URL安全的Base64编码 |
|
443 | - $manageToken = "ACL_" . $this->type . " " . $encodedStr; //5.最后为编码结果加上得到管理凭证 |
|
444 | - return $manageToken; |
|
445 | - } |
|
434 | + /** |
|
435 | + * 获取管理鉴权信息 |
|
436 | + */ |
|
437 | + protected function getAuthorization($uri, $date, $httpBody) |
|
438 | + { |
|
439 | + $stringBeforeSign = "{$uri}\n{$httpBody}\n{$date}"; //1.生成待加签的原始字符串 |
|
440 | + $signStr = hash_hmac('sha1', $stringBeforeSign, $this->sk); //2.使用SK对字符串计算HMAC-SHA1签名 |
|
441 | + $preenCode = $this->ak . ":" . $signStr; //3.将签名与AK进行拼接 |
|
442 | + $encodedStr = EncodeUtils::encodeWithURLSafeBase64($preenCode); //4.对拼接后的结果进行URL安全的Base64编码 |
|
443 | + $manageToken = "ACL_" . $this->type . " " . $encodedStr; //5.最后为编码结果加上得到管理凭证 |
|
444 | + return $manageToken; |
|
445 | + } |
|
446 | 446 | |
447 | - /** |
|
448 | - * 得到当前时间的毫秒数 |
|
449 | - */ |
|
450 | - protected function currentMilliSecond() |
|
451 | - { |
|
452 | - list($microSec, $stampSec) = explode(' ', microtime()); |
|
453 | - $tempMilli = sprintf('%03s', intval($microSec * 1000)); |
|
454 | - $currentMilli = $stampSec . $tempMilli; |
|
455 | - return $currentMilli; |
|
456 | - } |
|
447 | + /** |
|
448 | + * 得到当前时间的毫秒数 |
|
449 | + */ |
|
450 | + protected function currentMilliSecond() |
|
451 | + { |
|
452 | + list($microSec, $stampSec) = explode(' ', microtime()); |
|
453 | + $tempMilli = sprintf('%03s', intval($microSec * 1000)); |
|
454 | + $currentMilli = $stampSec . $tempMilli; |
|
455 | + return $currentMilli; |
|
456 | + } |
|
457 | 457 | |
458 | - /** |
|
459 | - * 反馈错误信息 |
|
460 | - */ |
|
461 | - protected function errorResponse($code = "UnknownError", $message = "unkonown error", $requestId = null) |
|
462 | - { |
|
463 | - return array( |
|
464 | - "isSuccess" => false, |
|
465 | - "code" => $code, |
|
466 | - "message" => $message, |
|
467 | - "requestId" => $requestId |
|
468 | - ); |
|
469 | - } |
|
458 | + /** |
|
459 | + * 反馈错误信息 |
|
460 | + */ |
|
461 | + protected function errorResponse($code = "UnknownError", $message = "unkonown error", $requestId = null) |
|
462 | + { |
|
463 | + return array( |
|
464 | + "isSuccess" => false, |
|
465 | + "code" => $code, |
|
466 | + "message" => $message, |
|
467 | + "requestId" => $requestId |
|
468 | + ); |
|
469 | + } |
|
470 | 470 | } |
@@ -335,7 +335,7 @@ discard block |
||
335 | 335 | $_headers = array('Expect:'); |
336 | 336 | $date = $this->currentMilliSecond(); //得到当前的时间戳,毫秒 |
337 | 337 | array_push($_headers, "Date: {$date}"); |
338 | - $authorization = $this->_getAuthorization($uri, $date, $httpBody); //Http的Body需要加入管理鉴权 |
|
338 | + $authorization = $this->_getAuthorization($uri, $date, $httpBody); //Http的Body需要加入管理鉴权 |
|
339 | 339 | array_push($_headers, "Authorization: {$authorization}"); |
340 | 340 | array_push($_headers, "User-Agent: {$this->getUserAgent()}"); |
341 | 341 | if (!is_null($headers) && is_array($headers)) { |
@@ -354,13 +354,13 @@ discard block |
||
354 | 354 | array_push($_headers, "Content-Type: application/x-www-form-urlencoded"); |
355 | 355 | } |
356 | 356 | array_push($_headers, "Content-Length: {$length}"); |
357 | - curl_setopt($ch, CURLOPT_HEADER, 1); //设置头部 |
|
358 | - curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers); //请求头 |
|
359 | - curl_setopt($ch, CURLOPT_TIMEOUT, Conf::HTTP_TIMEOUT); //超时时长 |
|
360 | - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); //连接超时时长 |
|
357 | + curl_setopt($ch, CURLOPT_HEADER, 1); //设置头部 |
|
358 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers); //请求头 |
|
359 | + curl_setopt($ch, CURLOPT_TIMEOUT, Conf::HTTP_TIMEOUT); //超时时长 |
|
360 | + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); //连接超时时长 |
|
361 | 361 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //成功,只返回结果,不自动输出任何内容。如果失败返回FALSE |
362 | 362 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); |
363 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //自定义请求 |
|
363 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //自定义请求 |
|
364 | 364 | //设置请求方式(GET或POST等) |
365 | 365 | if ($method == 'PUT' || $method == 'POST') { |
366 | 366 | curl_setopt($ch, CURLOPT_POST, 1); |
@@ -375,10 +375,10 @@ discard block |
||
375 | 375 | } else { |
376 | 376 | //解析返回结果,并判断是否上传成功 |
377 | 377 | $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
378 | - $success = ($http_code == 200) ? true : false; //判断是否上传成功 |
|
378 | + $success = ($http_code == 200) ? true : false; //判断是否上传成功 |
|
379 | 379 | $resStr = explode("\r\n\r\n", $response); |
380 | 380 | $resBody = isset($resStr[1]) ? $resStr[1] : ''; |
381 | - $resArray = json_decode($resBody, true); //解析得到结果 |
|
381 | + $resArray = json_decode($resBody, true); //解析得到结果 |
|
382 | 382 | $result = (empty($resArray)) ? array() : $resArray; |
383 | 383 | } |
384 | 384 | } catch (Exception $e) { |
@@ -450,7 +450,7 @@ discard block |
||
450 | 450 | protected function currentMilliSecond() |
451 | 451 | { |
452 | 452 | list($microSec, $stampSec) = explode(' ', microtime()); |
453 | - $tempMilli = sprintf('%03s', intval($microSec * 1000)); |
|
453 | + $tempMilli = sprintf('%03s', intval($microSec * 1000)); |
|
454 | 454 | $currentMilli = $stampSec . $tempMilli; |
455 | 455 | return $currentMilli; |
456 | 456 | } |
@@ -4,93 +4,93 @@ |
||
4 | 4 | /**文件资源类*/ |
5 | 5 | class ResourceInfo |
6 | 6 | { |
7 | - /* 以下属性是资源ID所需的属性*/ |
|
8 | - protected $namespace; //空间名,必须 |
|
9 | - protected $dir; //路径 |
|
10 | - protected $name; //文件名信息 |
|
11 | - /* 以下属性是资源ID所需的属性*/ |
|
12 | - /** |
|
13 | - * ResourceOption的构造函数 |
|
14 | - */ |
|
15 | - public function __construct($namespace, $dir = null, $name = null) |
|
16 | - { |
|
17 | - $this->namespace = $namespace; |
|
18 | - $this->dir = $dir; |
|
19 | - $this->name = $name; |
|
20 | - } |
|
21 | - /**检测资源ID的是否合法。$dirState表示是否检测dir,$nameState表示是否检测filename<p> |
|
7 | + /* 以下属性是资源ID所需的属性*/ |
|
8 | + protected $namespace; //空间名,必须 |
|
9 | + protected $dir; //路径 |
|
10 | + protected $name; //文件名信息 |
|
11 | + /* 以下属性是资源ID所需的属性*/ |
|
12 | + /** |
|
13 | + * ResourceOption的构造函数 |
|
14 | + */ |
|
15 | + public function __construct($namespace, $dir = null, $name = null) |
|
16 | + { |
|
17 | + $this->namespace = $namespace; |
|
18 | + $this->dir = $dir; |
|
19 | + $this->name = $name; |
|
20 | + } |
|
21 | + /**检测资源ID的是否合法。$dirState表示是否检测dir,$nameState表示是否检测filename<p> |
|
22 | 22 | * 返回格式{$isValid, $message}*/ |
23 | - public function checkResourceInfo($dirState = false, $nameState = false) |
|
24 | - { |
|
25 | - if (empty($this->namespace)) { |
|
26 | - return array(false, "namespace is empty.[{$this->toString()}]"); // 判断是否设置空间名 |
|
27 | - } else { |
|
28 | - return $this->checkFileInfo($dirState, $nameState); // 判断dir和name的合法性 |
|
29 | - } |
|
30 | - } |
|
31 | - /**检测文件信息是否合法。$dirState表示是否检测dir,$nameState表示是否检测filename<p> |
|
23 | + public function checkResourceInfo($dirState = false, $nameState = false) |
|
24 | + { |
|
25 | + if (empty($this->namespace)) { |
|
26 | + return array(false, "namespace is empty.[{$this->toString()}]"); // 判断是否设置空间名 |
|
27 | + } else { |
|
28 | + return $this->checkFileInfo($dirState, $nameState); // 判断dir和name的合法性 |
|
29 | + } |
|
30 | + } |
|
31 | + /**检测文件信息是否合法。$dirState表示是否检测dir,$nameState表示是否检测filename<p> |
|
32 | 32 | * 返回格式{$isValid, $message}*/ |
33 | - public function checkFileInfo($dirState = false, $nameState = false) |
|
34 | - { |
|
35 | - if ($nameState && empty($this->name)) { |
|
36 | - return array(false, "file's name is empty.[{$this->toString()}]"); // 1:若需要进行文件名name检测,则判断文件名是否为空 |
|
37 | - } |
|
38 | - if ($dirState) { |
|
39 | - if (empty($this->dir)) { |
|
40 | - $this->dir = '/'; // 2:判断路径是否为空,若为空,则默认为根目录'/' |
|
41 | - } elseif (strpos($this->dir, '/') !== 0) { |
|
42 | - $this->dir = '/' . $this->dir; // 3:判断路径是否以'/'开头,若不是,则添加'/' |
|
43 | - } |
|
44 | - } |
|
45 | - return array(true, null); |
|
46 | - } |
|
47 | - /**资源ID(resourceId)的生成*/ |
|
48 | - public function buildResourceId() |
|
49 | - { |
|
50 | - $jsonData = array(); |
|
51 | - if (!empty($this->namespace)) { |
|
52 | - array_push($jsonData, urldecode($this->namespace)); |
|
53 | - } |
|
54 | - if (!empty($this->dir)) { |
|
55 | - array_push($jsonData, urldecode($this->dir)); |
|
56 | - } |
|
57 | - if (!empty($this->name)) { |
|
58 | - array_push($jsonData, urldecode($this->name)); |
|
59 | - } |
|
60 | - return EncodeUtils::encodeWithURLSafeBase64(json_encode($jsonData, true)); |
|
61 | - } |
|
62 | - /**对URL中文进行编码*/ |
|
63 | - protected function urlencodeCh($str) |
|
64 | - { |
|
65 | - return preg_replace_callback('/[^\0-\127]+/', function ($match) { |
|
66 | - return urlencode($match[0]); |
|
67 | - }, $str); |
|
68 | - } |
|
69 | - public function toString() |
|
70 | - { |
|
71 | - return "namespace={$this->namespace}, dir={$this->dir}, name={$this->name}"; |
|
72 | - } |
|
73 | - public function toArray() |
|
74 | - { |
|
75 | - return array("namespace" => $this->namespace, "dir" => $this->dir, "name" => $this->name); |
|
76 | - } |
|
77 | - /*###################下面是属性的get和set方法###############*/ |
|
78 | - /**设置路径*/ |
|
79 | - public function setNamespace($namespace) |
|
80 | - { |
|
81 | - $this->namespace = $namespace; |
|
82 | - return $this; |
|
83 | - } |
|
84 | - /**设置路径*/ |
|
85 | - public function setDir($dir) |
|
86 | - { |
|
87 | - $this->dir = $dir; |
|
88 | - return $this; |
|
89 | - } |
|
90 | - /**设置文件名*/ |
|
91 | - public function setName($filename) |
|
92 | - { |
|
93 | - $this->name = $filename; |
|
94 | - return $this; |
|
95 | - } |
|
33 | + public function checkFileInfo($dirState = false, $nameState = false) |
|
34 | + { |
|
35 | + if ($nameState && empty($this->name)) { |
|
36 | + return array(false, "file's name is empty.[{$this->toString()}]"); // 1:若需要进行文件名name检测,则判断文件名是否为空 |
|
37 | + } |
|
38 | + if ($dirState) { |
|
39 | + if (empty($this->dir)) { |
|
40 | + $this->dir = '/'; // 2:判断路径是否为空,若为空,则默认为根目录'/' |
|
41 | + } elseif (strpos($this->dir, '/') !== 0) { |
|
42 | + $this->dir = '/' . $this->dir; // 3:判断路径是否以'/'开头,若不是,则添加'/' |
|
43 | + } |
|
44 | + } |
|
45 | + return array(true, null); |
|
46 | + } |
|
47 | + /**资源ID(resourceId)的生成*/ |
|
48 | + public function buildResourceId() |
|
49 | + { |
|
50 | + $jsonData = array(); |
|
51 | + if (!empty($this->namespace)) { |
|
52 | + array_push($jsonData, urldecode($this->namespace)); |
|
53 | + } |
|
54 | + if (!empty($this->dir)) { |
|
55 | + array_push($jsonData, urldecode($this->dir)); |
|
56 | + } |
|
57 | + if (!empty($this->name)) { |
|
58 | + array_push($jsonData, urldecode($this->name)); |
|
59 | + } |
|
60 | + return EncodeUtils::encodeWithURLSafeBase64(json_encode($jsonData, true)); |
|
61 | + } |
|
62 | + /**对URL中文进行编码*/ |
|
63 | + protected function urlencodeCh($str) |
|
64 | + { |
|
65 | + return preg_replace_callback('/[^\0-\127]+/', function ($match) { |
|
66 | + return urlencode($match[0]); |
|
67 | + }, $str); |
|
68 | + } |
|
69 | + public function toString() |
|
70 | + { |
|
71 | + return "namespace={$this->namespace}, dir={$this->dir}, name={$this->name}"; |
|
72 | + } |
|
73 | + public function toArray() |
|
74 | + { |
|
75 | + return array("namespace" => $this->namespace, "dir" => $this->dir, "name" => $this->name); |
|
76 | + } |
|
77 | + /*###################下面是属性的get和set方法###############*/ |
|
78 | + /**设置路径*/ |
|
79 | + public function setNamespace($namespace) |
|
80 | + { |
|
81 | + $this->namespace = $namespace; |
|
82 | + return $this; |
|
83 | + } |
|
84 | + /**设置路径*/ |
|
85 | + public function setDir($dir) |
|
86 | + { |
|
87 | + $this->dir = $dir; |
|
88 | + return $this; |
|
89 | + } |
|
90 | + /**设置文件名*/ |
|
91 | + public function setName($filename) |
|
92 | + { |
|
93 | + $this->name = $filename; |
|
94 | + return $this; |
|
95 | + } |
|
96 | 96 | } |
@@ -5,9 +5,9 @@ discard block |
||
5 | 5 | class ResourceInfo |
6 | 6 | { |
7 | 7 | /* 以下属性是资源ID所需的属性*/ |
8 | - protected $namespace; //空间名,必须 |
|
9 | - protected $dir; //路径 |
|
10 | - protected $name; //文件名信息 |
|
8 | + protected $namespace; //空间名,必须 |
|
9 | + protected $dir; //路径 |
|
10 | + protected $name; //文件名信息 |
|
11 | 11 | /* 以下属性是资源ID所需的属性*/ |
12 | 12 | /** |
13 | 13 | * ResourceOption的构造函数 |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | /**对URL中文进行编码*/ |
63 | 63 | protected function urlencodeCh($str) |
64 | 64 | { |
65 | - return preg_replace_callback('/[^\0-\127]+/', function ($match) { |
|
65 | + return preg_replace_callback('/[^\0-\127]+/', function($match) { |
|
66 | 66 | return urlencode($match[0]); |
67 | 67 | }, $str); |
68 | 68 | } |
@@ -5,45 +5,45 @@ |
||
5 | 5 | /**视频截图的参数*/ |
6 | 6 | class SnapShotOption extends MediaResOption |
7 | 7 | { |
8 | - /** 视频截图的位置,单位为毫秒。该属性必须*/ |
|
9 | - private $time; |
|
10 | - /** 通知url,任务结束之后会调用这个url。该属性可选 */ |
|
11 | - private $notifyUrl; |
|
12 | - /**检测视频截图参数是否合法。如果合法,则返回http请求体<p> 返回格式{$isValid, $message, $httpBody}*/ |
|
13 | - public function checkOptionParameters() |
|
14 | - { |
|
15 | - list($valid, $msg) = parent::checkOptionParameters(); //检测输入输出资源是否合法 |
|
16 | - if (!$valid) { |
|
17 | - return array($valid, $msg, null); |
|
18 | - } |
|
19 | - if (empty($this->time) || !is_int($this->time) || $this->time < 0) { |
|
20 | - return array(false, "time is empty or invalid.", null); // 是否设置时间,且时间是否合法 |
|
21 | - } |
|
22 | - return $this->getOptionsHttpBody(); //返回http请求体 |
|
23 | - } |
|
8 | + /** 视频截图的位置,单位为毫秒。该属性必须*/ |
|
9 | + private $time; |
|
10 | + /** 通知url,任务结束之后会调用这个url。该属性可选 */ |
|
11 | + private $notifyUrl; |
|
12 | + /**检测视频截图参数是否合法。如果合法,则返回http请求体<p> 返回格式{$isValid, $message, $httpBody}*/ |
|
13 | + public function checkOptionParameters() |
|
14 | + { |
|
15 | + list($valid, $msg) = parent::checkOptionParameters(); //检测输入输出资源是否合法 |
|
16 | + if (!$valid) { |
|
17 | + return array($valid, $msg, null); |
|
18 | + } |
|
19 | + if (empty($this->time) || !is_int($this->time) || $this->time < 0) { |
|
20 | + return array(false, "time is empty or invalid.", null); // 是否设置时间,且时间是否合法 |
|
21 | + } |
|
22 | + return $this->getOptionsHttpBody(); //返回http请求体 |
|
23 | + } |
|
24 | 24 | |
25 | - /**构建多媒体转码所需的http请求体*/ |
|
26 | - public function getOptionsHttpBody() |
|
27 | - { |
|
28 | - //必须的参数 |
|
29 | - $httpBody = 'input=' . $this->getInputResId(); |
|
30 | - $httpBody .= '&output=' . $this->getOutputResId(); |
|
31 | - $httpBody .= '&time=' . $this->time; |
|
32 | - // 可选的参数 |
|
33 | - if (isset($this->notifyUrl)) { |
|
34 | - $httpBody .= '¬ifyUrl=' . urlencode($this->notifyUrl); |
|
35 | - } |
|
36 | - return array(true, "valid", $httpBody); //视频转码参数合法,返回http请求体 |
|
37 | - } |
|
25 | + /**构建多媒体转码所需的http请求体*/ |
|
26 | + public function getOptionsHttpBody() |
|
27 | + { |
|
28 | + //必须的参数 |
|
29 | + $httpBody = 'input=' . $this->getInputResId(); |
|
30 | + $httpBody .= '&output=' . $this->getOutputResId(); |
|
31 | + $httpBody .= '&time=' . $this->time; |
|
32 | + // 可选的参数 |
|
33 | + if (isset($this->notifyUrl)) { |
|
34 | + $httpBody .= '¬ifyUrl=' . urlencode($this->notifyUrl); |
|
35 | + } |
|
36 | + return array(true, "valid", $httpBody); //视频转码参数合法,返回http请求体 |
|
37 | + } |
|
38 | 38 | |
39 | - /**设置视频截图的位置,单位为毫秒。改参数必须设置*/ |
|
40 | - public function setTime($time) |
|
41 | - { |
|
42 | - $this->time = $time; |
|
43 | - } |
|
44 | - /**设置截图完成后的通知url。可选*/ |
|
45 | - public function setNotifyUrl($notifyUrl) |
|
46 | - { |
|
47 | - $this->notifyUrl = $notifyUrl; |
|
48 | - } |
|
39 | + /**设置视频截图的位置,单位为毫秒。改参数必须设置*/ |
|
40 | + public function setTime($time) |
|
41 | + { |
|
42 | + $this->time = $time; |
|
43 | + } |
|
44 | + /**设置截图完成后的通知url。可选*/ |
|
45 | + public function setNotifyUrl($notifyUrl) |
|
46 | + { |
|
47 | + $this->notifyUrl = $notifyUrl; |
|
48 | + } |
|
49 | 49 | } |
@@ -3,138 +3,138 @@ |
||
3 | 3 | |
4 | 4 | class MimeTypes |
5 | 5 | { |
6 | - public static $mime_types = array( |
|
7 | - '3gp' => 'video/3gpp', 'ai' => 'application/postscript', |
|
8 | - 'aif' => 'audio/x-aiff', 'aifc' => 'audio/x-aiff', |
|
9 | - 'aiff' => 'audio/x-aiff', 'asc' => 'text/plain', |
|
10 | - 'atom' => 'application/atom+xml', 'au' => 'audio/basic', |
|
11 | - 'avi' => 'video/x-msvideo', 'bcpio' => 'application/x-bcpio', |
|
12 | - 'bin' => 'application/octet-stream', 'bmp' => 'image/bmp', |
|
13 | - 'cdf' => 'application/x-netcdf', 'cgm' => 'image/cgm', |
|
14 | - 'class' => 'application/octet-stream', |
|
15 | - 'cpio' => 'application/x-cpio', |
|
16 | - 'cpt' => 'application/mac-compactpro', |
|
17 | - 'csh' => 'application/x-csh', 'css' => 'text/css', |
|
18 | - 'dcr' => 'application/x-director', 'dif' => 'video/x-dv', |
|
19 | - 'dir' => 'application/x-director', 'djv' => 'image/vnd.djvu', |
|
20 | - 'djvu' => 'image/vnd.djvu', |
|
21 | - 'dll' => 'application/octet-stream', |
|
22 | - 'dmg' => 'application/octet-stream', |
|
23 | - 'dms' => 'application/octet-stream', |
|
24 | - 'doc' => 'application/msword', 'dtd' => 'application/xml-dtd', |
|
25 | - 'dv' => 'video/x-dv', 'dvi' => 'application/x-dvi', |
|
26 | - 'dxr' => 'application/x-director', |
|
27 | - 'eps' => 'application/postscript', 'etx' => 'text/x-setext', |
|
28 | - 'exe' => 'application/octet-stream', |
|
29 | - 'ez' => 'application/andrew-inset', 'flv' => 'video/x-flv', |
|
30 | - 'gif' => 'image/gif', 'gram' => 'application/srgs', |
|
31 | - 'grxml' => 'application/srgs+xml', |
|
32 | - 'gtar' => 'application/x-gtar', 'gz' => 'application/x-gzip', |
|
33 | - 'hdf' => 'application/x-hdf', |
|
34 | - 'hqx' => 'application/mac-binhex40', 'htm' => 'text/html', |
|
35 | - 'html' => 'text/html', 'ice' => 'x-conference/x-cooltalk', |
|
36 | - 'ico' => 'image/x-icon', 'ics' => 'text/calendar', |
|
37 | - 'ief' => 'image/ief', 'ifb' => 'text/calendar', |
|
38 | - 'iges' => 'model/iges', 'igs' => 'model/iges', |
|
39 | - 'jnlp' => 'application/x-java-jnlp-file', 'jp2' => 'image/jp2', |
|
40 | - 'jpe' => 'image/jpeg', 'jpeg' => 'image/jpeg', |
|
41 | - 'jpg' => 'image/jpeg', 'js' => 'application/x-javascript', |
|
42 | - 'kar' => 'audio/midi', 'latex' => 'application/x-latex', |
|
43 | - 'lha' => 'application/octet-stream', |
|
44 | - 'lzh' => 'application/octet-stream', |
|
45 | - 'm3u' => 'audio/x-mpegurl', 'm4a' => 'audio/mp4a-latm', |
|
46 | - 'm4p' => 'audio/mp4a-latm', 'm4u' => 'video/vnd.mpegurl', |
|
47 | - 'm4v' => 'video/x-m4v', 'mac' => 'image/x-macpaint', |
|
48 | - 'man' => 'application/x-troff-man', |
|
49 | - 'mathml' => 'application/mathml+xml', |
|
50 | - 'me' => 'application/x-troff-me', 'mesh' => 'model/mesh', |
|
51 | - 'mid' => 'audio/midi', 'midi' => 'audio/midi', |
|
52 | - 'mif' => 'application/vnd.mif', 'mov' => 'video/quicktime', |
|
53 | - 'movie' => 'video/x-sgi-movie', 'mp2' => 'audio/mpeg', |
|
54 | - 'mp3' => 'audio/mpeg', 'mp4' => 'video/mp4', |
|
55 | - 'mpe' => 'video/mpeg', 'mpeg' => 'video/mpeg', |
|
56 | - 'mpg' => 'video/mpeg', 'mpga' => 'audio/mpeg', |
|
57 | - 'ms' => 'application/x-troff-ms', 'msh' => 'model/mesh', |
|
58 | - 'mxu' => 'video/vnd.mpegurl', 'nc' => 'application/x-netcdf', |
|
59 | - 'oda' => 'application/oda', 'ogg' => 'application/ogg', |
|
60 | - 'ogv' => 'video/ogv', 'pbm' => 'image/x-portable-bitmap', |
|
61 | - 'pct' => 'image/pict', 'pdb' => 'chemical/x-pdb', |
|
62 | - 'pdf' => 'application/pdf', |
|
63 | - 'pgm' => 'image/x-portable-graymap', |
|
64 | - 'pgn' => 'application/x-chess-pgn', 'pic' => 'image/pict', |
|
65 | - 'pict' => 'image/pict', 'png' => 'image/png', |
|
66 | - 'pnm' => 'image/x-portable-anymap', |
|
67 | - 'pnt' => 'image/x-macpaint', 'pntg' => 'image/x-macpaint', |
|
68 | - 'ppm' => 'image/x-portable-pixmap', |
|
69 | - 'ppt' => 'application/vnd.ms-powerpoint', |
|
70 | - 'ps' => 'application/postscript', 'qt' => 'video/quicktime', |
|
71 | - 'qti' => 'image/x-quicktime', 'qtif' => 'image/x-quicktime', |
|
72 | - 'ra' => 'audio/x-pn-realaudio', |
|
73 | - 'ram' => 'audio/x-pn-realaudio', 'ras' => 'image/x-cmu-raster', |
|
74 | - 'rdf' => 'application/rdf+xml', 'rgb' => 'image/x-rgb', |
|
75 | - 'rm' => 'application/vnd.rn-realmedia', |
|
76 | - 'roff' => 'application/x-troff', 'rtf' => 'text/rtf', |
|
77 | - 'rtx' => 'text/richtext', 'sgm' => 'text/sgml', |
|
78 | - 'sgml' => 'text/sgml', 'sh' => 'application/x-sh', |
|
79 | - 'shar' => 'application/x-shar', 'silo' => 'model/mesh', |
|
80 | - 'sit' => 'application/x-stuffit', |
|
81 | - 'skd' => 'application/x-koan', 'skm' => 'application/x-koan', |
|
82 | - 'skp' => 'application/x-koan', 'skt' => 'application/x-koan', |
|
83 | - 'smi' => 'application/smil', 'smil' => 'application/smil', |
|
84 | - 'snd' => 'audio/basic', 'so' => 'application/octet-stream', |
|
85 | - 'spl' => 'application/x-futuresplash', |
|
86 | - 'src' => 'application/x-wais-source', |
|
87 | - 'sv4cpio' => 'application/x-sv4cpio', |
|
88 | - 'sv4crc' => 'application/x-sv4crc', 'svg' => 'image/svg+xml', |
|
89 | - 'swf' => 'application/x-shockwave-flash', |
|
90 | - 't' => 'application/x-troff', 'tar' => 'application/x-tar', |
|
91 | - 'tcl' => 'application/x-tcl', 'tex' => 'application/x-tex', |
|
92 | - 'texi' => 'application/x-texinfo', |
|
93 | - 'texinfo' => 'application/x-texinfo', 'tif' => 'image/tiff', |
|
94 | - 'tiff' => 'image/tiff', 'tr' => 'application/x-troff', |
|
95 | - 'tsv' => 'text/tab-separated-values', 'txt' => 'text/plain', |
|
96 | - 'ustar' => 'application/x-ustar', |
|
97 | - 'vcd' => 'application/x-cdlink', 'vrml' => 'model/vrml', |
|
98 | - 'vxml' => 'application/voicexml+xml', 'wav' => 'audio/x-wav', |
|
99 | - 'wbmp' => 'image/vnd.wap.wbmp', |
|
100 | - 'wbxml' => 'application/vnd.wap.wbxml', 'webm' => 'video/webm', |
|
101 | - 'wml' => 'text/vnd.wap.wml', |
|
102 | - 'wmlc' => 'application/vnd.wap.wmlc', |
|
103 | - 'wmls' => 'text/vnd.wap.wmlscript', |
|
104 | - 'wmlsc' => 'application/vnd.wap.wmlscriptc', |
|
105 | - 'wmv' => 'video/x-ms-wmv', 'wrl' => 'model/vrml', |
|
106 | - 'xbm' => 'image/x-xbitmap', 'xht' => 'application/xhtml+xml', |
|
107 | - 'xhtml' => 'application/xhtml+xml', |
|
108 | - 'xls' => 'application/vnd.ms-excel', |
|
109 | - 'xml' => 'application/xml', 'xpm' => 'image/x-xpixmap', |
|
110 | - 'xsl' => 'application/xml', 'xslt' => 'application/xslt+xml', |
|
111 | - 'xul' => 'application/vnd.mozilla.xul+xml', |
|
112 | - 'xwd' => 'image/x-xwindowdump', 'xyz' => 'chemical/x-xyz', |
|
113 | - 'zip' => 'application/zip', |
|
114 | - "apk" => "application/vnd.android.package-archive", |
|
115 | - "bin" => "application/octet-stream", |
|
116 | - "cab" => "application/vnd.ms-cab-compressed", |
|
117 | - "gb" => "application/chinese-gb", |
|
118 | - "gba" => "application/octet-stream", |
|
119 | - "gbc" => "application/octet-stream", |
|
120 | - "jad" => "text/vnd.sun.j2me.app-descriptor", |
|
121 | - "jar" => "application/java-archive", |
|
122 | - "nes" => "application/octet-stream", |
|
123 | - "rar" => "application/x-rar-compressed", |
|
124 | - "sis" => "application/vnd.symbian.install", |
|
125 | - "sisx" => "x-epoc/x-sisx-app", |
|
126 | - "smc" => "application/octet-stream", |
|
127 | - "smd" => "application/octet-stream", |
|
128 | - "swf" => "application/x-shockwave-flash", |
|
129 | - "zip" => "application/x-zip-compressed", |
|
130 | - "wap" => "text/vnd.wap.wml wml", "mrp" => "application/mrp", |
|
131 | - "wma" => "audio/x-ms-wma", |
|
132 | - "lrc" => "application/lrc" |
|
133 | - ); |
|
6 | + public static $mime_types = array( |
|
7 | + '3gp' => 'video/3gpp', 'ai' => 'application/postscript', |
|
8 | + 'aif' => 'audio/x-aiff', 'aifc' => 'audio/x-aiff', |
|
9 | + 'aiff' => 'audio/x-aiff', 'asc' => 'text/plain', |
|
10 | + 'atom' => 'application/atom+xml', 'au' => 'audio/basic', |
|
11 | + 'avi' => 'video/x-msvideo', 'bcpio' => 'application/x-bcpio', |
|
12 | + 'bin' => 'application/octet-stream', 'bmp' => 'image/bmp', |
|
13 | + 'cdf' => 'application/x-netcdf', 'cgm' => 'image/cgm', |
|
14 | + 'class' => 'application/octet-stream', |
|
15 | + 'cpio' => 'application/x-cpio', |
|
16 | + 'cpt' => 'application/mac-compactpro', |
|
17 | + 'csh' => 'application/x-csh', 'css' => 'text/css', |
|
18 | + 'dcr' => 'application/x-director', 'dif' => 'video/x-dv', |
|
19 | + 'dir' => 'application/x-director', 'djv' => 'image/vnd.djvu', |
|
20 | + 'djvu' => 'image/vnd.djvu', |
|
21 | + 'dll' => 'application/octet-stream', |
|
22 | + 'dmg' => 'application/octet-stream', |
|
23 | + 'dms' => 'application/octet-stream', |
|
24 | + 'doc' => 'application/msword', 'dtd' => 'application/xml-dtd', |
|
25 | + 'dv' => 'video/x-dv', 'dvi' => 'application/x-dvi', |
|
26 | + 'dxr' => 'application/x-director', |
|
27 | + 'eps' => 'application/postscript', 'etx' => 'text/x-setext', |
|
28 | + 'exe' => 'application/octet-stream', |
|
29 | + 'ez' => 'application/andrew-inset', 'flv' => 'video/x-flv', |
|
30 | + 'gif' => 'image/gif', 'gram' => 'application/srgs', |
|
31 | + 'grxml' => 'application/srgs+xml', |
|
32 | + 'gtar' => 'application/x-gtar', 'gz' => 'application/x-gzip', |
|
33 | + 'hdf' => 'application/x-hdf', |
|
34 | + 'hqx' => 'application/mac-binhex40', 'htm' => 'text/html', |
|
35 | + 'html' => 'text/html', 'ice' => 'x-conference/x-cooltalk', |
|
36 | + 'ico' => 'image/x-icon', 'ics' => 'text/calendar', |
|
37 | + 'ief' => 'image/ief', 'ifb' => 'text/calendar', |
|
38 | + 'iges' => 'model/iges', 'igs' => 'model/iges', |
|
39 | + 'jnlp' => 'application/x-java-jnlp-file', 'jp2' => 'image/jp2', |
|
40 | + 'jpe' => 'image/jpeg', 'jpeg' => 'image/jpeg', |
|
41 | + 'jpg' => 'image/jpeg', 'js' => 'application/x-javascript', |
|
42 | + 'kar' => 'audio/midi', 'latex' => 'application/x-latex', |
|
43 | + 'lha' => 'application/octet-stream', |
|
44 | + 'lzh' => 'application/octet-stream', |
|
45 | + 'm3u' => 'audio/x-mpegurl', 'm4a' => 'audio/mp4a-latm', |
|
46 | + 'm4p' => 'audio/mp4a-latm', 'm4u' => 'video/vnd.mpegurl', |
|
47 | + 'm4v' => 'video/x-m4v', 'mac' => 'image/x-macpaint', |
|
48 | + 'man' => 'application/x-troff-man', |
|
49 | + 'mathml' => 'application/mathml+xml', |
|
50 | + 'me' => 'application/x-troff-me', 'mesh' => 'model/mesh', |
|
51 | + 'mid' => 'audio/midi', 'midi' => 'audio/midi', |
|
52 | + 'mif' => 'application/vnd.mif', 'mov' => 'video/quicktime', |
|
53 | + 'movie' => 'video/x-sgi-movie', 'mp2' => 'audio/mpeg', |
|
54 | + 'mp3' => 'audio/mpeg', 'mp4' => 'video/mp4', |
|
55 | + 'mpe' => 'video/mpeg', 'mpeg' => 'video/mpeg', |
|
56 | + 'mpg' => 'video/mpeg', 'mpga' => 'audio/mpeg', |
|
57 | + 'ms' => 'application/x-troff-ms', 'msh' => 'model/mesh', |
|
58 | + 'mxu' => 'video/vnd.mpegurl', 'nc' => 'application/x-netcdf', |
|
59 | + 'oda' => 'application/oda', 'ogg' => 'application/ogg', |
|
60 | + 'ogv' => 'video/ogv', 'pbm' => 'image/x-portable-bitmap', |
|
61 | + 'pct' => 'image/pict', 'pdb' => 'chemical/x-pdb', |
|
62 | + 'pdf' => 'application/pdf', |
|
63 | + 'pgm' => 'image/x-portable-graymap', |
|
64 | + 'pgn' => 'application/x-chess-pgn', 'pic' => 'image/pict', |
|
65 | + 'pict' => 'image/pict', 'png' => 'image/png', |
|
66 | + 'pnm' => 'image/x-portable-anymap', |
|
67 | + 'pnt' => 'image/x-macpaint', 'pntg' => 'image/x-macpaint', |
|
68 | + 'ppm' => 'image/x-portable-pixmap', |
|
69 | + 'ppt' => 'application/vnd.ms-powerpoint', |
|
70 | + 'ps' => 'application/postscript', 'qt' => 'video/quicktime', |
|
71 | + 'qti' => 'image/x-quicktime', 'qtif' => 'image/x-quicktime', |
|
72 | + 'ra' => 'audio/x-pn-realaudio', |
|
73 | + 'ram' => 'audio/x-pn-realaudio', 'ras' => 'image/x-cmu-raster', |
|
74 | + 'rdf' => 'application/rdf+xml', 'rgb' => 'image/x-rgb', |
|
75 | + 'rm' => 'application/vnd.rn-realmedia', |
|
76 | + 'roff' => 'application/x-troff', 'rtf' => 'text/rtf', |
|
77 | + 'rtx' => 'text/richtext', 'sgm' => 'text/sgml', |
|
78 | + 'sgml' => 'text/sgml', 'sh' => 'application/x-sh', |
|
79 | + 'shar' => 'application/x-shar', 'silo' => 'model/mesh', |
|
80 | + 'sit' => 'application/x-stuffit', |
|
81 | + 'skd' => 'application/x-koan', 'skm' => 'application/x-koan', |
|
82 | + 'skp' => 'application/x-koan', 'skt' => 'application/x-koan', |
|
83 | + 'smi' => 'application/smil', 'smil' => 'application/smil', |
|
84 | + 'snd' => 'audio/basic', 'so' => 'application/octet-stream', |
|
85 | + 'spl' => 'application/x-futuresplash', |
|
86 | + 'src' => 'application/x-wais-source', |
|
87 | + 'sv4cpio' => 'application/x-sv4cpio', |
|
88 | + 'sv4crc' => 'application/x-sv4crc', 'svg' => 'image/svg+xml', |
|
89 | + 'swf' => 'application/x-shockwave-flash', |
|
90 | + 't' => 'application/x-troff', 'tar' => 'application/x-tar', |
|
91 | + 'tcl' => 'application/x-tcl', 'tex' => 'application/x-tex', |
|
92 | + 'texi' => 'application/x-texinfo', |
|
93 | + 'texinfo' => 'application/x-texinfo', 'tif' => 'image/tiff', |
|
94 | + 'tiff' => 'image/tiff', 'tr' => 'application/x-troff', |
|
95 | + 'tsv' => 'text/tab-separated-values', 'txt' => 'text/plain', |
|
96 | + 'ustar' => 'application/x-ustar', |
|
97 | + 'vcd' => 'application/x-cdlink', 'vrml' => 'model/vrml', |
|
98 | + 'vxml' => 'application/voicexml+xml', 'wav' => 'audio/x-wav', |
|
99 | + 'wbmp' => 'image/vnd.wap.wbmp', |
|
100 | + 'wbxml' => 'application/vnd.wap.wbxml', 'webm' => 'video/webm', |
|
101 | + 'wml' => 'text/vnd.wap.wml', |
|
102 | + 'wmlc' => 'application/vnd.wap.wmlc', |
|
103 | + 'wmls' => 'text/vnd.wap.wmlscript', |
|
104 | + 'wmlsc' => 'application/vnd.wap.wmlscriptc', |
|
105 | + 'wmv' => 'video/x-ms-wmv', 'wrl' => 'model/vrml', |
|
106 | + 'xbm' => 'image/x-xbitmap', 'xht' => 'application/xhtml+xml', |
|
107 | + 'xhtml' => 'application/xhtml+xml', |
|
108 | + 'xls' => 'application/vnd.ms-excel', |
|
109 | + 'xml' => 'application/xml', 'xpm' => 'image/x-xpixmap', |
|
110 | + 'xsl' => 'application/xml', 'xslt' => 'application/xslt+xml', |
|
111 | + 'xul' => 'application/vnd.mozilla.xul+xml', |
|
112 | + 'xwd' => 'image/x-xwindowdump', 'xyz' => 'chemical/x-xyz', |
|
113 | + 'zip' => 'application/zip', |
|
114 | + "apk" => "application/vnd.android.package-archive", |
|
115 | + "bin" => "application/octet-stream", |
|
116 | + "cab" => "application/vnd.ms-cab-compressed", |
|
117 | + "gb" => "application/chinese-gb", |
|
118 | + "gba" => "application/octet-stream", |
|
119 | + "gbc" => "application/octet-stream", |
|
120 | + "jad" => "text/vnd.sun.j2me.app-descriptor", |
|
121 | + "jar" => "application/java-archive", |
|
122 | + "nes" => "application/octet-stream", |
|
123 | + "rar" => "application/x-rar-compressed", |
|
124 | + "sis" => "application/vnd.symbian.install", |
|
125 | + "sisx" => "x-epoc/x-sisx-app", |
|
126 | + "smc" => "application/octet-stream", |
|
127 | + "smd" => "application/octet-stream", |
|
128 | + "swf" => "application/x-shockwave-flash", |
|
129 | + "zip" => "application/x-zip-compressed", |
|
130 | + "wap" => "text/vnd.wap.wml wml", "mrp" => "application/mrp", |
|
131 | + "wma" => "audio/x-ms-wma", |
|
132 | + "lrc" => "application/lrc" |
|
133 | + ); |
|
134 | 134 | |
135 | - public static function getMimetype($ext) |
|
136 | - { |
|
137 | - $ext = strtolower($ext); |
|
138 | - return (isset(self::$mime_types[$ext]) ? self::$mime_types[$ext] : 'application/octet-stream'); |
|
139 | - } |
|
135 | + public static function getMimetype($ext) |
|
136 | + { |
|
137 | + $ext = strtolower($ext); |
|
138 | + return (isset(self::$mime_types[$ext]) ? self::$mime_types[$ext] : 'application/octet-stream'); |
|
139 | + } |
|
140 | 140 | } |
@@ -4,27 +4,27 @@ |
||
4 | 4 | class EncodeUtils |
5 | 5 | { |
6 | 6 | |
7 | - /* |
|
7 | + /* |
|
8 | 8 | * URL安全的Base64编码适用于以URL方式传递Base64编码结果的场景。 |
9 | 9 | * 该编码方式的基本过程是先将内容以Base64格式编码为字符串, |
10 | 10 | * 然后检查该结果字符串,将字符串中的加号+换成中划线-,并且将斜杠/换成下划线_,同时尾部去除等号padding。 |
11 | 11 | */ |
12 | - public static function encodeWithURLSafeBase64($arg) |
|
13 | - { |
|
14 | - if ($arg === null || empty($arg)) { |
|
15 | - return null; |
|
16 | - } |
|
17 | - $result = base64_encode($arg); |
|
18 | - $result = str_replace(array('+', '/', '='), array('-', '_', ''), $result); |
|
19 | - return $result; |
|
20 | - } |
|
12 | + public static function encodeWithURLSafeBase64($arg) |
|
13 | + { |
|
14 | + if ($arg === null || empty($arg)) { |
|
15 | + return null; |
|
16 | + } |
|
17 | + $result = base64_encode($arg); |
|
18 | + $result = str_replace(array('+', '/', '='), array('-', '_', ''), $result); |
|
19 | + return $result; |
|
20 | + } |
|
21 | 21 | |
22 | - public static function newEncodeWithUrlSafeBase64($arg) |
|
23 | - { |
|
24 | - if ($arg === null || empty($arg)) { |
|
25 | - return null; |
|
26 | - } |
|
27 | - $result = preg_replace(array("/\r/", "/\n/"), "", rtrim(base64_encode($arg), '=')); |
|
28 | - return $result; |
|
29 | - } |
|
22 | + public static function newEncodeWithUrlSafeBase64($arg) |
|
23 | + { |
|
24 | + if ($arg === null || empty($arg)) { |
|
25 | + return null; |
|
26 | + } |
|
27 | + $result = preg_replace(array("/\r/", "/\n/"), "", rtrim(base64_encode($arg), '=')); |
|
28 | + return $result; |
|
29 | + } |
|
30 | 30 | } |
@@ -5,88 +5,88 @@ |
||
5 | 5 | /**多媒体转码的参数*/ |
6 | 6 | class MediaEncodeOption extends MediaResOption |
7 | 7 | { |
8 | - /* 以下属性是"视频转码"方法必须的属性 */ |
|
9 | - private $encodeTemplate; //模板名称。可以设置系统或者用户自定义模板。用户模板名称可以登录后台查看和设置,系统模板见附录系统模板列表 |
|
10 | - public $usePreset = 0; //是否使用系统模板。默认0。如果为1,则encodeTemplate必须设置系统模板名称 |
|
11 | - public $force = 0; //是否强制覆盖。默认0,如果为1,当output文件已经存在的时候会强制覆盖,否则不执行转码并结束任务 |
|
12 | - /* 以下属性是"视频转码"方法可选的属性 */ |
|
13 | - private $watermark; //水印资源 |
|
14 | - private $watermarkTemplate; //用户自定义水印模板 |
|
15 | - private $notifyUrl; //通知url,任务结束之后会调用这个url |
|
16 | - private $seek; //截取音视频的开始位置 |
|
17 | - private $duration; //截取音视频的长度 |
|
8 | + /* 以下属性是"视频转码"方法必须的属性 */ |
|
9 | + private $encodeTemplate; //模板名称。可以设置系统或者用户自定义模板。用户模板名称可以登录后台查看和设置,系统模板见附录系统模板列表 |
|
10 | + public $usePreset = 0; //是否使用系统模板。默认0。如果为1,则encodeTemplate必须设置系统模板名称 |
|
11 | + public $force = 0; //是否强制覆盖。默认0,如果为1,当output文件已经存在的时候会强制覆盖,否则不执行转码并结束任务 |
|
12 | + /* 以下属性是"视频转码"方法可选的属性 */ |
|
13 | + private $watermark; //水印资源 |
|
14 | + private $watermarkTemplate; //用户自定义水印模板 |
|
15 | + private $notifyUrl; //通知url,任务结束之后会调用这个url |
|
16 | + private $seek; //截取音视频的开始位置 |
|
17 | + private $duration; //截取音视频的长度 |
|
18 | 18 | |
19 | - /**设置转码模板。必须。模板在顽兔控制台"多媒体处理"中配置*/ |
|
20 | - public function setEncodeTemplate($encodeTemplate) |
|
21 | - { |
|
22 | - $this->encodeTemplate = $encodeTemplate; |
|
23 | - } |
|
24 | - /**检测多媒体转码选项是否合法。如果合法,则返回http请求体<p> 返回格式{$isValid, $message, $httpBody}*/ |
|
25 | - public function checkOptionParameters() |
|
26 | - { |
|
27 | - list($valid, $msg) = parent::checkOptionParameters(); //检测输入输出资源是否合法 |
|
28 | - if (!$valid) { |
|
29 | - return array($valid, $msg, null); |
|
30 | - } |
|
31 | - if (empty($this->encodeTemplate)) { |
|
32 | - return array(false, "encodeTemplate is empty.", null); // 判断是否设置输入输出文件,或者转码模板 |
|
33 | - } |
|
34 | - if (($this->usePreset != 0 && $this->usePreset != 1) || ($this->force != 0 && $this->force != 1)) { |
|
35 | - return array(false, "parameters 'usePreset' or 'force' is invalid.", null); // 判断usePreset和force参数是否为0或1 |
|
36 | - } |
|
37 | - return $this->getOptionsHttpBody(); //返回http请求体 |
|
38 | - } |
|
39 | - /**构建多媒体转码所需的http请求体*/ |
|
40 | - public function getOptionsHttpBody() |
|
41 | - { |
|
42 | - //必须的参数 |
|
43 | - $httpBody = 'input=' . $this->getInputResId(); |
|
44 | - $httpBody .= '&output=' . $this->getOutputResId(); |
|
45 | - $httpBody .= '&encodeTemplate=' . urlencode($this->encodeTemplate); |
|
46 | - $httpBody .= '&usePreset=' . $this->usePreset; |
|
47 | - $httpBody .= '&force=' . $this->force; |
|
48 | - // 可选的参数 |
|
49 | - if (isset($this->watermark)) { |
|
50 | - $httpBody .= '&watermark=' . $this->watermark->buildResourceId(); |
|
51 | - } |
|
52 | - if (isset($this->watermarkTemplate)) { |
|
53 | - $httpBody .= '&watermarkTemplate=' . urlencode($this->watermarkTemplate); |
|
54 | - } |
|
55 | - if (isset($this->notifyUrl)) { |
|
56 | - $httpBody .= '¬ifyUrl=' . urlencode($this->notifyUrl); |
|
57 | - } |
|
58 | - if (isset($this->seek)) { |
|
59 | - $httpBody .= '&seek=' . $this->seek; |
|
60 | - } |
|
61 | - if (isset($this->duration)) { |
|
62 | - $httpBody .= '&duration=' . $this->duration; |
|
63 | - } |
|
64 | - return array(true, "valid", $httpBody); //视频转码参数合法,返回http请求体 |
|
65 | - } |
|
66 | - /*######################以下是可选的参数set方法#######################*/ |
|
67 | - /**设置水印。可选*/ |
|
68 | - public function setWatermark($namespace = null, $dir = null, $name = null) |
|
69 | - { |
|
70 | - $this->watermark = new ResourceInfo($namespace, $dir, $name); |
|
71 | - } |
|
72 | - /**设置用户自定义水印模板。可选*/ |
|
73 | - public function setWatermarkTemplate($watermarkTemplate) |
|
74 | - { |
|
75 | - $this->watermarkTemplate = $watermarkTemplate; |
|
76 | - } |
|
77 | - /**设置转码完成后的通知url。可选*/ |
|
78 | - public function setNotifyUrl($notifyUrl) |
|
79 | - { |
|
80 | - $this->notifyUrl = $notifyUrl; |
|
81 | - } |
|
82 | - /**设置转码起始位置。可选*/ |
|
83 | - public function setSeek($seek) |
|
84 | - { |
|
85 | - $this->seek = $seek; |
|
86 | - } |
|
87 | - /**设置转码长度。可选*/ |
|
88 | - public function setDuration($duration) |
|
89 | - { |
|
90 | - $this->duration = $duration; |
|
91 | - } |
|
19 | + /**设置转码模板。必须。模板在顽兔控制台"多媒体处理"中配置*/ |
|
20 | + public function setEncodeTemplate($encodeTemplate) |
|
21 | + { |
|
22 | + $this->encodeTemplate = $encodeTemplate; |
|
23 | + } |
|
24 | + /**检测多媒体转码选项是否合法。如果合法,则返回http请求体<p> 返回格式{$isValid, $message, $httpBody}*/ |
|
25 | + public function checkOptionParameters() |
|
26 | + { |
|
27 | + list($valid, $msg) = parent::checkOptionParameters(); //检测输入输出资源是否合法 |
|
28 | + if (!$valid) { |
|
29 | + return array($valid, $msg, null); |
|
30 | + } |
|
31 | + if (empty($this->encodeTemplate)) { |
|
32 | + return array(false, "encodeTemplate is empty.", null); // 判断是否设置输入输出文件,或者转码模板 |
|
33 | + } |
|
34 | + if (($this->usePreset != 0 && $this->usePreset != 1) || ($this->force != 0 && $this->force != 1)) { |
|
35 | + return array(false, "parameters 'usePreset' or 'force' is invalid.", null); // 判断usePreset和force参数是否为0或1 |
|
36 | + } |
|
37 | + return $this->getOptionsHttpBody(); //返回http请求体 |
|
38 | + } |
|
39 | + /**构建多媒体转码所需的http请求体*/ |
|
40 | + public function getOptionsHttpBody() |
|
41 | + { |
|
42 | + //必须的参数 |
|
43 | + $httpBody = 'input=' . $this->getInputResId(); |
|
44 | + $httpBody .= '&output=' . $this->getOutputResId(); |
|
45 | + $httpBody .= '&encodeTemplate=' . urlencode($this->encodeTemplate); |
|
46 | + $httpBody .= '&usePreset=' . $this->usePreset; |
|
47 | + $httpBody .= '&force=' . $this->force; |
|
48 | + // 可选的参数 |
|
49 | + if (isset($this->watermark)) { |
|
50 | + $httpBody .= '&watermark=' . $this->watermark->buildResourceId(); |
|
51 | + } |
|
52 | + if (isset($this->watermarkTemplate)) { |
|
53 | + $httpBody .= '&watermarkTemplate=' . urlencode($this->watermarkTemplate); |
|
54 | + } |
|
55 | + if (isset($this->notifyUrl)) { |
|
56 | + $httpBody .= '¬ifyUrl=' . urlencode($this->notifyUrl); |
|
57 | + } |
|
58 | + if (isset($this->seek)) { |
|
59 | + $httpBody .= '&seek=' . $this->seek; |
|
60 | + } |
|
61 | + if (isset($this->duration)) { |
|
62 | + $httpBody .= '&duration=' . $this->duration; |
|
63 | + } |
|
64 | + return array(true, "valid", $httpBody); //视频转码参数合法,返回http请求体 |
|
65 | + } |
|
66 | + /*######################以下是可选的参数set方法#######################*/ |
|
67 | + /**设置水印。可选*/ |
|
68 | + public function setWatermark($namespace = null, $dir = null, $name = null) |
|
69 | + { |
|
70 | + $this->watermark = new ResourceInfo($namespace, $dir, $name); |
|
71 | + } |
|
72 | + /**设置用户自定义水印模板。可选*/ |
|
73 | + public function setWatermarkTemplate($watermarkTemplate) |
|
74 | + { |
|
75 | + $this->watermarkTemplate = $watermarkTemplate; |
|
76 | + } |
|
77 | + /**设置转码完成后的通知url。可选*/ |
|
78 | + public function setNotifyUrl($notifyUrl) |
|
79 | + { |
|
80 | + $this->notifyUrl = $notifyUrl; |
|
81 | + } |
|
82 | + /**设置转码起始位置。可选*/ |
|
83 | + public function setSeek($seek) |
|
84 | + { |
|
85 | + $this->seek = $seek; |
|
86 | + } |
|
87 | + /**设置转码长度。可选*/ |
|
88 | + public function setDuration($duration) |
|
89 | + { |
|
90 | + $this->duration = $duration; |
|
91 | + } |
|
92 | 92 | } |
@@ -6,15 +6,15 @@ discard block |
||
6 | 6 | class MediaEncodeOption extends MediaResOption |
7 | 7 | { |
8 | 8 | /* 以下属性是"视频转码"方法必须的属性 */ |
9 | - private $encodeTemplate; //模板名称。可以设置系统或者用户自定义模板。用户模板名称可以登录后台查看和设置,系统模板见附录系统模板列表 |
|
10 | - public $usePreset = 0; //是否使用系统模板。默认0。如果为1,则encodeTemplate必须设置系统模板名称 |
|
11 | - public $force = 0; //是否强制覆盖。默认0,如果为1,当output文件已经存在的时候会强制覆盖,否则不执行转码并结束任务 |
|
9 | + private $encodeTemplate; //模板名称。可以设置系统或者用户自定义模板。用户模板名称可以登录后台查看和设置,系统模板见附录系统模板列表 |
|
10 | + public $usePreset = 0; //是否使用系统模板。默认0。如果为1,则encodeTemplate必须设置系统模板名称 |
|
11 | + public $force = 0; //是否强制覆盖。默认0,如果为1,当output文件已经存在的时候会强制覆盖,否则不执行转码并结束任务 |
|
12 | 12 | /* 以下属性是"视频转码"方法可选的属性 */ |
13 | - private $watermark; //水印资源 |
|
14 | - private $watermarkTemplate; //用户自定义水印模板 |
|
15 | - private $notifyUrl; //通知url,任务结束之后会调用这个url |
|
16 | - private $seek; //截取音视频的开始位置 |
|
17 | - private $duration; //截取音视频的长度 |
|
13 | + private $watermark; //水印资源 |
|
14 | + private $watermarkTemplate; //用户自定义水印模板 |
|
15 | + private $notifyUrl; //通知url,任务结束之后会调用这个url |
|
16 | + private $seek; //截取音视频的开始位置 |
|
17 | + private $duration; //截取音视频的长度 |
|
18 | 18 | |
19 | 19 | /**设置转码模板。必须。模板在顽兔控制台"多媒体处理"中配置*/ |
20 | 20 | public function setEncodeTemplate($encodeTemplate) |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | $httpBody .= '¬ifyUrl=' . urlencode($this->notifyUrl); |
57 | 57 | } |
58 | 58 | if (isset($this->seek)) { |
59 | - $httpBody .= '&seek=' . $this->seek; |
|
59 | + $httpBody .= '&seek=' . $this->seek; |
|
60 | 60 | } |
61 | 61 | if (isset($this->duration)) { |
62 | 62 | $httpBody .= '&duration=' . $this->duration; |
@@ -6,164 +6,164 @@ |
||
6 | 6 | |
7 | 7 | class UploadOption |
8 | 8 | { |
9 | - /*optionType用于标识UploadOption的类型,即:普通上传、初始化分片上传、分片上传、分片上传完成*/ |
|
10 | - public $optionType; |
|
9 | + /*optionType用于标识UploadOption的类型,即:普通上传、初始化分片上传、分片上传、分片上传完成*/ |
|
10 | + public $optionType; |
|
11 | 11 | |
12 | - /*以下属性是上传时的可选参数。即Rest API中Http请求Body中所需的可选参数*/ |
|
13 | - public $dir; // 顽兔空间的图片路径(如果UploadPolicy中不指定dir属性,则生效) |
|
14 | - public $name; // 上传到服务端的文件名(如果UploadPolicy中不指定name属性,则生效) |
|
15 | - public $metaArray; // 用户自定义的文件meta信息("meta-"为参数前缀, "*"为用户用于渲染的自定义Meta信息名) |
|
16 | - public $varArray; // 用户自定义的魔法变量("var-"为参数前缀, "*"为用户用于渲染的自定义魔法变量名) |
|
17 | - private $md5; // 文件md5值(推荐提供此参数进行一致性检查) |
|
18 | - private $size; // 文件大小 |
|
19 | - private $content; // 文件内容(在http请求体Body中必须位于参数的最后一位) |
|
12 | + /*以下属性是上传时的可选参数。即Rest API中Http请求Body中所需的可选参数*/ |
|
13 | + public $dir; // 顽兔空间的图片路径(如果UploadPolicy中不指定dir属性,则生效) |
|
14 | + public $name; // 上传到服务端的文件名(如果UploadPolicy中不指定name属性,则生效) |
|
15 | + public $metaArray; // 用户自定义的文件meta信息("meta-"为参数前缀, "*"为用户用于渲染的自定义Meta信息名) |
|
16 | + public $varArray; // 用户自定义的魔法变量("var-"为参数前缀, "*"为用户用于渲染的自定义魔法变量名) |
|
17 | + private $md5; // 文件md5值(推荐提供此参数进行一致性检查) |
|
18 | + private $size; // 文件大小 |
|
19 | + private $content; // 文件内容(在http请求体Body中必须位于参数的最后一位) |
|
20 | 20 | |
21 | - /*以下属性是用户根据自己应用需求,可选的配置*/ |
|
22 | - public $blockSize; // 文件分片的大小。针对分片上传。 |
|
23 | - public $timeout; // 进行http连接的超时时间 |
|
24 | - public $httpReTry; // http失败自动重试。0 or 1 |
|
21 | + /*以下属性是用户根据自己应用需求,可选的配置*/ |
|
22 | + public $blockSize; // 文件分片的大小。针对分片上传。 |
|
23 | + public $timeout; // 进行http连接的超时时间 |
|
24 | + public $httpReTry; // http失败自动重试。0 or 1 |
|
25 | 25 | |
26 | - /*以下属性是用于分片上传时的参数,仅用于分片上传。用户在调用分片上传时可以选择配置。*/ |
|
27 | - private $uploadId; // OSS分片上传ID(OSS用于区分上传的id) |
|
28 | - private $uniqueId; // 服务上传唯一ID(多媒体服务用于区分上传的id) |
|
29 | - private $partNumber; // 分片文件块上传成功后返回的文件块编号 |
|
30 | - private $eTag; // 分片文件块上传成功后返回的Tag标签(由md5和其他标记组成) |
|
31 | - private $array_PartNum_ETag; // 分片上传服务端返回的所有 块编号partNumber 和 标记ETag |
|
26 | + /*以下属性是用于分片上传时的参数,仅用于分片上传。用户在调用分片上传时可以选择配置。*/ |
|
27 | + private $uploadId; // OSS分片上传ID(OSS用于区分上传的id) |
|
28 | + private $uniqueId; // 服务上传唯一ID(多媒体服务用于区分上传的id) |
|
29 | + private $partNumber; // 分片文件块上传成功后返回的文件块编号 |
|
30 | + private $eTag; // 分片文件块上传成功后返回的Tag标签(由md5和其他标记组成) |
|
31 | + private $array_PartNum_ETag; // 分片上传服务端返回的所有 块编号partNumber 和 标记ETag |
|
32 | 32 | |
33 | - public function __construct() |
|
34 | - { |
|
35 | - $this->optionType = UpOptionType::COMMON_UPLOAD_TYPE; //默认普通上传类型 |
|
36 | - $this->metaArray = array(); |
|
37 | - $this->varArray = array(); |
|
38 | - $this->blockSize = Conf::BLOCK_DEFF_SIZE; //默认2M |
|
39 | - $this->timeout = Conf::HTTP_TIMEOUT; //默认超时30s |
|
40 | - $this->httpReTry = Conf::HTTP_RETRY; //默认重试 |
|
41 | - $this->array_PartNum_ETag = array(); |
|
42 | - } |
|
43 | - /**得到上传时http请求体所需的参数*/ |
|
44 | - public function getParaArray() |
|
45 | - { |
|
46 | - switch ($this->optionType) { |
|
47 | - case UpOptionType::COMMON_UPLOAD_TYPE: |
|
48 | - case UpOptionType::BLOCK_INIT_UPLOAD: |
|
49 | - return $this->getParasCommonBlockInit(); |
|
50 | - case UpOptionType::BLOCK_RUN_UPLOAD: |
|
51 | - return $this->getParasBlockRun(); |
|
52 | - case UpOptionType::BLOCK_COMPLETE_UPLOAD: |
|
53 | - return $this->getParasBlockComplete(); |
|
54 | - case UpOptionType::BLOCK_CANCEL_UPLOAD: |
|
55 | - return $this->getParasBlockCancel(); |
|
56 | - default: |
|
57 | - return null; |
|
58 | - } |
|
59 | - } |
|
60 | - /** 构造 普通上传 或者 初始化分片上传 所需的参数 */ |
|
61 | - private function getParasCommonBlockInit() |
|
62 | - { |
|
63 | - $paraArray = array(); |
|
64 | - if (isset($this->dir)) { |
|
65 | - $paraArray['dir'] = $this->dir; |
|
66 | - } |
|
67 | - if (isset($this->name)) { |
|
68 | - $paraArray['name'] = $this->name; |
|
69 | - } |
|
70 | - $paraArray['md5'] = md5($this->content); // 计算文件md5 |
|
71 | - $paraArray['size'] = strlen($this->content); // 计算文件大小 |
|
72 | - $paraArray['content'] = $this->content; |
|
73 | - $this->createMetaVars($paraArray, "meta", $this->metaArray); |
|
74 | - $this->createMetaVars($paraArray, "var", $this->varArray); |
|
75 | - return $paraArray; |
|
76 | - } |
|
77 | - /** 构造 分片上传过程中 所需的参数 */ |
|
78 | - private function getParasBlockRun() |
|
79 | - { |
|
80 | - $paraArray = array(); |
|
81 | - $paraArray['uploadId'] = $this->uploadId; |
|
82 | - $paraArray['id'] = $this->uniqueId; |
|
83 | - $paraArray['partNumber'] = $this->partNumber; |
|
84 | - $paraArray['md5'] = md5($this->content); // 计算文件md5 |
|
85 | - $paraArray['size'] = strlen($this->content); // 计算文件大小 |
|
86 | - $paraArray['content'] = $this->content; |
|
87 | - return $paraArray; |
|
88 | - } |
|
89 | - /** 构造 分片上传完成时 所需的参数 */ |
|
90 | - private function getParasBlockComplete() |
|
91 | - { |
|
92 | - $paraArray = array(); |
|
93 | - $paraArray['uploadId'] = $this->uploadId; |
|
94 | - $paraArray['id'] = $this->uniqueId; |
|
95 | - $paraArray['md5'] = $this->md5; |
|
96 | - $parts = EncodeUtils::encodeWithURLSafeBase64(json_encode($this->array_PartNum_ETag)); |
|
97 | - $paraArray['parts'] = $parts; // 所有文件块的编号partNumber 和 标记ETag,需要进行base64的编码 |
|
98 | - return $paraArray; |
|
99 | - } |
|
100 | - /** 构造 分片上传取消 所需的参数 */ |
|
101 | - private function getParasBlockCancel() |
|
102 | - { |
|
103 | - return array('id' => $this->uniqueId, 'uploadId' => $this->uploadId); |
|
104 | - } |
|
105 | - /** |
|
106 | - * 构建上传http请求体的meta-*和var-*选项参数。将tempArr中的元素加上前缀prefix,然后保存到paraArr中 |
|
107 | - * @param array $paraArr 最终的数组 |
|
108 | - * @param string $prefix 前缀 |
|
109 | - * @param array $tempArr 待添加的数组 |
|
110 | - * @return array $paraArr 最终的数组 |
|
111 | - */ |
|
112 | - private function createMetaVars($paraArr, $prefix, $tempArr) |
|
113 | - { |
|
114 | - foreach ($tempArr as $key => $val) { |
|
115 | - $key = $prefix . '-' . $key; |
|
116 | - $paraArr[$key] = $val; |
|
117 | - } |
|
118 | - return $paraArr; |
|
119 | - } |
|
120 | - /**设置待上传的数据。该方法开发者不需要调用,该方法根据用户数据自动调用 |
|
33 | + public function __construct() |
|
34 | + { |
|
35 | + $this->optionType = UpOptionType::COMMON_UPLOAD_TYPE; //默认普通上传类型 |
|
36 | + $this->metaArray = array(); |
|
37 | + $this->varArray = array(); |
|
38 | + $this->blockSize = Conf::BLOCK_DEFF_SIZE; //默认2M |
|
39 | + $this->timeout = Conf::HTTP_TIMEOUT; //默认超时30s |
|
40 | + $this->httpReTry = Conf::HTTP_RETRY; //默认重试 |
|
41 | + $this->array_PartNum_ETag = array(); |
|
42 | + } |
|
43 | + /**得到上传时http请求体所需的参数*/ |
|
44 | + public function getParaArray() |
|
45 | + { |
|
46 | + switch ($this->optionType) { |
|
47 | + case UpOptionType::COMMON_UPLOAD_TYPE: |
|
48 | + case UpOptionType::BLOCK_INIT_UPLOAD: |
|
49 | + return $this->getParasCommonBlockInit(); |
|
50 | + case UpOptionType::BLOCK_RUN_UPLOAD: |
|
51 | + return $this->getParasBlockRun(); |
|
52 | + case UpOptionType::BLOCK_COMPLETE_UPLOAD: |
|
53 | + return $this->getParasBlockComplete(); |
|
54 | + case UpOptionType::BLOCK_CANCEL_UPLOAD: |
|
55 | + return $this->getParasBlockCancel(); |
|
56 | + default: |
|
57 | + return null; |
|
58 | + } |
|
59 | + } |
|
60 | + /** 构造 普通上传 或者 初始化分片上传 所需的参数 */ |
|
61 | + private function getParasCommonBlockInit() |
|
62 | + { |
|
63 | + $paraArray = array(); |
|
64 | + if (isset($this->dir)) { |
|
65 | + $paraArray['dir'] = $this->dir; |
|
66 | + } |
|
67 | + if (isset($this->name)) { |
|
68 | + $paraArray['name'] = $this->name; |
|
69 | + } |
|
70 | + $paraArray['md5'] = md5($this->content); // 计算文件md5 |
|
71 | + $paraArray['size'] = strlen($this->content); // 计算文件大小 |
|
72 | + $paraArray['content'] = $this->content; |
|
73 | + $this->createMetaVars($paraArray, "meta", $this->metaArray); |
|
74 | + $this->createMetaVars($paraArray, "var", $this->varArray); |
|
75 | + return $paraArray; |
|
76 | + } |
|
77 | + /** 构造 分片上传过程中 所需的参数 */ |
|
78 | + private function getParasBlockRun() |
|
79 | + { |
|
80 | + $paraArray = array(); |
|
81 | + $paraArray['uploadId'] = $this->uploadId; |
|
82 | + $paraArray['id'] = $this->uniqueId; |
|
83 | + $paraArray['partNumber'] = $this->partNumber; |
|
84 | + $paraArray['md5'] = md5($this->content); // 计算文件md5 |
|
85 | + $paraArray['size'] = strlen($this->content); // 计算文件大小 |
|
86 | + $paraArray['content'] = $this->content; |
|
87 | + return $paraArray; |
|
88 | + } |
|
89 | + /** 构造 分片上传完成时 所需的参数 */ |
|
90 | + private function getParasBlockComplete() |
|
91 | + { |
|
92 | + $paraArray = array(); |
|
93 | + $paraArray['uploadId'] = $this->uploadId; |
|
94 | + $paraArray['id'] = $this->uniqueId; |
|
95 | + $paraArray['md5'] = $this->md5; |
|
96 | + $parts = EncodeUtils::encodeWithURLSafeBase64(json_encode($this->array_PartNum_ETag)); |
|
97 | + $paraArray['parts'] = $parts; // 所有文件块的编号partNumber 和 标记ETag,需要进行base64的编码 |
|
98 | + return $paraArray; |
|
99 | + } |
|
100 | + /** 构造 分片上传取消 所需的参数 */ |
|
101 | + private function getParasBlockCancel() |
|
102 | + { |
|
103 | + return array('id' => $this->uniqueId, 'uploadId' => $this->uploadId); |
|
104 | + } |
|
105 | + /** |
|
106 | + * 构建上传http请求体的meta-*和var-*选项参数。将tempArr中的元素加上前缀prefix,然后保存到paraArr中 |
|
107 | + * @param array $paraArr 最终的数组 |
|
108 | + * @param string $prefix 前缀 |
|
109 | + * @param array $tempArr 待添加的数组 |
|
110 | + * @return array $paraArr 最终的数组 |
|
111 | + */ |
|
112 | + private function createMetaVars($paraArr, $prefix, $tempArr) |
|
113 | + { |
|
114 | + foreach ($tempArr as $key => $val) { |
|
115 | + $key = $prefix . '-' . $key; |
|
116 | + $paraArr[$key] = $val; |
|
117 | + } |
|
118 | + return $paraArr; |
|
119 | + } |
|
120 | + /**设置待上传的数据。该方法开发者不需要调用,该方法根据用户数据自动调用 |
|
121 | 121 | * @param string $data 字符串 */ |
122 | - public function setContent($data) |
|
123 | - { |
|
124 | - $this->content = $data; |
|
125 | - } |
|
126 | - /**设置MD5值。该方法主要用于在分片上传完成时调用 |
|
122 | + public function setContent($data) |
|
123 | + { |
|
124 | + $this->content = $data; |
|
125 | + } |
|
126 | + /**设置MD5值。该方法主要用于在分片上传完成时调用 |
|
127 | 127 | * @param string $value md5值 */ |
128 | - public function setMd5($value) |
|
129 | - { |
|
130 | - $this->md5 = $value; |
|
131 | - } |
|
132 | - /**得到MD5值。该方法主要用于在分片上传完成时调用*/ |
|
133 | - public function getMd5() |
|
134 | - { |
|
135 | - return $this->md5; |
|
136 | - } |
|
137 | - /*下面四个函数均是用于分片上传时的设置,开发者不需要调用*/ |
|
138 | - /**分片上传时用于设置uploadId */ |
|
139 | - public function setUploadId($uploadId) |
|
140 | - { |
|
141 | - $this->uploadId = $uploadId; |
|
142 | - } |
|
143 | - /**分片上传时用于设置id */ |
|
144 | - public function setUniqueIdId($id) |
|
145 | - { |
|
146 | - $this->uniqueId = $id; |
|
147 | - } |
|
148 | - /**分片上传时,用于获取分片上传时的块编号partNumber */ |
|
149 | - public function getPartNumber() |
|
150 | - { |
|
151 | - return $this->partNumber; |
|
152 | - } |
|
153 | - /**分片上传时,用于设置分片上传时的块编号partNumber */ |
|
154 | - public function setPartNumber($partNumber) |
|
155 | - { |
|
156 | - $this->partNumber = $partNumber; |
|
157 | - } |
|
158 | - /**分片上传过程中,用于保存所有的 块编号partNumber 和 标记ETag*/ |
|
159 | - public function addPartNumberAndETag($partNumber, $eTag) |
|
160 | - { |
|
161 | - $this->eTag = $eTag; |
|
162 | - array_push($this->array_PartNum_ETag, array("partNumber" => $partNumber, "eTag" => $eTag)); |
|
163 | - } |
|
164 | - /**检测分片上传的参数。即uploadId、uniqueId是否有值*/ |
|
165 | - public function checkMutipartParas() |
|
166 | - { |
|
167 | - return isset($this->uploadId) && isset($this->uniqueId); |
|
168 | - } |
|
128 | + public function setMd5($value) |
|
129 | + { |
|
130 | + $this->md5 = $value; |
|
131 | + } |
|
132 | + /**得到MD5值。该方法主要用于在分片上传完成时调用*/ |
|
133 | + public function getMd5() |
|
134 | + { |
|
135 | + return $this->md5; |
|
136 | + } |
|
137 | + /*下面四个函数均是用于分片上传时的设置,开发者不需要调用*/ |
|
138 | + /**分片上传时用于设置uploadId */ |
|
139 | + public function setUploadId($uploadId) |
|
140 | + { |
|
141 | + $this->uploadId = $uploadId; |
|
142 | + } |
|
143 | + /**分片上传时用于设置id */ |
|
144 | + public function setUniqueIdId($id) |
|
145 | + { |
|
146 | + $this->uniqueId = $id; |
|
147 | + } |
|
148 | + /**分片上传时,用于获取分片上传时的块编号partNumber */ |
|
149 | + public function getPartNumber() |
|
150 | + { |
|
151 | + return $this->partNumber; |
|
152 | + } |
|
153 | + /**分片上传时,用于设置分片上传时的块编号partNumber */ |
|
154 | + public function setPartNumber($partNumber) |
|
155 | + { |
|
156 | + $this->partNumber = $partNumber; |
|
157 | + } |
|
158 | + /**分片上传过程中,用于保存所有的 块编号partNumber 和 标记ETag*/ |
|
159 | + public function addPartNumberAndETag($partNumber, $eTag) |
|
160 | + { |
|
161 | + $this->eTag = $eTag; |
|
162 | + array_push($this->array_PartNum_ETag, array("partNumber" => $partNumber, "eTag" => $eTag)); |
|
163 | + } |
|
164 | + /**检测分片上传的参数。即uploadId、uniqueId是否有值*/ |
|
165 | + public function checkMutipartParas() |
|
166 | + { |
|
167 | + return isset($this->uploadId) && isset($this->uniqueId); |
|
168 | + } |
|
169 | 169 | } |
@@ -10,34 +10,34 @@ |
||
10 | 10 | public $optionType; |
11 | 11 | |
12 | 12 | /*以下属性是上传时的可选参数。即Rest API中Http请求Body中所需的可选参数*/ |
13 | - public $dir; // 顽兔空间的图片路径(如果UploadPolicy中不指定dir属性,则生效) |
|
14 | - public $name; // 上传到服务端的文件名(如果UploadPolicy中不指定name属性,则生效) |
|
15 | - public $metaArray; // 用户自定义的文件meta信息("meta-"为参数前缀, "*"为用户用于渲染的自定义Meta信息名) |
|
16 | - public $varArray; // 用户自定义的魔法变量("var-"为参数前缀, "*"为用户用于渲染的自定义魔法变量名) |
|
17 | - private $md5; // 文件md5值(推荐提供此参数进行一致性检查) |
|
18 | - private $size; // 文件大小 |
|
19 | - private $content; // 文件内容(在http请求体Body中必须位于参数的最后一位) |
|
13 | + public $dir; // 顽兔空间的图片路径(如果UploadPolicy中不指定dir属性,则生效) |
|
14 | + public $name; // 上传到服务端的文件名(如果UploadPolicy中不指定name属性,则生效) |
|
15 | + public $metaArray; // 用户自定义的文件meta信息("meta-"为参数前缀, "*"为用户用于渲染的自定义Meta信息名) |
|
16 | + public $varArray; // 用户自定义的魔法变量("var-"为参数前缀, "*"为用户用于渲染的自定义魔法变量名) |
|
17 | + private $md5; // 文件md5值(推荐提供此参数进行一致性检查) |
|
18 | + private $size; // 文件大小 |
|
19 | + private $content; // 文件内容(在http请求体Body中必须位于参数的最后一位) |
|
20 | 20 | |
21 | 21 | /*以下属性是用户根据自己应用需求,可选的配置*/ |
22 | - public $blockSize; // 文件分片的大小。针对分片上传。 |
|
23 | - public $timeout; // 进行http连接的超时时间 |
|
24 | - public $httpReTry; // http失败自动重试。0 or 1 |
|
22 | + public $blockSize; // 文件分片的大小。针对分片上传。 |
|
23 | + public $timeout; // 进行http连接的超时时间 |
|
24 | + public $httpReTry; // http失败自动重试。0 or 1 |
|
25 | 25 | |
26 | 26 | /*以下属性是用于分片上传时的参数,仅用于分片上传。用户在调用分片上传时可以选择配置。*/ |
27 | - private $uploadId; // OSS分片上传ID(OSS用于区分上传的id) |
|
28 | - private $uniqueId; // 服务上传唯一ID(多媒体服务用于区分上传的id) |
|
29 | - private $partNumber; // 分片文件块上传成功后返回的文件块编号 |
|
30 | - private $eTag; // 分片文件块上传成功后返回的Tag标签(由md5和其他标记组成) |
|
31 | - private $array_PartNum_ETag; // 分片上传服务端返回的所有 块编号partNumber 和 标记ETag |
|
27 | + private $uploadId; // OSS分片上传ID(OSS用于区分上传的id) |
|
28 | + private $uniqueId; // 服务上传唯一ID(多媒体服务用于区分上传的id) |
|
29 | + private $partNumber; // 分片文件块上传成功后返回的文件块编号 |
|
30 | + private $eTag; // 分片文件块上传成功后返回的Tag标签(由md5和其他标记组成) |
|
31 | + private $array_PartNum_ETag; // 分片上传服务端返回的所有 块编号partNumber 和 标记ETag |
|
32 | 32 | |
33 | 33 | public function __construct() |
34 | 34 | { |
35 | 35 | $this->optionType = UpOptionType::COMMON_UPLOAD_TYPE; //默认普通上传类型 |
36 | 36 | $this->metaArray = array(); |
37 | 37 | $this->varArray = array(); |
38 | - $this->blockSize = Conf::BLOCK_DEFF_SIZE; //默认2M |
|
39 | - $this->timeout = Conf::HTTP_TIMEOUT; //默认超时30s |
|
40 | - $this->httpReTry = Conf::HTTP_RETRY; //默认重试 |
|
38 | + $this->blockSize = Conf::BLOCK_DEFF_SIZE; //默认2M |
|
39 | + $this->timeout = Conf::HTTP_TIMEOUT; //默认超时30s |
|
40 | + $this->httpReTry = Conf::HTTP_RETRY; //默认重试 |
|
41 | 41 | $this->array_PartNum_ETag = array(); |
42 | 42 | } |
43 | 43 | /**得到上传时http请求体所需的参数*/ |