Sina.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Upload\Lib\View;
  3. use Dever;
  4. Dever::apply('sdk/sina', 'upload');
  5. use sinacloud\sae\Storage as Storage;
  6. class Sina
  7. {
  8. private $client;
  9. private $token;
  10. private $auth;
  11. private $bucket;
  12. # 连接
  13. public function connect($config, $upload, $file = null)
  14. {
  15. $this->auth = new Storage("$AppName:" . $config['appkey'], $config['appsecret']);
  16. //$this->auth->setExceptions(true);
  17. $this->bucket = $upload['bucket'];
  18. return $this;
  19. }
  20. # 删除文件
  21. public function delete($file, $bucket = false)
  22. {
  23. $bucket = $bucket ? $bucket : $this->bucket;
  24. $result = $this->auth->deleteObject($bucket, $file);
  25. return $result;
  26. }
  27. # 下载文件
  28. public function download($bucket, $file, $local = false)
  29. {
  30. $temp_file = tempnam(sys_get_temp_dir(), 'Tux');
  31. $ret = $this->auth->getObject($bucket, $file, $temp_file);
  32. $content = file_get_contents($temp_file);
  33. return $content;
  34. }
  35. # 上传文件
  36. public function upload($file, $source_file, $options = array(), $base64 = false)
  37. {
  38. if ($base64) {
  39. $ret = $this->auth->putObject($source_file, $this->bucket, $file, array(), array('Content-Type' => 'text/plain'));
  40. } elseif (strstr($source_file, 'http')) {
  41. $source_file = file_get_contents($source_file);
  42. $ret = $this->auth->putObject($source_file, $this->bucket, $file, array(), array('Content-Type' => 'text/plain'));
  43. } else {
  44. $ret = $this->auth->putObjectFile($source_file, $this->bucket, $file);
  45. }
  46. if ($ret) {
  47. return $this->auth->getCdnUrl($this->bucket, $file);
  48. }
  49. return $ret;
  50. }
  51. /*
  52. # 连接
  53. public function connect($config, $upload, $file = null)
  54. {
  55. $this->auth = new \SCS($config['appkey'], $config['appsecret']);
  56. $this->auth->setExceptions(true);
  57. $this->bucket = $upload['bucket'];
  58. return $this;
  59. }
  60. # 删除文件
  61. public function delete($file, $bucket = false)
  62. {
  63. $bucket = $bucket ? $bucket : $this->bucket;
  64. $result = $this->auth->deleteObject($bucket, $file);
  65. return $result;
  66. }
  67. # 下载文件
  68. public function download($bucket, $file, $local = false)
  69. {
  70. $temp_file = tempnam(sys_get_temp_dir(), 'Tux');
  71. $ret = $this->auth->getObject($bucket, $file, $temp_file);
  72. $content = file_get_contents($temp_file);
  73. return $content;
  74. }
  75. # 上传文件
  76. public function upload($file, $source_file, $options = array(), $base64 = false)
  77. {
  78. try {
  79. if ($base64) {
  80. $ret = $this->auth->putObject($source_file, $this->bucket, $file, \SCS::ACL_PUBLIC_READ, array(), array('Content-Type' => 'text/plain'));
  81. } elseif (strstr($source_file, 'http')) {
  82. $source_file = file_get_contents($source_file);
  83. $ret = $this->auth->putObject($source_file, $this->bucket, $file, \SCS::ACL_PUBLIC_READ, array(), array('Content-Type' => 'text/plain'));
  84. } else {
  85. //$ret = $this->auth->putObject($this->auth->inputFile($source_file, false), $this->bucket, $file, \SCS::ACL_PUBLIC_READ);
  86. //初始化上传
  87. $info = $this->auth->initiateMultipartUpload($this->bucket, $file, \SCS::ACL_PUBLIC_READ);
  88. $uploadId = $info['upload_id'];
  89. $fp = fopen($source_file, 'rb');
  90. $i = 1;
  91. $part_info = array();
  92. while (!feof($fp)) {
  93. $res = $this->auth->putObject($this->auth->inputResourceMultipart($fp, 1024 * 512, $uploadId, $i), $this->bucket, $file);
  94. if (isset($res['hash'])) {
  95. $part_info[] = array(
  96. 'PartNumber' => $i,
  97. 'ETag' => $res['hash'],
  98. );
  99. }
  100. $i++;
  101. }
  102. //列分片
  103. $parts = $this->auth->listParts($this->bucket, $file, $uploadId);
  104. if (count($parts) > 0 && count($parts) == count($part_info)) {
  105. foreach ($parts as $part_number => $part) {
  106. if ($part['etag'] != $part_info[$part_number - 1]['ETag']) {
  107. Dever::alert('上传失败');
  108. break;
  109. }
  110. }
  111. //合并分片
  112. $this->auth->completeMultipartUpload($this->bucket, $file, $uploadId, $part_info);
  113. fclose($fp);
  114. }
  115. $ret = true;
  116. }
  117. } catch (\SCSException $e) {
  118. Dever::alert('上传失败:' . $e->getMessage());
  119. }
  120. return $ret;
  121. }
  122. */
  123. }