QiniuAdapter.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. /**
  3. * 七牛云存储 适配器类
  4. */
  5. namespace App\Services\FileSystem;
  6. use Illuminate\Contracts\Filesystem\FileNotFoundException;
  7. use League\Flysystem\Adapter\AbstractAdapter;
  8. use League\Flysystem\Config;
  9. use Qiniu\Auth;
  10. use Qiniu\Cdn\CdnManager;
  11. use Qiniu\Storage\BucketManager;
  12. use Qiniu\Storage\UploadManager;
  13. use Symfony\Component\HttpFoundation\File\Exception\UploadException;
  14. class QiniuAdapter extends AbstractAdapter
  15. {
  16. private $accessKey;
  17. private $accessSecret;
  18. // images.learnku.net
  19. protected $uploadManager;
  20. protected $bucketManager;
  21. protected $cdnManager;
  22. private $bucketName;
  23. private $domainName;
  24. private $token;
  25. public function __construct($qiniu='qiniu', $prefix = '')
  26. {
  27. $this->accessKey = \config('filesystems.disks.'. $qiniu .'.access_key');
  28. $this->accessSecret = \config('filesystems.disks.'. $qiniu .'.secret_key');
  29. // 上传文件句柄
  30. $this->uploadManager = new UploadManager();
  31. $this->bucketName = \config('filesystems.disks.'. $qiniu .'.bucket');
  32. $this->domainName = \config('filesystems.disks.'. $qiniu .'.domain');
  33. $auth = new \Qiniu\Auth($this->accessKey, $this->accessSecret);
  34. // 管理文件句柄
  35. $this->bucketManager = new BucketManager($auth);
  36. $this->cdnManager = new CdnManager($auth);
  37. $this->token = $auth->uploadToken($this->bucketName);
  38. $this->setPathPrefix($prefix);
  39. }
  40. /**
  41. * 写一个新文件。
  42. * Write a new file.
  43. *
  44. * @param string $path
  45. * @param string $contents
  46. * @param Config $config Config object
  47. *
  48. * @return array|false false on failure file meta data on success
  49. * @throws \Exception
  50. */
  51. public function write($path, $contents, Config $config)
  52. {
  53. return $this->upload($path, $contents);
  54. }
  55. /**
  56. * 使用流写一个新文件。
  57. * Write a new file using a stream.
  58. *
  59. * @param string $path
  60. * @param resource $resource
  61. * @param Config $config Config object
  62. *
  63. * @return array|false false on failure file meta data on success
  64. * @throws \Exception
  65. */
  66. public function writeStream($path, $resource, Config $config)
  67. {
  68. return $this->upload($path, $resource, true);
  69. }
  70. /**
  71. * 更新文件。
  72. * Update a file.
  73. *
  74. * @param string $path
  75. * @param string $contents
  76. * @param Config $config Config object
  77. *
  78. * @return array|false false on failure file meta data on success
  79. * @throws \Exception
  80. */
  81. public function update($path, $contents, Config $config)
  82. {
  83. // 先删除
  84. $error = $this->bucketManager->delete($this->bucketName, $path);
  85. if ($error == null) {
  86. $urls = array(
  87. assert_images($path)
  88. );
  89. // 再上传
  90. $result = $this->upload($path, $contents);
  91. // 后刷新
  92. $this->cdnManager->refreshUrls($urls);
  93. return $result;
  94. }
  95. }
  96. /**
  97. * 使用流更新文件。
  98. * Update a file using a stream.
  99. *
  100. * @param string $path
  101. * @param resource $resource
  102. * @param Config $config Config object
  103. *
  104. * @return array|false false on failure file meta data on success
  105. * @throws \Exception
  106. */
  107. public function updateStream($path, $resource, Config $config)
  108. {
  109. return $this->upload($path, $resource, true);
  110. }
  111. /**
  112. * 重命名文件。
  113. * Rename a file.
  114. *
  115. * @param string $path
  116. * @param string $newpath
  117. *
  118. * @return bool
  119. */
  120. public function rename($path, $newpath)
  121. {
  122. $path = $this->addPrefix($path);
  123. $newpath = $this->addPrefix($newpath);
  124. $error = $this->bucketManager->rename($this->bucketName, $path, $newpath);
  125. return $error == null ? true : false;
  126. }
  127. /**
  128. * 复制文件。
  129. * Copy a file.
  130. *
  131. * @param string $path
  132. * @param string $newpath
  133. *
  134. * @return bool
  135. */
  136. public function copy($path, $newpath)
  137. {
  138. $path = $this->addPrefix($path);
  139. $newpath = $this->addPrefix($newpath);
  140. $error = $this->bucketManager->copy($this->bucketName, $path, $this->bucketName, $newpath);
  141. return $error == null ? true : false;
  142. }
  143. /**
  144. * 删除文件。
  145. * Delete a file.
  146. *
  147. * @param string $path
  148. *
  149. * @return bool
  150. */
  151. public function delete($path)
  152. {
  153. $this->addPrefix($path);
  154. $error = $this->bucketManager->delete($this->bucketName, $path);
  155. return $error == null ? true : false;
  156. }
  157. /**
  158. * 删除目录。
  159. * Delete a directory.
  160. *
  161. * @param string $dirname
  162. *
  163. * @return void
  164. */
  165. public function deleteDir($dirname)
  166. {
  167. throw new \BadFunctionCallException('暂不支持该操作');
  168. }
  169. /**
  170. * 创建一个目录。
  171. * Create a directory.
  172. *
  173. * @param string $dirname directory name
  174. * @param Config $config
  175. *
  176. * @return void
  177. */
  178. public function createDir($dirname, Config $config)
  179. {
  180. throw new \BadFunctionCallException('暂不支持该操作');
  181. }
  182. /**
  183. * 刷新预取
  184. * @param $urls
  185. */
  186. public function refresh($urls)
  187. {
  188. $this->cdnManager->refreshUrls($urls);
  189. }
  190. /**
  191. * 设置文件的可见性。
  192. * Set the visibility for a file.
  193. *
  194. * @param string $path
  195. * @param string $visibility
  196. *
  197. * @return void file meta data
  198. */
  199. public function setVisibility($path, $visibility)
  200. {
  201. throw new \BadFunctionCallException('暂不支持该操作');
  202. }
  203. /**
  204. * 检查文件是否存在。
  205. * Check whether a file exists.
  206. *
  207. * @param string $path
  208. *
  209. * @return array|bool|null
  210. */
  211. public function has($path)
  212. {
  213. $path = $this->addPrefix($path);
  214. $stat = $this->bucketManager->stat($this->bucketName, $path);
  215. if ($stat[0] == null) {
  216. return false;
  217. } else {
  218. return true;
  219. }
  220. }
  221. /**
  222. * 读一个文件。
  223. * Read a file.
  224. *
  225. * @param string $path
  226. *
  227. * @return array|false
  228. * @throws FileNotFoundException
  229. */
  230. public function read($path)
  231. {
  232. $path = $this->addPrefix($path);
  233. list($fileInfo, $error) = $this->bucketManager->stat($this->bucketName, $path);
  234. if ($fileInfo) {
  235. return $fileInfo;
  236. } else {
  237. throw new FileNotFoundException('对应文件不存在');
  238. }
  239. }
  240. /**
  241. * 将文件作为流读取。
  242. * Read a file as a stream.
  243. *
  244. * @param string $path
  245. *
  246. * @return void
  247. */
  248. public function readStream($path)
  249. {
  250. throw new \BadFunctionCallException('暂不支持该操作');
  251. }
  252. /**
  253. * 列出目录的内容。
  254. * List contents of a directory.
  255. *
  256. * @param string $directory
  257. * @param bool $recursive
  258. *
  259. * @return array
  260. */
  261. public function listContents($directory = '', $recursive = '')
  262. {
  263. list($ret, $err) = $this->bucketManager->listFiles($this->bucketName, $recursive);
  264. return $ret;
  265. // return $this->bucketManager->listFiles($this->bucketName, $recursive);
  266. }
  267. /**
  268. * 获取文件或目录的所有元数据。
  269. * Get all the meta data of a file or directory.
  270. *
  271. * @param string $path
  272. *
  273. * @return array|false
  274. * @throws FileNotFoundException
  275. */
  276. public function getMetadata($path)
  277. {
  278. return $this->read($path);
  279. }
  280. /**
  281. * 获取文件的大小。
  282. * Get the size of a file.
  283. *
  284. * @param string $path
  285. *
  286. * @return array|false
  287. * @throws FileNotFoundException
  288. */
  289. public function getSize($path)
  290. {
  291. $fileInfo = $this->read($path);
  292. return $fileInfo['fsize'];
  293. }
  294. /**
  295. * 获取文件的mimetype。
  296. * Get the mimetype of a file.
  297. *
  298. * @param string $path
  299. *
  300. * @return array|false
  301. * @throws FileNotFoundException
  302. */
  303. public function getMimetype($path)
  304. {
  305. $fileInfo = $this->read($path);
  306. return $fileInfo['fileType'];// TODO: Implement getMimetype() method.
  307. }
  308. /**
  309. * 获取文件的上次修改时间作为时间戳。
  310. * Get the last modified time of a file as a timestamp.
  311. *
  312. * @param string $path
  313. *
  314. * @return array|false
  315. * @throws FileNotFoundException
  316. */
  317. public function getTimestamp($path)
  318. {
  319. $fileInfo = $this->read($path);
  320. return $fileInfo['putTime'];
  321. }
  322. /**
  323. * 获取文件的可见性。
  324. * Get the visibility of a file.
  325. *
  326. * @param string $path
  327. *
  328. * @return void
  329. */
  330. public function getVisibility($path)
  331. {
  332. throw new \BadFunctionCallException('暂不支持该操作');
  333. }
  334. /**
  335. * @param string $path
  336. * @param $contents
  337. * @param bool $stream
  338. * @return mixed
  339. * @throws \Exception
  340. */
  341. protected function upload(string $path, $contents, $stream = false)
  342. {
  343. $path = $this->addPrefix($path);
  344. try {
  345. if ($stream) {
  346. $response = $this->uploadManager->put($this->token, $path, $contents);
  347. } else {
  348. $response = $this->uploadManager->putFile($this->token, $path, $contents);
  349. }
  350. } catch (\Exception $ex) {
  351. throw $ex;
  352. }
  353. list($uploadResult, $error) = $response;
  354. if ($uploadResult) {
  355. return $uploadResult;
  356. } else {
  357. throw new UploadException('上传文件到七牛失败:' . $error->message());
  358. }
  359. }
  360. protected function addPrefix($path)
  361. {
  362. return $this->applyPathPrefix($path);
  363. // return ltrim($path, '\\/');
  364. }
  365. }