|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once("http.php"); |
| 4 | +require_once("auth_digest.php"); |
| 5 | + |
| 6 | +// ---------------------------------------------------------- |
| 7 | +// class Qiniu_Rio_PutExtra |
| 8 | + |
| 9 | +class Qiniu_Rio_PutExtra |
| 10 | +{ |
| 11 | + public $Bucket = null; // 必选(未来会没有这个字段)。 |
| 12 | + public $Params = null; |
| 13 | + public $MimeType = null; |
| 14 | + public $ChunkSize = 0; // 可选。每次上传的Chunk大小 |
| 15 | + public $TryTimes = 0; // 可选。尝试次数 |
| 16 | + public $Progresses = null; // 可选。上传进度:[]BlkputRet |
| 17 | + public $Notify = null; // 进度通知:func(blkIdx int, blkSize int, ret *BlkputRet) |
| 18 | + public $NotifyErr = null; // 错误通知:func(blkIdx int, blkSize int, err error) |
| 19 | + |
| 20 | + public function __construct($bucket = null) { |
| 21 | + $this->Bucket = $bucket; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// ---------------------------------------------------------- |
| 26 | +// func Qiniu_Rio_BlockCount |
| 27 | + |
| 28 | +define('QINIU_RIO_BLOCK_BITS', 22); |
| 29 | +define('QINIU_RIO_BLOCK_SIZE', 1 << QINIU_RIO_BLOCK_BITS); // 4M |
| 30 | + |
| 31 | +function Qiniu_Rio_BlockCount($fsize) // => $blockCnt |
| 32 | +{ |
| 33 | + return ($fsize + (QINIU_RIO_BLOCK_SIZE - 1)) >> QINIU_RIO_BLOCK_BITS; |
| 34 | +} |
| 35 | + |
| 36 | +// ---------------------------------------------------------- |
| 37 | +// internal func Qiniu_Rio_Mkblock/Mkfile |
| 38 | + |
| 39 | +function Qiniu_Rio_Mkblock($self, $host, $reader, $size) // => ($blkputRet, $err) |
| 40 | +{ |
| 41 | + if (is_resource($reader)) { |
| 42 | + $body = fread($reader, $size); |
| 43 | + if ($body === false) { |
| 44 | + $err = Qiniu_NewError(0, 'fread failed'); |
| 45 | + return array(null, $err); |
| 46 | + } |
| 47 | + } else { |
| 48 | + list($body, $err) = $reader->Read($size); |
| 49 | + if ($err !== null) { |
| 50 | + return array(null, $err); |
| 51 | + } |
| 52 | + } |
| 53 | + if (strlen($body) != $size) { |
| 54 | + $err = Qiniu_NewError(0, 'fread failed: unexpected eof'); |
| 55 | + return array(null, $err); |
| 56 | + } |
| 57 | + |
| 58 | + $url = $host . '/mkblk/' . $size; |
| 59 | + return Qiniu_Client_CallWithForm($self, $url, $body, 'application/octet-stream'); |
| 60 | +} |
| 61 | + |
| 62 | +function Qiniu_Rio_Mkfile($self, $host, $key, $fsize, $extra) // => ($putRet, $err) |
| 63 | +{ |
| 64 | + $entry = $extra->Bucket . ':' . $key; |
| 65 | + $url = $host . '/rs-mkfile/' . Qiniu_Encode($entry) . '/fsize/' . $fsize; |
| 66 | + |
| 67 | + if (!empty($extra->MimeType)) { |
| 68 | + $url .= '/mimeType/' . Qiniu_Encode($extra->MimeType); |
| 69 | + } |
| 70 | + |
| 71 | + $ctxs = array(); |
| 72 | + foreach ($extra->Progresses as $prog) { |
| 73 | + $ctxs []= $prog['ctx']; |
| 74 | + } |
| 75 | + $body = implode(',', $ctxs); |
| 76 | + |
| 77 | + return Qiniu_Client_CallWithForm($self, $url, $body, 'text/plain'); |
| 78 | +} |
| 79 | + |
| 80 | +// ---------------------------------------------------------- |
| 81 | +// class Qiniu_Rio_UploadClient |
| 82 | + |
| 83 | +class Qiniu_Rio_UploadClient |
| 84 | +{ |
| 85 | + public $uptoken; |
| 86 | + |
| 87 | + public function __construct($uptoken) |
| 88 | + { |
| 89 | + $this->uptoken = $uptoken; |
| 90 | + } |
| 91 | + |
| 92 | + public function RoundTrip($req) // => ($resp, $error) |
| 93 | + { |
| 94 | + $token = $this->uptoken; |
| 95 | + $req->Header['Authorization'] = "UpToken $token"; |
| 96 | + return Qiniu_Client_do($req); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// ---------------------------------------------------------- |
| 101 | +// class Qiniu_Rio_Put/PutFile |
| 102 | + |
| 103 | +function Qiniu_Rio_Put($upToken, $key, $body, $fsize, $putExtra) // => ($putRet, $err) |
| 104 | +{ |
| 105 | + global $QINIU_UP_HOST; |
| 106 | + |
| 107 | + $self = new Qiniu_Rio_UploadClient($upToken); |
| 108 | + |
| 109 | + $progresses = array(); |
| 110 | + $host = $QINIU_UP_HOST; |
| 111 | + $uploaded = 0; |
| 112 | + while ($uploaded < $fsize) { |
| 113 | + if ($fsize < $uploaded + QINIU_RIO_BLOCK_SIZE) { |
| 114 | + $bsize = $fsize - $uploaded; |
| 115 | + } else { |
| 116 | + $bsize = QINIU_RIO_BLOCK_SIZE; |
| 117 | + } |
| 118 | + list($blkputRet, $err) = Qiniu_Rio_Mkblock($self, $host, $body, $bsize); |
| 119 | + $host = $blkputRet['host']; |
| 120 | + $uploaded += $bsize; |
| 121 | + $progresses []= $blkputRet; |
| 122 | + } |
| 123 | + |
| 124 | + $putExtra->Progresses = $progresses; |
| 125 | + return Qiniu_Rio_Mkfile($self, $host, $key, $fsize, $putExtra); |
| 126 | +} |
| 127 | + |
| 128 | +function Qiniu_Rio_PutFile($upToken, $key, $localFile, $putExtra) // => ($putRet, $err) |
| 129 | +{ |
| 130 | + $fp = fopen($localFile, 'rb'); |
| 131 | + if ($fp === false) { |
| 132 | + $err = Qiniu_NewError(0, 'fopen failed'); |
| 133 | + return array(null, $err); |
| 134 | + } |
| 135 | + |
| 136 | + $fi = fstat($fp); |
| 137 | + $result = Qiniu_Rio_Put($upToken, $key, $fp, $fi['size'], $putExtra); |
| 138 | + fclose($fp); |
| 139 | + return $result; |
| 140 | +} |
| 141 | + |
| 142 | +// ---------------------------------------------------------- |
| 143 | + |
0 commit comments