FilesystemAdapter.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. namespace common\modules\attachment\components;
  3. use common\modules\attachment\components\contracts\Cloud as CloudFilesystemContract;
  4. use common\modules\attachment\components\contracts\FileNotFoundException as ContractFileNotFoundException;
  5. use common\modules\attachment\components\contracts\Filesystem as FilesystemContract;
  6. use common\modules\attachment\components\contracts\ImageProcessor as ImageContract;
  7. use common\modules\attachment\components\flysystem\QiniuAdapter;
  8. use creocoder\flysystem\Filesystem;
  9. use InvalidArgumentException;
  10. use League\Flysystem\Adapter\Local as LocalAdapter;
  11. use League\Flysystem\AdapterInterface;
  12. use League\Flysystem\FileNotFoundException;
  13. use RuntimeException;
  14. class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract, ImageContract
  15. {
  16. /**
  17. * The Flysystem filesystem implementation.
  18. *
  19. * @var Filesystem
  20. */
  21. protected $driver;
  22. /**
  23. * Create a new filesystem adapter instance.
  24. *
  25. * @param Filesystem $driver
  26. */
  27. public function __construct(Filesystem $driver)
  28. {
  29. $this->driver = $driver;
  30. }
  31. /**
  32. * Determine if a file exists.
  33. *
  34. * @param string $path
  35. * @return bool
  36. */
  37. public function exists($path)
  38. {
  39. return $this->driver->has($path);
  40. }
  41. /**
  42. * Get the contents of a file.
  43. *
  44. * @param string $path
  45. * @return string
  46. *
  47. * @throws \common\modules\attachment\components\contracts\FileNotFoundException
  48. */
  49. public function get($path)
  50. {
  51. try {
  52. return $this->driver->read($path);
  53. } catch (FileNotFoundException $e) {
  54. throw new ContractFileNotFoundException($path, $e->getCode(), $e);
  55. }
  56. }
  57. /**
  58. * Write the contents of a file.
  59. *
  60. * @param string $path
  61. * @param string|resource|UploadedFile $contents
  62. * @param array $options
  63. * @return bool
  64. */
  65. public function put($path, $contents, $options = [])
  66. {
  67. if (is_string($options)) {
  68. $options = ['visibility' => $options];
  69. }
  70. // If the given contents is actually a file or uploaded file instance than we will
  71. // automatically store the file using a stream. This provides a convenient path
  72. // for the developer to store streams without managing them manually in code.
  73. if ($contents instanceof UploadedFile) {
  74. return $this->putFile($path, $contents, $options);
  75. }
  76. return is_resource($contents)
  77. ? $this->driver->putStream($path, $contents, $options)
  78. : $this->driver->put($path, $contents, $options);
  79. }
  80. /**
  81. * Store the uploaded file on the disk.
  82. *
  83. * @param string $path
  84. * @param UploadedFile $file
  85. * @param array $options
  86. * @return string|false
  87. */
  88. public function putFile($path, $file, $options = [])
  89. {
  90. return $this->putFileAs($path, $file, $file->getHashName(), $options);
  91. }
  92. /**
  93. * Store the uploaded file on the disk with a given name.
  94. *
  95. * @param string $path
  96. * @param UploadedFile $file
  97. * @param string $name
  98. * @param array $options
  99. * @return string|false
  100. */
  101. public function putFileAs($path, $file, $name, $options = [])
  102. {
  103. // $stream = fopen($file->tempName, 'r+');
  104. $stream = file_get_contents($file->tempName);
  105. // Next, we will format the path of the file and store the file using a stream since
  106. // they provide better performance than alternatives. Once we write the file this
  107. // stream will get closed automatically by us so the developer doesn't have to.
  108. $result = $this->put(
  109. $path = trim($path.'/'.$name, '/'), $stream, $options
  110. );
  111. if (is_resource($stream)) {
  112. fclose($stream);
  113. }
  114. return $result ? $path : false;
  115. }
  116. /**
  117. * Get the visibility for the given path.
  118. *
  119. * @param string $path
  120. * @return string
  121. */
  122. public function getVisibility($path)
  123. {
  124. if ($this->driver->getVisibility($path) == AdapterInterface::VISIBILITY_PUBLIC) {
  125. return FilesystemContract::VISIBILITY_PUBLIC;
  126. }
  127. return FilesystemContract::VISIBILITY_PRIVATE;
  128. }
  129. /**
  130. * Set the visibility for the given path.
  131. *
  132. * @param string $path
  133. * @param string $visibility
  134. * @return void
  135. */
  136. public function setVisibility($path, $visibility)
  137. {
  138. return $this->driver->setVisibility($path, $this->parseVisibility($visibility));
  139. }
  140. /**
  141. * Prepend to a file.
  142. *
  143. * @param string $path
  144. * @param string $data
  145. * @param string $separator
  146. * @return int
  147. */
  148. public function prepend($path, $data, $separator = PHP_EOL)
  149. {
  150. if ($this->exists($path)) {
  151. return $this->put($path, $data.$separator.$this->get($path));
  152. }
  153. return $this->put($path, $data);
  154. }
  155. /**
  156. * Append to a file.
  157. *
  158. * @param string $path
  159. * @param string $data
  160. * @param string $separator
  161. * @return int
  162. */
  163. public function append($path, $data, $separator = PHP_EOL)
  164. {
  165. if ($this->exists($path)) {
  166. return $this->put($path, $this->get($path).$separator.$data);
  167. }
  168. return $this->put($path, $data);
  169. }
  170. /**
  171. * Delete the file at a given path.
  172. *
  173. * @param string|array $paths
  174. * @return bool
  175. */
  176. public function delete($paths)
  177. {
  178. $paths = is_array($paths) ? $paths : func_get_args();
  179. $success = true;
  180. foreach ($paths as $path) {
  181. try {
  182. if (! $this->driver->delete($path)) {
  183. $success = false;
  184. }
  185. } catch (FileNotFoundException $e) {
  186. $success = false;
  187. }
  188. }
  189. return $success;
  190. }
  191. /**
  192. * Copy a file to a new location.
  193. *
  194. * @param string $from
  195. * @param string $to
  196. * @return bool
  197. */
  198. public function copy($from, $to)
  199. {
  200. return $this->driver->copy($from, $to);
  201. }
  202. /**
  203. * Move a file to a new location.
  204. *
  205. * @param string $from
  206. * @param string $to
  207. * @return bool
  208. */
  209. public function move($from, $to)
  210. {
  211. return $this->driver->rename($from, $to);
  212. }
  213. /**
  214. * Get the file size of a given file.
  215. *
  216. * @param string $path
  217. * @return int
  218. */
  219. public function size($path)
  220. {
  221. return $this->driver->getSize($path);
  222. }
  223. /**
  224. * Get the mime-type of a given file.
  225. *
  226. * @param string $path
  227. * @return string|false
  228. */
  229. public function mimeType($path)
  230. {
  231. return $this->driver->getMimetype($path);
  232. }
  233. /**
  234. * Get the file's last modification time.
  235. *
  236. * @param string $path
  237. * @return int
  238. */
  239. public function lastModified($path)
  240. {
  241. return $this->driver->getTimestamp($path);
  242. }
  243. /**
  244. * Get the URL for the file at the given path.
  245. *
  246. * @param string $path
  247. * @return string
  248. */
  249. public function getUrl($path)
  250. {
  251. $adapter = $this->driver->getAdapter();
  252. if (method_exists($adapter, 'getUrl')) {
  253. return $adapter->getUrl($path);
  254. } elseif ($adapter instanceof QiniuAdapter) {
  255. return $this->getQiniuUrl($adapter, $path);
  256. } elseif ($adapter instanceof LocalAdapter) {
  257. return $this->getLocalUrl($path);
  258. } else {
  259. throw new RuntimeException('This driver does not support retrieving URLs.');
  260. }
  261. }
  262. /**
  263. * Get the URL for the file at the given path.
  264. *
  265. * @param QiniuAdapter $adapter
  266. * @param string $path
  267. * @return string
  268. */
  269. protected function getQiniuUrl($adapter, $path)
  270. {
  271. return $adapter->getUrl($path);
  272. }
  273. /**
  274. * Get the URL for the file at the given path.
  275. *
  276. * @param string $path
  277. * @return string
  278. */
  279. protected function getLocalUrl($path)
  280. {
  281. $url = $this->driver->url;
  282. return rtrim($url, '/').'/'.ltrim($path, '/');
  283. }
  284. public function thumbnail($path, $width, $height)
  285. {
  286. $adapter = $this->driver->getAdapter();
  287. if (method_exists($adapter, 'thumbnail')) {
  288. try {
  289. return $adapter->thumbnail($path, $width, $height);
  290. } catch(\Exception $e) {
  291. return $path;
  292. }
  293. } else {
  294. throw new RuntimeException('This driver does not support thumbnail');
  295. }
  296. }
  297. public function crop($path, $width, $height, array $start = [0, 0])
  298. {
  299. $adapter = $this->driver->getAdapter();
  300. if (method_exists($adapter, 'crop')) {
  301. try {
  302. return $adapter->crop($path, $width, $height, $start);
  303. } catch(\Exception $e) {
  304. return $path;
  305. }
  306. } else {
  307. throw new RuntimeException('This driver does not support crop');
  308. }
  309. }
  310. /**
  311. * Create a directory.
  312. *
  313. * @param string $path
  314. * @return bool
  315. */
  316. public function makeDirectory($path)
  317. {
  318. return $this->driver->createDir($path);
  319. }
  320. /**
  321. * Recursively delete a directory.
  322. *
  323. * @param string $directory
  324. * @return bool
  325. */
  326. public function deleteDirectory($directory)
  327. {
  328. return $this->driver->deleteDir($directory);
  329. }
  330. /**
  331. * Get the Flysystem driver.
  332. *
  333. * @return Filesystem
  334. */
  335. public function getDriver()
  336. {
  337. return $this->driver;
  338. }
  339. /**
  340. * Parse the given visibility value.
  341. *
  342. * @param string|null $visibility
  343. * @return string|null
  344. *
  345. * @throws \InvalidArgumentException
  346. */
  347. protected function parseVisibility($visibility)
  348. {
  349. if (is_null($visibility)) {
  350. return;
  351. }
  352. switch ($visibility) {
  353. case FilesystemContract::VISIBILITY_PUBLIC:
  354. return AdapterInterface::VISIBILITY_PUBLIC;
  355. case FilesystemContract::VISIBILITY_PRIVATE:
  356. return AdapterInterface::VISIBILITY_PRIVATE;
  357. }
  358. throw new InvalidArgumentException('Unknown visibility: '.$visibility);
  359. }
  360. /**
  361. * Pass dynamic methods call onto Flysystem.
  362. *
  363. * @param string $method
  364. * @param array $parameters
  365. * @return mixed
  366. *
  367. * @throws \BadMethodCallException
  368. */
  369. public function __call($method, array $parameters)
  370. {
  371. return call_user_func_array([$this->driver, $method], $parameters);
  372. }
  373. }