123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace Qiniu\Storage;
- use Qiniu\Config;
- use Qiniu\Http\HttpClient;
- use Qiniu\Storage\ResumeUploader;
- use Qiniu\Storage\FormUploader;
- /**
- * 主要涉及了资源上传接口的实现
- *
- * @link http://developer.qiniu.com/docs/v6/api/reference/up/
- */
- final class UploadManager
- {
- private $config;
- public function __construct(Config $config = null)
- {
- if ($config === null) {
- $config = new Config();
- }
- $this->config = $config;
- }
- /**
- * 上传二进制流到七牛
- *
- * @param $upToken 上传凭证
- * @param $key 上传文件名
- * @param $data 上传二进制流
- * @param $params 自定义变量,规格参考
- * http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
- * @param $mime 上传数据的mimeType
- * @param $checkCrc 是否校验crc32
- *
- * @return array 包含已上传文件的信息,类似:
- * [
- * "hash" => "<Hash string>",
- * "key" => "<Key string>"
- * ]
- */
- public function put(
- $upToken,
- $key,
- $data,
- $params = null,
- $mime = 'application/octet-stream',
- $fname = null
- ) {
-
- $params = self::trimParams($params);
- return FormUploader::put(
- $upToken,
- $key,
- $data,
- $this->config,
- $params,
- $mime,
- $fname
- );
- }
- /**
- * 上传文件到七牛
- *
- * @param $upToken 上传凭证
- * @param $key 上传文件名
- * @param $filePath 上传文件的路径
- * @param $params 自定义变量,规格参考
- * http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
- * @param $mime 上传数据的mimeType
- * @param $checkCrc 是否校验crc32
- *
- * @return array 包含已上传文件的信息,类似:
- * [
- * "hash" => "<Hash string>",
- * "key" => "<Key string>"
- * ]
- */
- public function putFile(
- $upToken,
- $key,
- $filePath,
- $params = null,
- $mime = 'application/octet-stream',
- $checkCrc = false
- ) {
-
- $file = fopen($filePath, 'rb');
- if ($file === false) {
- throw new \Exception("file can not open", 1);
- }
- $params = self::trimParams($params);
- $stat = fstat($file);
- $size = $stat['size'];
- if ($size <= Config::BLOCK_SIZE) {
- $data = fread($file, $size);
- fclose($file);
- if ($data === false) {
- throw new \Exception("file can not read", 1);
- }
- return FormUploader::put(
- $upToken,
- $key,
- $data,
- $this->config,
- $params,
- $mime,
- $checkCrc,
- basename($filePath)
- );
- }
- $up = new ResumeUploader(
- $upToken,
- $key,
- $file,
- $size,
- $params,
- $mime,
- $this->config
- );
- $ret = $up->upload(basename($filePath));
- fclose($file);
- return $ret;
- }
- public static function trimParams($params)
- {
- if ($params === null) {
- return null;
- }
- $ret = array();
- foreach ($params as $k => $v) {
- $pos = strpos($k, 'x:');
- if ($pos === 0 && !empty($v)) {
- $ret[$k] = $v;
- }
- }
- return $ret;
- }
- }
|