QiniuAdapter.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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\AdapterInterface;
  12. use League\Flysystem\Config;
  13. use Qiniu\Auth;
  14. use Qiniu\Processing\ImageUrlBuilder;
  15. use Qiniu\Storage\UploadManager;
  16. use Qiniu\Storage\BucketManager;
  17. class QiniuAdapter extends AbstractAdapter
  18. {
  19. use NotSupportingVisibilityTrait;
  20. /**
  21. * @var \Qiniu\Storage\UploadManager
  22. */
  23. protected $uploadManager;
  24. /**
  25. * @var \Qiniu\Storage\BucketManager
  26. */
  27. protected $bucketManager;
  28. /**
  29. * @var string
  30. */
  31. protected $bucket;
  32. /**
  33. * @var string
  34. */
  35. protected $domain;
  36. /**
  37. * @var string
  38. */
  39. protected $token;
  40. /**
  41. * Constructor.
  42. */
  43. public function __construct($ak, $sk, $bucket, $domain)
  44. {
  45. $auth = new Auth($ak, $sk);
  46. $this->bucket = $bucket;
  47. $this->domain = $domain;
  48. $this->token = $auth->uploadToken($bucket);
  49. $this->uploadManager = new UploadManager();
  50. $this->bucketManager = new BucketManager($auth);
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function write($path, $contents, Config $config)
  56. {
  57. list($ret, $error) = $this->uploadManager->put($this->token, $path, $contents);
  58. if ($error) {
  59. return false;
  60. }
  61. return $ret;
  62. }
  63. public function put($path, $contents, Config $config)
  64. {
  65. list($ret, $error) = $this->uploadManager->put($this->token, $path, $contents);
  66. if ($error) {
  67. return false;
  68. }
  69. return $ret;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function writeStream($path, $resource, Config $config)
  75. {
  76. list($ret, $error) = $this->uploadManager->put($this->token, $path, $resource);
  77. if ($error) {
  78. return false;
  79. }
  80. return $ret;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function putStream($path, $resource, Config $config)
  86. {
  87. list($ret, $error) = $this->uploadManager->put($this->token, $path, $resource);
  88. if ($error) {
  89. return false;
  90. }
  91. return $ret;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function update($path, $contents, Config $config)
  97. {
  98. $this->delete($path);
  99. return $this->write($path, $contents, $config);
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function updateStream($path, $resource, Config $config)
  105. {
  106. $this->delete($path);
  107. return $this->writeStream($path, $resource, $config);
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function rename($path, $newpath)
  113. {
  114. $r = $this->bucketManager->rename($this->bucket, $path, $newpath);
  115. return is_null($r);
  116. }
  117. public function copy($path, $newpath)
  118. {
  119. $r = $this->bucketManager->copy($this->bucket, $path, $this->bucket, $newpath);
  120. return is_null($r);
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function delete($path)
  126. {
  127. return $this->bucketManager->delete($this->bucket, $path);
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function deleteDir($dirname)
  133. {
  134. return true;
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function createDir($dirname, Config $config)
  140. {
  141. return ['path' => $dirname, 'type' => 'dir'];
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. public function has($path)
  147. {
  148. $r = $this->bucketManager->stat($this->bucket, $path);
  149. return is_array($r[0]);
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function read($path)
  155. {
  156. return false;
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function readStream($path)
  162. {
  163. // @todo readStream;
  164. return false;
  165. }
  166. /**
  167. * {@inheritdoc}
  168. */
  169. public function listContents($directory = '', $recursive = false)
  170. {
  171. $list = [];
  172. $r = $this->bucketManager->listFiles($this->bucket, $directory);
  173. foreach ($r[0] as $v) {
  174. $list[] = $this->normalizeFileInfo($v);
  175. }
  176. return $list;
  177. }
  178. /**
  179. * {@inheritdoc}
  180. */
  181. public function getMetadata($path)
  182. {
  183. $r = $this->bucketManager->stat($this->bucket, $path);
  184. $r[0]['key'] = $path;
  185. return $this->normalizeFileInfo($r[0]);
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function getSize($path)
  191. {
  192. return $this->getMetadata($path);
  193. }
  194. /**
  195. * {@inheritdoc}
  196. */
  197. public function getMimetype($path)
  198. {
  199. $r = $this->bucketManager->stat($this->bucket, $path);
  200. return ['mimetype' => $r[0]['mimeType']];
  201. }
  202. /**
  203. * {@inheritdoc}
  204. */
  205. public function getTimestamp($path)
  206. {
  207. return $this->getMetadata($path);
  208. }
  209. protected function normalizeFileInfo($filestat)
  210. {
  211. return array(
  212. 'type' => 'file',
  213. 'path' => $filestat['key'],
  214. 'timestamp' => floor($filestat['putTime']/10000000),
  215. 'size' => $filestat['fsize'],
  216. );
  217. }
  218. public function getUrl($path)
  219. {
  220. return $this->domain . '/' . $path;
  221. }
  222. public function thumbnail($path, $width, $height)
  223. {
  224. $imageBuilder = new ImageUrlBuilder();
  225. if (strpos($path, $this->domain . '/') !== false) {
  226. $path = substr($path, strlen($this->domain . '/'));
  227. }
  228. return $imageBuilder->thumbnail($this->domain . '/' . $path, 1, $width, $height);
  229. }
  230. public function crop($path, $width, $height, array $start = [0, 0])
  231. {
  232. if (strpos($path, $this->domain . '/') !== false) {
  233. $path = substr($path, strlen($this->domain . '/'));
  234. }
  235. return $this->domain . '/' . $path . '?imageMogr2/crop/!' . $width . 'x' . $height . 'a' . $start[0] . 'a' . $start[1];
  236. }
  237. public function water()
  238. {
  239. // TODO: Implement water() method.
  240. }
  241. }