Sina.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Upload\Lib\View;
  3. use Dever;
  4. Dever::apply('sdk/sina', 'upload');
  5. class Sina
  6. {
  7. private $client;
  8. private $token;
  9. private $auth;
  10. private $bucket;
  11. # 连接
  12. public function connect($config, $upload, $file = null)
  13. {
  14. $this->auth = new \SCS($config['appkey'], $config['appsecret']);
  15. $this->auth->setExceptions(true);
  16. $this->bucket = $upload['bucket'];
  17. return $this;
  18. }
  19. # 删除文件
  20. public function delete($file, $bucket = false)
  21. {
  22. $bucket = $bucket ? $bucket : $this->bucket;
  23. $result = $this->auth->deleteObject($bucket, $file);
  24. return $result;
  25. }
  26. # 下载文件
  27. public function download($bucket, $file, $local = false)
  28. {
  29. $temp_file = tempnam(sys_get_temp_dir(), 'Tux');
  30. $ret = $this->auth->getObject($bucket, $file, $temp_file);
  31. $content = file_get_contents($temp_file);
  32. return $content;
  33. }
  34. # 上传文件
  35. public function upload($file, $source_file, $options = array(), $base64 = false)
  36. {
  37. try {
  38. if ($base64) {
  39. $ret = $this->auth->putObject($source_file, $this->bucket, $file, \SCS::ACL_PUBLIC_READ, 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, \SCS::ACL_PUBLIC_READ, array(), array('Content-Type' => 'text/plain'));
  43. } else {
  44. //$ret = $this->auth->putObject($this->auth->inputFile($source_file, false), $this->bucket, $file, \SCS::ACL_PUBLIC_READ);
  45. //初始化上传
  46. $info = $this->auth->initiateMultipartUpload($this->bucket, $file, \SCS::ACL_PUBLIC_READ);
  47. $uploadId = $info['upload_id'];
  48. $fp = fopen($source_file, 'rb');
  49. $i = 1;
  50. $part_info = array();
  51. while (!feof($fp)) {
  52. /*上传分片,一个分片512KB*/
  53. $res = $this->auth->putObject($this->auth->inputResourceMultipart($fp, 1024 * 512, $uploadId, $i), $this->bucket, $file);
  54. if (isset($res['hash'])) {
  55. $part_info[] = array(
  56. 'PartNumber' => $i,
  57. 'ETag' => $res['hash'],
  58. );
  59. }
  60. $i++;
  61. }
  62. //列分片
  63. $parts = $this->auth->listParts($this->bucket, $file, $uploadId);
  64. if (count($parts) > 0 && count($parts) == count($part_info)) {
  65. foreach ($parts as $part_number => $part) {
  66. if ($part['etag'] != $part_info[$part_number - 1]['ETag']) {
  67. Dever::alert('上传失败');
  68. break;
  69. }
  70. }
  71. //合并分片
  72. $this->auth->completeMultipartUpload($this->bucket, $file, $uploadId, $part_info);
  73. fclose($fp);
  74. }
  75. $ret = true;
  76. }
  77. } catch (\SCSException $e) {
  78. Dever::alert('上传失败:' . $e->getMessage());
  79. }
  80. return $ret;
  81. }
  82. }