PageRenderTime 85ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/league/flysystem/src/Adapter/Local.php

https://gitlab.com/dzakiafif/cokelatklasik
PHP | 401 lines | 233 code | 72 blank | 96 comment | 20 complexity | 4de0585734d66c57808f1249da2fe047 MD5 | raw file
  1. <?php
  2. namespace League\Flysystem\Adapter;
  3. use DirectoryIterator;
  4. use FilesystemIterator;
  5. use Finfo;
  6. use League\Flysystem\AdapterInterface;
  7. use League\Flysystem\Config;
  8. use League\Flysystem\NotSupportedException;
  9. use League\Flysystem\Util;
  10. use RecursiveDirectoryIterator;
  11. use RecursiveIteratorIterator;
  12. use SplFileInfo;
  13. class Local extends AbstractAdapter
  14. {
  15. protected static $permissions = [
  16. 'public' => 0744,
  17. 'private' => 0700,
  18. ];
  19. /**
  20. * @var string
  21. */
  22. protected $pathSeparator = DIRECTORY_SEPARATOR;
  23. /**
  24. * Constructor.
  25. *
  26. * @param string $root
  27. */
  28. public function __construct($root)
  29. {
  30. $realRoot = $this->ensureDirectory($root);
  31. if (! is_dir($realRoot) || ! is_readable($realRoot)) {
  32. throw new \LogicException('The root path '.$root.' is not readable.');
  33. }
  34. $this->setPathPrefix($realRoot);
  35. }
  36. /**
  37. * Ensure the root directory exists.
  38. *
  39. * @param string $root root directory path
  40. *
  41. * @return string real path to root
  42. */
  43. protected function ensureDirectory($root)
  44. {
  45. if (! is_dir($root)) {
  46. $umask = umask(0);
  47. mkdir($root, 0777, true);
  48. umask($umask);
  49. }
  50. return realpath($root);
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function has($path)
  56. {
  57. $location = $this->applyPathPrefix($path);
  58. return file_exists($location);
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function write($path, $contents, Config $config)
  64. {
  65. $location = $this->applyPathPrefix($path);
  66. $this->ensureDirectory(dirname($location));
  67. if (($size = file_put_contents($location, $contents, LOCK_EX)) === false) {
  68. return false;
  69. }
  70. $type = 'file';
  71. $result = compact('contents', 'type', 'size', 'path');
  72. if ($visibility = $config->get('visibility')) {
  73. $result['visibility'] = $visibility;
  74. $this->setVisibility($path, $visibility);
  75. }
  76. return $result;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function writeStream($path, $resource, Config $config)
  82. {
  83. $location = $this->applyPathPrefix($path);
  84. $this->ensureDirectory(dirname($location));
  85. if (! $stream = fopen($location, 'w+')) {
  86. return false;
  87. }
  88. stream_copy_to_stream($resource, $stream);
  89. if (! fclose($stream)) {
  90. return false;
  91. }
  92. if ($visibility = $config->get('visibility')) {
  93. $this->setVisibility($path, $visibility);
  94. }
  95. return compact('path', 'visibility');
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function readStream($path)
  101. {
  102. $location = $this->applyPathPrefix($path);
  103. $stream = fopen($location, 'r');
  104. return compact('stream', 'path');
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function updateStream($path, $resource, Config $config)
  110. {
  111. return $this->writeStream($path, $resource, $config);
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function update($path, $contents, Config $config)
  117. {
  118. $location = $this->applyPathPrefix($path);
  119. $mimetype = Util::guessMimeType($path, $contents);
  120. if (($size = file_put_contents($location, $contents, LOCK_EX)) === false) {
  121. return false;
  122. }
  123. return compact('path', 'size', 'contents', 'mimetype');
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function read($path)
  129. {
  130. $location = $this->applyPathPrefix($path);
  131. $contents = file_get_contents($location);
  132. if ($contents === false) {
  133. return false;
  134. }
  135. return compact('contents', 'path');
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function rename($path, $newpath)
  141. {
  142. $location = $this->applyPathPrefix($path);
  143. $destination = $this->applyPathPrefix($newpath);
  144. $parentDirectory = $this->applyPathPrefix(Util::dirname($newpath));
  145. $this->ensureDirectory($parentDirectory);
  146. return rename($location, $destination);
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function copy($path, $newpath)
  152. {
  153. $location = $this->applyPathPrefix($path);
  154. $destination = $this->applyPathPrefix($newpath);
  155. $this->ensureDirectory(dirname($destination));
  156. return copy($location, $destination);
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function delete($path)
  162. {
  163. $location = $this->applyPathPrefix($path);
  164. return unlink($location);
  165. }
  166. /**
  167. * {@inheritdoc}
  168. */
  169. public function listContents($directory = '', $recursive = false)
  170. {
  171. $result = [];
  172. $location = $this->applyPathPrefix($directory).$this->pathSeparator;
  173. if (! is_dir($location)) {
  174. return [];
  175. }
  176. $iterator = $recursive ? $this->getRecursiveDirectoryIterator($location) : $this->getDirectoryIterator($location);
  177. foreach ($iterator as $file) {
  178. $path = $this->getFilePath($file);
  179. if (preg_match('#(^|/|\\\\)\.{1,2}$#', $path)) {
  180. continue;
  181. }
  182. $result[] = $this->normalizeFileInfo($file);
  183. }
  184. return $result;
  185. }
  186. /**
  187. * {@inheritdoc}
  188. */
  189. public function getMetadata($path)
  190. {
  191. $location = $this->applyPathPrefix($path);
  192. $info = new SplFileInfo($location);
  193. return $this->normalizeFileInfo($info);
  194. }
  195. /**
  196. * {@inheritdoc}
  197. */
  198. public function getSize($path)
  199. {
  200. return $this->getMetadata($path);
  201. }
  202. /**
  203. * {@inheritdoc}
  204. */
  205. public function getMimetype($path)
  206. {
  207. $location = $this->applyPathPrefix($path);
  208. $finfo = new Finfo(FILEINFO_MIME_TYPE);
  209. return ['mimetype' => $finfo->file($location)];
  210. }
  211. /**
  212. * {@inheritdoc}
  213. */
  214. public function getTimestamp($path)
  215. {
  216. return $this->getMetadata($path);
  217. }
  218. /**
  219. * {@inheritdoc}
  220. */
  221. public function getVisibility($path)
  222. {
  223. $location = $this->applyPathPrefix($path);
  224. clearstatcache(false, $location);
  225. $permissions = octdec(substr(sprintf('%o', fileperms($location)), -4));
  226. $visibility = $permissions & 0044 ? AdapterInterface::VISIBILITY_PUBLIC : AdapterInterface::VISIBILITY_PRIVATE;
  227. return compact('visibility');
  228. }
  229. /**
  230. * {@inheritdoc}
  231. */
  232. public function setVisibility($path, $visibility)
  233. {
  234. $location = $this->applyPathPrefix($path);
  235. chmod($location, static::$permissions[$visibility]);
  236. return compact('visibility');
  237. }
  238. /**
  239. * {@inheritdoc}
  240. */
  241. public function createDir($dirname, Config $config)
  242. {
  243. $location = $this->applyPathPrefix($dirname);
  244. $umask = umask(0);
  245. if (! is_dir($location) && ! mkdir($location, 0777, true)) {
  246. $return = false;
  247. } else {
  248. $return = ['path' => $dirname, 'type' => 'dir'];
  249. }
  250. umask($umask);
  251. return $return;
  252. }
  253. /**
  254. * {@inheritdoc}
  255. */
  256. public function deleteDir($dirname)
  257. {
  258. $location = $this->applyPathPrefix($dirname);
  259. if (! is_dir($location)) {
  260. return false;
  261. }
  262. $contents = $this->listContents($dirname, true);
  263. $contents = array_reverse($contents);
  264. foreach ($contents as $file) {
  265. if ($file['type'] !== 'dir') {
  266. unlink($this->applyPathPrefix($file['path']));
  267. } else {
  268. rmdir($this->applyPathPrefix($file['path']));
  269. }
  270. }
  271. return rmdir($location);
  272. }
  273. /**
  274. * Normalize the file info.
  275. *
  276. * @param SplFileInfo $file
  277. *
  278. * @return array
  279. */
  280. protected function normalizeFileInfo(SplFileInfo $file)
  281. {
  282. if ($file->isLink()) {
  283. throw NotSupportedException::forLink($file);
  284. }
  285. $normalized = [
  286. 'type' => $file->getType(),
  287. 'path' => $this->getFilePath($file),
  288. ];
  289. $normalized['timestamp'] = $file->getMTime();
  290. if ($normalized['type'] === 'file') {
  291. $normalized['size'] = $file->getSize();
  292. }
  293. return $normalized;
  294. }
  295. /**
  296. * Get the normalized path from a SplFileInfo object.
  297. *
  298. * @param SplFileInfo $file
  299. *
  300. * @return string
  301. */
  302. protected function getFilePath(SplFileInfo $file)
  303. {
  304. $path = $file->getPathname();
  305. $path = $this->removePathPrefix($path);
  306. return trim($path, '\\/');
  307. }
  308. /**
  309. * @param string $path
  310. *
  311. * @return RecursiveIteratorIterator
  312. */
  313. protected function getRecursiveDirectoryIterator($path)
  314. {
  315. $directory = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS);
  316. $iterator = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST);
  317. return $iterator;
  318. }
  319. /**
  320. * @param string $path
  321. *
  322. * @return DirectoryIterator
  323. */
  324. protected function getDirectoryIterator($path)
  325. {
  326. $iterator = new DirectoryIterator($path);
  327. return $iterator;
  328. }
  329. }