Proxy.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. namespace app\common\library\ucloud;
  3. class Proxy
  4. {
  5. function __construct()
  6. {
  7. require("./conf.php");
  8. require("./http.php");
  9. require("./utils.php");
  10. require("./digest.php");
  11. }
  12. //------------------------------普通上传------------------------------
  13. public static function UCloud_PutFile($bucket, $key, $file)
  14. {
  15. $action_type = ActionType::PUTFILE;
  16. $err = CheckConfig(ActionType::PUTFILE);
  17. if ($err != null) {
  18. return array(null, $err);
  19. }
  20. $f = @fopen($file, "r");
  21. if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
  22. $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
  23. $host = $bucket . $UCLOUD_PROXY_SUFFIX;
  24. $path = $key;
  25. $content = @fread($f, filesize($file));
  26. list($mimetype, $err) = GetFileMimeType($file);
  27. if ($err) {
  28. fclose($f);
  29. return array("", $err);
  30. }
  31. $req = new HTTP_Request('PUT', array('host' => $host, 'path' => $path), $content, $bucket, $key, $action_type);
  32. $req->Header['Expect'] = '';
  33. $req->Header['Content-Type'] = $mimetype;
  34. $client = new UCloud_AuthHttpClient(null, $mimetype);
  35. list($data, $err) = UCloud_Client_Call($client, $req);
  36. fclose($f);
  37. return array($data, $err);
  38. }
  39. //------------------------------表单上传------------------------------
  40. public static function UCloud_MultipartForm($bucket, $key, $file)
  41. {
  42. $action_type = ActionType::POSTFILE;
  43. $err = CheckConfig(ActionType::POSTFILE);
  44. if ($err != null) {
  45. return array(null, $err);
  46. }
  47. $f = @fopen($file, "r");
  48. if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
  49. $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
  50. $host = $bucket . $UCLOUD_PROXY_SUFFIX;
  51. $path = "";
  52. $fsize = filesize($file);
  53. $content = "";
  54. if ($fsize != 0) {
  55. $content = @fread($f, filesize($file));
  56. if ($content == FALSE) {
  57. fclose($f);
  58. return array(null, new UCloud_Error(0, -1, "read file error"));
  59. }
  60. }
  61. list($mimetype, $err) = GetFileMimeType($file);
  62. if ($err) {
  63. fclose($f);
  64. return array("", $err);
  65. }
  66. $req = new HTTP_Request('POST', array('host' => $host, 'path' => $path), $content, $bucket, $key, $action_type);
  67. $req->Header['Expect'] = '';
  68. $token = UCloud_SignRequest(null, $req, $mimetype);
  69. $fields = array('Authorization' => $token, 'FileName' => $key);
  70. $files = array('files' => array('file', $file, $content, $mimetype));
  71. $client = new UCloud_AuthHttpClient(null, NO_AUTH_CHECK);
  72. list($data, $err) = UCloud_Client_CallWithMultipartForm($client, $req, $fields, $files);
  73. fclose($f);
  74. return array($data, $err);
  75. }
  76. //------------------------------分片上传------------------------------
  77. public static function UCloud_MInit($bucket, $key)
  78. {
  79. $err = CheckConfig(ActionType::MINIT);
  80. if ($err != null) {
  81. return array(null, $err);
  82. }
  83. $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
  84. $host = $bucket . $UCLOUD_PROXY_SUFFIX;
  85. $path = $key;
  86. $querys = array(
  87. "uploads" => ""
  88. );
  89. $req = new HTTP_Request('POST', array('host' => $host, 'path' => $path, 'query' => $querys), null, $bucket, $key);
  90. $req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
  91. $client = new UCloud_AuthHttpClient(null);
  92. return UCloud_Client_Call($client, $req);
  93. }
  94. //@results: (tagList, err)
  95. public static function UCloud_MUpload($bucket, $key, $file, $uploadId, $blkSize, $partNumber = 0)
  96. {
  97. $err = CheckConfig(ActionType::MUPLOAD);
  98. if ($err != null) {
  99. return array(null, $err);
  100. }
  101. $f = @fopen($file, "r");
  102. if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
  103. $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
  104. $etagList = array();
  105. list($mimetype, $err) = GetFileMimeType($file);
  106. if ($err) {
  107. fclose($f);
  108. return array("", $err);
  109. }
  110. $client = new UCloud_AuthHttpClient(null);
  111. for (; ;) {
  112. $host = $bucket . $UCLOUD_PROXY_SUFFIX;
  113. $path = $key;
  114. if (@fseek($f, $blkSize * $partNumber, SEEK_SET) < 0) {
  115. fclose($f);
  116. return array(null, new UCloud_Error(0, -1, "fseek error"));
  117. }
  118. $content = @fread($f, $blkSize);
  119. if ($content == FALSE) {
  120. if (feof($f)) break;
  121. fclose($f);
  122. return array(null, new UCloud_Error(0, -1, "read file error"));
  123. }
  124. $querys = array(
  125. "uploadId" => $uploadId,
  126. "partNumber" => $partNumber
  127. );
  128. $req = new HTTP_Request('PUT', array('host' => $host, 'path' => $path, 'query' => $querys), $content, $bucket, $key);
  129. $req->Header['Content-Type'] = $mimetype;
  130. $req->Header['Expect'] = '';
  131. list($data, $err) = UCloud_Client_Call($client, $req);
  132. if ($err) {
  133. fclose($f);
  134. return array(null, $err);
  135. }
  136. $etag = @$data['ETag'];
  137. $part = @$data['PartNumber'];
  138. if ($part != $partNumber) {
  139. fclose($f);
  140. return array(null, new UCloud_Error(0, -1, "unmatch partnumber"));
  141. }
  142. $etagList[] = $etag;
  143. $partNumber += 1;
  144. }
  145. fclose($f);
  146. return array($etagList, null);
  147. }
  148. public static function UCloud_MFinish($bucket, $key, $uploadId, $etagList, $newKey = '')
  149. {
  150. $err = CheckConfig(ActionType::MFINISH);
  151. if ($err != null) {
  152. return array(null, $err);
  153. }
  154. $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
  155. $host = $bucket . $UCLOUD_PROXY_SUFFIX;
  156. $path = $key;
  157. $querys = array(
  158. 'uploadId' => $uploadId,
  159. 'newKey' => $newKey,
  160. );
  161. $body = @implode(',', $etagList);
  162. $req = new HTTP_Request('POST', array('host' => $host, 'path' => $path, 'query' => $querys), $body, $bucket, $key);
  163. $req->Header['Content-Type'] = 'text/plain';
  164. $client = new UCloud_AuthHttpClient(null);
  165. return UCloud_Client_Call($client, $req);
  166. }
  167. public static function UCloud_MCancel($bucket, $key, $uploadId)
  168. {
  169. $err = CheckConfig(ActionType::MCANCEL);
  170. if ($err != null) {
  171. return array(null, $err);
  172. }
  173. $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
  174. $host = $bucket . $UCLOUD_PROXY_SUFFIX;
  175. $path = $key;
  176. $querys = array(
  177. 'uploadId' => $uploadId
  178. );
  179. $req = new HTTP_Request('DELETE', array('host' => $host, 'path' => $path, 'query' => $querys), null, $bucket, $key);
  180. $req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
  181. $client = new UCloud_AuthHttpClient(null);
  182. return UCloud_Client_Call($client, $req);
  183. }
  184. //------------------------------秒传------------------------------
  185. public static function UCloud_UploadHit($bucket, $key, $file)
  186. {
  187. $err = CheckConfig(ActionType::UPLOADHIT);
  188. if ($err != null) {
  189. return array(null, $err);
  190. }
  191. $f = @fopen($file, "r");
  192. if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
  193. $content = "";
  194. $fileSize = filesize($file);
  195. if ($fileSize != 0) {
  196. $content = @fread($f, $fileSize);
  197. if ($content == FALSE) {
  198. fclose($f);
  199. return array(null, new UCloud_Error(0, -1, "read file error"));
  200. }
  201. }
  202. list($fileHash, $err) = UCloud_FileHash($file);
  203. if ($err) {
  204. fclose($f);
  205. return array(null, $err);
  206. }
  207. fclose($f);
  208. $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
  209. $host = $bucket . $UCLOUD_PROXY_SUFFIX;
  210. $path = "uploadhit";
  211. $querys = array(
  212. 'Hash' => $fileHash,
  213. 'FileName' => $key,
  214. 'FileSize' => $fileSize
  215. );
  216. $req = new HTTP_Request('POST', array('host' => $host, 'path' => $path, 'query' => $querys), null, $bucket, $key);
  217. $req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
  218. $client = new UCloud_AuthHttpClient(null);
  219. return UCloud_Client_Call($client, $req);
  220. }
  221. //------------------------------删除文件------------------------------
  222. public static function UCloud_Delete($bucket, $key)
  223. {
  224. require_once("utils.php");
  225. $err = CheckConfig(\ActionType::DELETE);
  226. if ($err != null) {
  227. return array(null, $err);
  228. }
  229. $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
  230. $host = $bucket . $UCLOUD_PROXY_SUFFIX;
  231. $path = "$key";
  232. require("http.php");
  233. $req = new \HTTP_Request('DELETE', array('host' => $host, 'path' => $path), null, $bucket, $key);
  234. $req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
  235. $client = new \UCloud_AuthHttpClient(null);
  236. return UCloud_Client_Call($client, $req);
  237. }
  238. //------------------------------Head文件------------------------------
  239. public static function UCloud_Head($bucket, $key)
  240. {
  241. require_once("utils.php");
  242. $err = CheckConfig(11);
  243. if ($err != null) {
  244. return array(null, $err);
  245. }
  246. $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
  247. $host = $bucket . $UCLOUD_PROXY_SUFFIX;
  248. $path = "$key";
  249. require_once("http.php");
  250. $req = new \HTTP_Request('HEAD', array('host' => $host, 'path' => $path), null, $bucket, $key);
  251. $req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
  252. $client = new \UCloud_AuthHttpClient(null);
  253. return UCloud_Client_Call_ReHeader($client, $req);
  254. }
  255. //------------------------------追加上传------------------------------
  256. public static function UCloud_AppendFile($bucket, $key, $file, $position)
  257. {
  258. $action_type = ActionType::APPENDFILE;
  259. $err = CheckConfig(ActionType::APPENDFILE);
  260. if ($err != null) {
  261. return array(null, $err);
  262. }
  263. $f = @fopen($file, "r");
  264. if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
  265. $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
  266. $host = $bucket . $UCLOUD_PROXY_SUFFIX;
  267. $key = $key . "?append&position=" . $position;
  268. $path = $key;
  269. $content = @fread($f, filesize($file));
  270. list($mimetype, $err) = GetFileMimeType($file);
  271. if ($err) {
  272. fclose($f);
  273. return array("", $err);
  274. }
  275. $req = new HTTP_Request('PUT', array('host' => $host, 'path' => $path), $content, $bucket, $key, $action_type);
  276. $req->Header['Expect'] = '';
  277. $req->Header['Content-Type'] = $mimetype;
  278. $client = new UCloud_AuthHttpClient(null, $mimetype);
  279. list($data, $err) = UCloud_Client_Call($client, $req);
  280. fclose($f);
  281. return array($data, $err);
  282. }
  283. //------------------------------列表目录------------------------------
  284. public static function UCloud_ListObjects($bucket, $path_prefix, $marker, $count, $delimiter)
  285. {
  286. $action_type = ActionType::LISTOBJECTS;
  287. $err = CheckConfig(ActionType::LISTOBJECTS);
  288. if ($err != null) {
  289. return array(null, $err);
  290. }
  291. $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
  292. $host = $bucket . $UCLOUD_PROXY_SUFFIX;
  293. $query = "listobjects&prefix=" . $path_prefix . "&marker=" . $marker . "&max-keys=" . $count . "&delimiter=" . $delimiter;
  294. parse_str($query, $arr);
  295. $path = "?" . http_build_query($arr);
  296. $req = new HTTP_Request('GET', array('host' => $host, 'path' => $path), null, $bucket, null, $action_type);
  297. $req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
  298. $client = new UCloud_AuthHttpClient(null);
  299. list($data, $err) = UCloud_Client_Call($client, $req);
  300. return array($data, $err);
  301. }
  302. //------------------------------生成公有文件Url------------------------------
  303. // @results: $url
  304. public static function UCloud_MakePublicUrl($bucket, $key)
  305. {
  306. $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
  307. return $bucket . $UCLOUD_PROXY_SUFFIX . "/" . rawurlencode($key);
  308. }
  309. //------------------------------生成私有文件Url------------------------------
  310. // @results: $url
  311. public static function UCloud_MakePrivateUrl($bucket, $key, $expires = 0)
  312. {
  313. $err = CheckConfig(ActionType::GETFILE);
  314. if ($err != null) {
  315. return array(null, $err);
  316. }
  317. global $UCLOUD_PUBLIC_KEY;
  318. $public_url = UCloud_MakePublicUrl($bucket, $key);
  319. $req = new HTTP_Request('GET', array('path' => $public_url), null, $bucket, $key);
  320. if ($expires > 0) {
  321. $req->Header['Expires'] = $expires;
  322. }
  323. $client = new UCloud_AuthHttpClient(null);
  324. $temp = $client->Auth->SignRequest($req, null, QUERY_STRING_CHECK);
  325. $signature = substr($temp, -28, 28);
  326. $url = $public_url . "?AWSAccessKeyId=" . rawurlencode($UCLOUD_PUBLIC_KEY) . "&Signature=" . rawurlencode($signature);
  327. if ('' != $expires) {
  328. $url .= "&Expires=" . rawurlencode($expires);
  329. }
  330. return $url;
  331. }
  332. }