123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- namespace Upload\Lib\View;
- use Dever;
- Dever::apply('sdk/sina', 'upload');
- use sinacloud\sae\Storage as Storage;
- class Sina
- {
- private $client;
- private $token;
- private $auth;
- private $bucket;
- # 连接
- public function connect($config, $upload, $file = null)
- {
- $this->auth = new Storage("$AppName:" . $config['appkey'], $config['appsecret']);
- //$this->auth->setExceptions(true);
- $this->bucket = $upload['bucket'];
- return $this;
- }
- # 删除文件
- public function delete($file, $bucket = false)
- {
- $bucket = $bucket ? $bucket : $this->bucket;
- $result = $this->auth->deleteObject($bucket, $file);
- return $result;
- }
- # 下载文件
- public function download($bucket, $file, $local = false)
- {
- $temp_file = tempnam(sys_get_temp_dir(), 'Tux');
- $ret = $this->auth->getObject($bucket, $file, $temp_file);
- $content = file_get_contents($temp_file);
- return $content;
- }
- # 上传文件
- public function upload($file, $source_file, $options = array(), $base64 = false)
- {
- if ($base64) {
- $ret = $this->auth->putObject($source_file, $this->bucket, $file, array(), array('Content-Type' => 'text/plain'));
- } elseif (strstr($source_file, 'http')) {
- $source_file = file_get_contents($source_file);
- $ret = $this->auth->putObject($source_file, $this->bucket, $file, array(), array('Content-Type' => 'text/plain'));
- } else {
- $ret = $this->auth->putObjectFile($source_file, $this->bucket, $file);
- }
- if ($ret) {
- return $this->auth->getCdnUrl($this->bucket, $file);
- }
- return $ret;
- }
- /*
- # 连接
- public function connect($config, $upload, $file = null)
- {
- $this->auth = new \SCS($config['appkey'], $config['appsecret']);
- $this->auth->setExceptions(true);
- $this->bucket = $upload['bucket'];
- return $this;
- }
- # 删除文件
- public function delete($file, $bucket = false)
- {
- $bucket = $bucket ? $bucket : $this->bucket;
- $result = $this->auth->deleteObject($bucket, $file);
- return $result;
- }
- # 下载文件
- public function download($bucket, $file, $local = false)
- {
- $temp_file = tempnam(sys_get_temp_dir(), 'Tux');
- $ret = $this->auth->getObject($bucket, $file, $temp_file);
- $content = file_get_contents($temp_file);
- return $content;
- }
- # 上传文件
- public function upload($file, $source_file, $options = array(), $base64 = false)
- {
- try {
- if ($base64) {
- $ret = $this->auth->putObject($source_file, $this->bucket, $file, \SCS::ACL_PUBLIC_READ, array(), array('Content-Type' => 'text/plain'));
- } elseif (strstr($source_file, 'http')) {
- $source_file = file_get_contents($source_file);
- $ret = $this->auth->putObject($source_file, $this->bucket, $file, \SCS::ACL_PUBLIC_READ, array(), array('Content-Type' => 'text/plain'));
- } else {
- //$ret = $this->auth->putObject($this->auth->inputFile($source_file, false), $this->bucket, $file, \SCS::ACL_PUBLIC_READ);
- //初始化上传
- $info = $this->auth->initiateMultipartUpload($this->bucket, $file, \SCS::ACL_PUBLIC_READ);
- $uploadId = $info['upload_id'];
- $fp = fopen($source_file, 'rb');
- $i = 1;
- $part_info = array();
- while (!feof($fp)) {
- $res = $this->auth->putObject($this->auth->inputResourceMultipart($fp, 1024 * 512, $uploadId, $i), $this->bucket, $file);
- if (isset($res['hash'])) {
- $part_info[] = array(
- 'PartNumber' => $i,
- 'ETag' => $res['hash'],
- );
- }
- $i++;
- }
- //列分片
- $parts = $this->auth->listParts($this->bucket, $file, $uploadId);
- if (count($parts) > 0 && count($parts) == count($part_info)) {
- foreach ($parts as $part_number => $part) {
- if ($part['etag'] != $part_info[$part_number - 1]['ETag']) {
- Dever::alert('上传失败');
- break;
- }
- }
- //合并分片
- $this->auth->completeMultipartUpload($this->bucket, $file, $uploadId, $part_info);
- fclose($fp);
- }
- $ret = true;
- }
- } catch (\SCSException $e) {
- Dever::alert('上传失败:' . $e->getMessage());
- }
- return $ret;
- }
- */
- }
|