123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- <?php
- namespace app\common\library\ucloud;
- class Proxy
- {
- function __construct()
- {
- require("./conf.php");
- require("./http.php");
- require("./utils.php");
- require("./digest.php");
- }
- //------------------------------普通上传------------------------------
- public static function UCloud_PutFile($bucket, $key, $file)
- {
- $action_type = ActionType::PUTFILE;
- $err = CheckConfig(ActionType::PUTFILE);
- if ($err != null) {
- return array(null, $err);
- }
- $f = @fopen($file, "r");
- if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
- $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
- $host = $bucket . $UCLOUD_PROXY_SUFFIX;
- $path = $key;
- $content = @fread($f, filesize($file));
- list($mimetype, $err) = GetFileMimeType($file);
- if ($err) {
- fclose($f);
- return array("", $err);
- }
- $req = new HTTP_Request('PUT', array('host' => $host, 'path' => $path), $content, $bucket, $key, $action_type);
- $req->Header['Expect'] = '';
- $req->Header['Content-Type'] = $mimetype;
- $client = new UCloud_AuthHttpClient(null, $mimetype);
- list($data, $err) = UCloud_Client_Call($client, $req);
- fclose($f);
- return array($data, $err);
- }
- //------------------------------表单上传------------------------------
- public static function UCloud_MultipartForm($bucket, $key, $file)
- {
- $action_type = ActionType::POSTFILE;
- $err = CheckConfig(ActionType::POSTFILE);
- if ($err != null) {
- return array(null, $err);
- }
- $f = @fopen($file, "r");
- if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
- $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
- $host = $bucket . $UCLOUD_PROXY_SUFFIX;
- $path = "";
- $fsize = filesize($file);
- $content = "";
- if ($fsize != 0) {
- $content = @fread($f, filesize($file));
- if ($content == FALSE) {
- fclose($f);
- return array(null, new UCloud_Error(0, -1, "read file error"));
- }
- }
- list($mimetype, $err) = GetFileMimeType($file);
- if ($err) {
- fclose($f);
- return array("", $err);
- }
- $req = new HTTP_Request('POST', array('host' => $host, 'path' => $path), $content, $bucket, $key, $action_type);
- $req->Header['Expect'] = '';
- $token = UCloud_SignRequest(null, $req, $mimetype);
- $fields = array('Authorization' => $token, 'FileName' => $key);
- $files = array('files' => array('file', $file, $content, $mimetype));
- $client = new UCloud_AuthHttpClient(null, NO_AUTH_CHECK);
- list($data, $err) = UCloud_Client_CallWithMultipartForm($client, $req, $fields, $files);
- fclose($f);
- return array($data, $err);
- }
- //------------------------------分片上传------------------------------
- public static function UCloud_MInit($bucket, $key)
- {
- $err = CheckConfig(ActionType::MINIT);
- if ($err != null) {
- return array(null, $err);
- }
- $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
- $host = $bucket . $UCLOUD_PROXY_SUFFIX;
- $path = $key;
- $querys = array(
- "uploads" => ""
- );
- $req = new HTTP_Request('POST', array('host' => $host, 'path' => $path, 'query' => $querys), null, $bucket, $key);
- $req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
- $client = new UCloud_AuthHttpClient(null);
- return UCloud_Client_Call($client, $req);
- }
- //@results: (tagList, err)
- public static function UCloud_MUpload($bucket, $key, $file, $uploadId, $blkSize, $partNumber = 0)
- {
- $err = CheckConfig(ActionType::MUPLOAD);
- if ($err != null) {
- return array(null, $err);
- }
- $f = @fopen($file, "r");
- if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
- $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
- $etagList = array();
- list($mimetype, $err) = GetFileMimeType($file);
- if ($err) {
- fclose($f);
- return array("", $err);
- }
- $client = new UCloud_AuthHttpClient(null);
- for (; ;) {
- $host = $bucket . $UCLOUD_PROXY_SUFFIX;
- $path = $key;
- if (@fseek($f, $blkSize * $partNumber, SEEK_SET) < 0) {
- fclose($f);
- return array(null, new UCloud_Error(0, -1, "fseek error"));
- }
- $content = @fread($f, $blkSize);
- if ($content == FALSE) {
- if (feof($f)) break;
- fclose($f);
- return array(null, new UCloud_Error(0, -1, "read file error"));
- }
- $querys = array(
- "uploadId" => $uploadId,
- "partNumber" => $partNumber
- );
- $req = new HTTP_Request('PUT', array('host' => $host, 'path' => $path, 'query' => $querys), $content, $bucket, $key);
- $req->Header['Content-Type'] = $mimetype;
- $req->Header['Expect'] = '';
- list($data, $err) = UCloud_Client_Call($client, $req);
- if ($err) {
- fclose($f);
- return array(null, $err);
- }
- $etag = @$data['ETag'];
- $part = @$data['PartNumber'];
- if ($part != $partNumber) {
- fclose($f);
- return array(null, new UCloud_Error(0, -1, "unmatch partnumber"));
- }
- $etagList[] = $etag;
- $partNumber += 1;
- }
- fclose($f);
- return array($etagList, null);
- }
- public static function UCloud_MFinish($bucket, $key, $uploadId, $etagList, $newKey = '')
- {
- $err = CheckConfig(ActionType::MFINISH);
- if ($err != null) {
- return array(null, $err);
- }
- $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
- $host = $bucket . $UCLOUD_PROXY_SUFFIX;
- $path = $key;
- $querys = array(
- 'uploadId' => $uploadId,
- 'newKey' => $newKey,
- );
- $body = @implode(',', $etagList);
- $req = new HTTP_Request('POST', array('host' => $host, 'path' => $path, 'query' => $querys), $body, $bucket, $key);
- $req->Header['Content-Type'] = 'text/plain';
- $client = new UCloud_AuthHttpClient(null);
- return UCloud_Client_Call($client, $req);
- }
- public static function UCloud_MCancel($bucket, $key, $uploadId)
- {
- $err = CheckConfig(ActionType::MCANCEL);
- if ($err != null) {
- return array(null, $err);
- }
- $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
- $host = $bucket . $UCLOUD_PROXY_SUFFIX;
- $path = $key;
- $querys = array(
- 'uploadId' => $uploadId
- );
- $req = new HTTP_Request('DELETE', array('host' => $host, 'path' => $path, 'query' => $querys), null, $bucket, $key);
- $req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
- $client = new UCloud_AuthHttpClient(null);
- return UCloud_Client_Call($client, $req);
- }
- //------------------------------秒传------------------------------
- public static function UCloud_UploadHit($bucket, $key, $file)
- {
- $err = CheckConfig(ActionType::UPLOADHIT);
- if ($err != null) {
- return array(null, $err);
- }
- $f = @fopen($file, "r");
- if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
- $content = "";
- $fileSize = filesize($file);
- if ($fileSize != 0) {
- $content = @fread($f, $fileSize);
- if ($content == FALSE) {
- fclose($f);
- return array(null, new UCloud_Error(0, -1, "read file error"));
- }
- }
- list($fileHash, $err) = UCloud_FileHash($file);
- if ($err) {
- fclose($f);
- return array(null, $err);
- }
- fclose($f);
- $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
- $host = $bucket . $UCLOUD_PROXY_SUFFIX;
- $path = "uploadhit";
- $querys = array(
- 'Hash' => $fileHash,
- 'FileName' => $key,
- 'FileSize' => $fileSize
- );
- $req = new HTTP_Request('POST', array('host' => $host, 'path' => $path, 'query' => $querys), null, $bucket, $key);
- $req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
- $client = new UCloud_AuthHttpClient(null);
- return UCloud_Client_Call($client, $req);
- }
- //------------------------------删除文件------------------------------
- public static function UCloud_Delete($bucket, $key)
- {
- require_once("utils.php");
- $err = CheckConfig(\ActionType::DELETE);
- if ($err != null) {
- return array(null, $err);
- }
- $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
- $host = $bucket . $UCLOUD_PROXY_SUFFIX;
- $path = "$key";
- require("http.php");
- $req = new \HTTP_Request('DELETE', array('host' => $host, 'path' => $path), null, $bucket, $key);
- $req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
- $client = new \UCloud_AuthHttpClient(null);
- return UCloud_Client_Call($client, $req);
- }
- //------------------------------Head文件------------------------------
- public static function UCloud_Head($bucket, $key)
- {
- require_once("utils.php");
- $err = CheckConfig(11);
- if ($err != null) {
- return array(null, $err);
- }
- $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
- $host = $bucket . $UCLOUD_PROXY_SUFFIX;
- $path = "$key";
- require_once("http.php");
- $req = new \HTTP_Request('HEAD', array('host' => $host, 'path' => $path), null, $bucket, $key);
- $req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
- $client = new \UCloud_AuthHttpClient(null);
- return UCloud_Client_Call_ReHeader($client, $req);
- }
- //------------------------------追加上传------------------------------
- public static function UCloud_AppendFile($bucket, $key, $file, $position)
- {
- $action_type = ActionType::APPENDFILE;
- $err = CheckConfig(ActionType::APPENDFILE);
- if ($err != null) {
- return array(null, $err);
- }
- $f = @fopen($file, "r");
- if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
- $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
- $host = $bucket . $UCLOUD_PROXY_SUFFIX;
- $key = $key . "?append&position=" . $position;
- $path = $key;
- $content = @fread($f, filesize($file));
- list($mimetype, $err) = GetFileMimeType($file);
- if ($err) {
- fclose($f);
- return array("", $err);
- }
- $req = new HTTP_Request('PUT', array('host' => $host, 'path' => $path), $content, $bucket, $key, $action_type);
- $req->Header['Expect'] = '';
- $req->Header['Content-Type'] = $mimetype;
- $client = new UCloud_AuthHttpClient(null, $mimetype);
- list($data, $err) = UCloud_Client_Call($client, $req);
- fclose($f);
- return array($data, $err);
- }
- //------------------------------列表目录------------------------------
- public static function UCloud_ListObjects($bucket, $path_prefix, $marker, $count, $delimiter)
- {
- $action_type = ActionType::LISTOBJECTS;
- $err = CheckConfig(ActionType::LISTOBJECTS);
- if ($err != null) {
- return array(null, $err);
- }
- $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
- $host = $bucket . $UCLOUD_PROXY_SUFFIX;
- $query = "listobjects&prefix=" . $path_prefix . "&marker=" . $marker . "&max-keys=" . $count . "&delimiter=" . $delimiter;
- parse_str($query, $arr);
- $path = "?" . http_build_query($arr);
- $req = new HTTP_Request('GET', array('host' => $host, 'path' => $path), null, $bucket, null, $action_type);
- $req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
- $client = new UCloud_AuthHttpClient(null);
- list($data, $err) = UCloud_Client_Call($client, $req);
- return array($data, $err);
- }
- //------------------------------生成公有文件Url------------------------------
- // @results: $url
- public static function UCloud_MakePublicUrl($bucket, $key)
- {
- $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
- return $bucket . $UCLOUD_PROXY_SUFFIX . "/" . rawurlencode($key);
- }
- //------------------------------生成私有文件Url------------------------------
- // @results: $url
- public static function UCloud_MakePrivateUrl($bucket, $key, $expires = 0)
- {
- $err = CheckConfig(ActionType::GETFILE);
- if ($err != null) {
- return array(null, $err);
- }
- global $UCLOUD_PUBLIC_KEY;
- $public_url = UCloud_MakePublicUrl($bucket, $key);
- $req = new HTTP_Request('GET', array('path' => $public_url), null, $bucket, $key);
- if ($expires > 0) {
- $req->Header['Expires'] = $expires;
- }
- $client = new UCloud_AuthHttpClient(null);
- $temp = $client->Auth->SignRequest($req, null, QUERY_STRING_CHECK);
- $signature = substr($temp, -28, 28);
- $url = $public_url . "?AWSAccessKeyId=" . rawurlencode($UCLOUD_PUBLIC_KEY) . "&Signature=" . rawurlencode($signature);
- if ('' != $expires) {
- $url .= "&Expires=" . rawurlencode($expires);
- }
- return $url;
- }
- }
|