AliyuncsAdapter.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 2016/12/11
  6. * Time: 下午11:44
  7. */
  8. namespace common\modules\attachment\components\flysystem;
  9. use League\Flysystem\Adapter\AbstractAdapter;
  10. use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait;
  11. use League\Flysystem\Config;
  12. use OSS\OssClient;
  13. class AliyuncsAdapter extends AbstractAdapter
  14. {
  15. use NotSupportingVisibilityTrait;
  16. protected $ossClient;
  17. /**
  18. * @var string
  19. */
  20. protected $bucket;
  21. /**
  22. * @var string
  23. */
  24. protected $token;
  25. /**
  26. * Constructor.
  27. */
  28. public function __construct($ak, $sk, $endpoint, $isCName, $bucket)
  29. {
  30. $this->ossClient = new OssClient($ak, $sk, $endpoint, $isCName);
  31. $this->bucket = $bucket;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function write($path, $contents, Config $config)
  37. {
  38. return $this->ossClient->putObject($this->bucket, $path, $contents);
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function writeStream($path, $resource, Config $config)
  44. {
  45. // @todo Sharding Upload
  46. return false;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function update($path, $contents, Config $config)
  52. {
  53. $this->delete($path);
  54. return $this->write($path, $contents, $config);
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function updateStream($path, $resource, Config $config)
  60. {
  61. $this->delete($path);
  62. return $this->writeStream($path, $resource, $config);
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function rename($path, $newpath)
  68. {
  69. return false;
  70. }
  71. public function copy($path, $newpath)
  72. {
  73. $r = $this->ossClient->copyObject($this->bucket, $path, $this->bucket, $newpath);
  74. return is_null($r);
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function delete($path)
  80. {
  81. $r = $this->ossClient->deleteObject($this->bucket, $path);
  82. return is_null($r);
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function deleteDir($dirname)
  88. {
  89. return true;
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function createDir($dirname, Config $config)
  95. {
  96. return ['path' => $dirname, 'type' => 'dir'];
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function has($path)
  102. {
  103. return $this->ossClient->doesObjectExist($this->bucket, $path);
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function read($path)
  109. {
  110. return false;
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function readStream($path)
  116. {
  117. // @todo readStream;
  118. return false;
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function listContents($directory = '', $recursive = false)
  124. {
  125. $list = [];
  126. $r = $this->ossClient->listObjects($this->bucket, $directory);
  127. foreach ($r[0] as $v) {
  128. $list[] = $this->normalizeFileInfo($v);
  129. }
  130. return $list;
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function getMetadata($path)
  136. {
  137. $r = $this->ossClient->getObjectMeta($this->bucket, $path);
  138. $r['key'] = $path;
  139. return $this->normalizeFileInfo($r);
  140. }
  141. /**
  142. * {@inheritdoc}
  143. */
  144. public function getSize($path)
  145. {
  146. return $this->getMetadata($path);
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function getMimetype($path)
  152. {
  153. $r = $this->ossClient->getObjectMeta($this->bucket, $path);
  154. return ['mimetype' => $r['content-type']];
  155. }
  156. /**
  157. * {@inheritdoc}
  158. */
  159. public function getTimestamp($path)
  160. {
  161. return $this->getMetadata($path);
  162. }
  163. protected function normalizeFileInfo($filestat)
  164. {
  165. return array(
  166. 'type' => 'file',
  167. 'path' => $filestat['key'],
  168. 'timestamp' => $filestat['last-modified'],
  169. 'size' => $filestat['content-length'],
  170. );
  171. }
  172. public function getThumb()
  173. {
  174. }
  175. }