PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/Front_End/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php

https://gitlab.com/Sigpot/AirSpot
PHP | 383 lines | 171 code | 40 blank | 172 comment | 12 complexity | e81470f45fc9ca88eedd70f3b28db620 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Filesystem;
  3. use RuntimeException;
  4. use InvalidArgumentException;
  5. use Illuminate\Support\Collection;
  6. use League\Flysystem\AdapterInterface;
  7. use League\Flysystem\FilesystemInterface;
  8. use League\Flysystem\AwsS3v3\AwsS3Adapter;
  9. use League\Flysystem\FileNotFoundException;
  10. use League\Flysystem\Adapter\Local as LocalAdapter;
  11. use Illuminate\Contracts\Filesystem\Filesystem as FilesystemContract;
  12. use Illuminate\Contracts\Filesystem\Cloud as CloudFilesystemContract;
  13. use Illuminate\Contracts\Filesystem\FileNotFoundException as ContractFileNotFoundException;
  14. class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
  15. {
  16. /**
  17. * The Flysystem filesystem implementation.
  18. *
  19. * @var \League\Flysystem\FilesystemInterface
  20. */
  21. protected $driver;
  22. /**
  23. * Create a new filesystem adapter instance.
  24. *
  25. * @param \League\Flysystem\FilesystemInterface $driver
  26. * @return void
  27. */
  28. public function __construct(FilesystemInterface $driver)
  29. {
  30. $this->driver = $driver;
  31. }
  32. /**
  33. * Determine if a file exists.
  34. *
  35. * @param string $path
  36. * @return bool
  37. */
  38. public function exists($path)
  39. {
  40. return $this->driver->has($path);
  41. }
  42. /**
  43. * Get the contents of a file.
  44. *
  45. * @param string $path
  46. * @return string
  47. *
  48. * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
  49. */
  50. public function get($path)
  51. {
  52. try {
  53. return $this->driver->read($path);
  54. } catch (FileNotFoundException $e) {
  55. throw new ContractFileNotFoundException($path, $e->getCode(), $e);
  56. }
  57. }
  58. /**
  59. * Write the contents of a file.
  60. *
  61. * @param string $path
  62. * @param string|resource $contents
  63. * @param string $visibility
  64. * @return bool
  65. */
  66. public function put($path, $contents, $visibility = null)
  67. {
  68. if ($visibility = $this->parseVisibility($visibility)) {
  69. $config = ['visibility' => $visibility];
  70. } else {
  71. $config = [];
  72. }
  73. if (is_resource($contents)) {
  74. return $this->driver->putStream($path, $contents, $config);
  75. } else {
  76. return $this->driver->put($path, $contents, $config);
  77. }
  78. }
  79. /**
  80. * Get the visibility for the given path.
  81. *
  82. * @param string $path
  83. * @return string
  84. */
  85. public function getVisibility($path)
  86. {
  87. if ($this->driver->getVisibility($path) == AdapterInterface::VISIBILITY_PUBLIC) {
  88. return FilesystemContract::VISIBILITY_PUBLIC;
  89. }
  90. return FilesystemContract::VISIBILITY_PRIVATE;
  91. }
  92. /**
  93. * Set the visibility for the given path.
  94. *
  95. * @param string $path
  96. * @param string $visibility
  97. * @return void
  98. */
  99. public function setVisibility($path, $visibility)
  100. {
  101. return $this->driver->setVisibility($path, $this->parseVisibility($visibility));
  102. }
  103. /**
  104. * Prepend to a file.
  105. *
  106. * @param string $path
  107. * @param string $data
  108. * @return int
  109. */
  110. public function prepend($path, $data, $separator = PHP_EOL)
  111. {
  112. if ($this->exists($path)) {
  113. return $this->put($path, $data.$separator.$this->get($path));
  114. }
  115. return $this->put($path, $data);
  116. }
  117. /**
  118. * Append to a file.
  119. *
  120. * @param string $path
  121. * @param string $data
  122. * @return int
  123. */
  124. public function append($path, $data, $separator = PHP_EOL)
  125. {
  126. if ($this->exists($path)) {
  127. return $this->put($path, $this->get($path).$separator.$data);
  128. }
  129. return $this->put($path, $data);
  130. }
  131. /**
  132. * Delete the file at a given path.
  133. *
  134. * @param string|array $paths
  135. * @return bool
  136. */
  137. public function delete($paths)
  138. {
  139. $paths = is_array($paths) ? $paths : func_get_args();
  140. foreach ($paths as $path) {
  141. try {
  142. $this->driver->delete($path);
  143. } catch (FileNotFoundException $e) {
  144. //
  145. }
  146. }
  147. return true;
  148. }
  149. /**
  150. * Copy a file to a new location.
  151. *
  152. * @param string $from
  153. * @param string $to
  154. * @return bool
  155. */
  156. public function copy($from, $to)
  157. {
  158. return $this->driver->copy($from, $to);
  159. }
  160. /**
  161. * Move a file to a new location.
  162. *
  163. * @param string $from
  164. * @param string $to
  165. * @return bool
  166. */
  167. public function move($from, $to)
  168. {
  169. return $this->driver->rename($from, $to);
  170. }
  171. /**
  172. * Get the file size of a given file.
  173. *
  174. * @param string $path
  175. * @return int
  176. */
  177. public function size($path)
  178. {
  179. return $this->driver->getSize($path);
  180. }
  181. /**
  182. * Get the mime-type of a given file.
  183. *
  184. * @param string $path
  185. * @return string|false
  186. */
  187. public function mimeType($path)
  188. {
  189. return $this->driver->getMimetype($path);
  190. }
  191. /**
  192. * Get the file's last modification time.
  193. *
  194. * @param string $path
  195. * @return int
  196. */
  197. public function lastModified($path)
  198. {
  199. return $this->driver->getTimestamp($path);
  200. }
  201. /**
  202. * Get the URL for the file at the given path.
  203. *
  204. * @param string $path
  205. * @return string
  206. */
  207. public function url($path)
  208. {
  209. $adapter = $this->driver->getAdapter();
  210. if ($adapter instanceof AwsS3Adapter) {
  211. $path = $adapter->getPathPrefix().$path;
  212. return $adapter->getClient()->getObjectUrl($adapter->getBucket(), $path);
  213. } elseif ($adapter instanceof LocalAdapter) {
  214. return '/storage/'.$path;
  215. } elseif (method_exists($adapter, 'getUrl')) {
  216. return $adapter->getUrl($path);
  217. } else {
  218. throw new RuntimeException('This driver does not support retrieving URLs.');
  219. }
  220. }
  221. /**
  222. * Get an array of all files in a directory.
  223. *
  224. * @param string|null $directory
  225. * @param bool $recursive
  226. * @return array
  227. */
  228. public function files($directory = null, $recursive = false)
  229. {
  230. $contents = $this->driver->listContents($directory, $recursive);
  231. return $this->filterContentsByType($contents, 'file');
  232. }
  233. /**
  234. * Get all of the files from the given directory (recursive).
  235. *
  236. * @param string|null $directory
  237. * @return array
  238. */
  239. public function allFiles($directory = null)
  240. {
  241. return $this->files($directory, true);
  242. }
  243. /**
  244. * Get all of the directories within a given directory.
  245. *
  246. * @param string|null $directory
  247. * @param bool $recursive
  248. * @return array
  249. */
  250. public function directories($directory = null, $recursive = false)
  251. {
  252. $contents = $this->driver->listContents($directory, $recursive);
  253. return $this->filterContentsByType($contents, 'dir');
  254. }
  255. /**
  256. * Get all (recursive) of the directories within a given directory.
  257. *
  258. * @param string|null $directory
  259. * @return array
  260. */
  261. public function allDirectories($directory = null)
  262. {
  263. return $this->directories($directory, true);
  264. }
  265. /**
  266. * Create a directory.
  267. *
  268. * @param string $path
  269. * @return bool
  270. */
  271. public function makeDirectory($path)
  272. {
  273. return $this->driver->createDir($path);
  274. }
  275. /**
  276. * Recursively delete a directory.
  277. *
  278. * @param string $directory
  279. * @return bool
  280. */
  281. public function deleteDirectory($directory)
  282. {
  283. return $this->driver->deleteDir($directory);
  284. }
  285. /**
  286. * Get the Flysystem driver.
  287. *
  288. * @return \League\Flysystem\FilesystemInterface
  289. */
  290. public function getDriver()
  291. {
  292. return $this->driver;
  293. }
  294. /**
  295. * Filter directory contents by type.
  296. *
  297. * @param array $contents
  298. * @param string $type
  299. * @return array
  300. */
  301. protected function filterContentsByType($contents, $type)
  302. {
  303. return Collection::make($contents)
  304. ->where('type', $type)
  305. ->pluck('path')
  306. ->values()
  307. ->all();
  308. }
  309. /**
  310. * Parse the given visibility value.
  311. *
  312. * @param string|null $visibility
  313. * @return string|null
  314. *
  315. * @throws \InvalidArgumentException
  316. */
  317. protected function parseVisibility($visibility)
  318. {
  319. if (is_null($visibility)) {
  320. return;
  321. }
  322. switch ($visibility) {
  323. case FilesystemContract::VISIBILITY_PUBLIC:
  324. return AdapterInterface::VISIBILITY_PUBLIC;
  325. case FilesystemContract::VISIBILITY_PRIVATE:
  326. return AdapterInterface::VISIBILITY_PRIVATE;
  327. }
  328. throw new InvalidArgumentException('Unknown visibility: '.$visibility);
  329. }
  330. /**
  331. * Pass dynamic methods call onto Flysystem.
  332. *
  333. * @param string $method
  334. * @param array $parameters
  335. * @return mixed
  336. *
  337. * @throws \BadMethodCallException
  338. */
  339. public function __call($method, array $parameters)
  340. {
  341. return call_user_func_array([$this->driver, $method], $parameters);
  342. }
  343. }