PageRenderTime 29ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

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