LocalFilesystemAdapter.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Illuminate\Filesystem;
  3. use Closure;
  4. use Illuminate\Support\Traits\Conditionable;
  5. use RuntimeException;
  6. class LocalFilesystemAdapter extends FilesystemAdapter
  7. {
  8. use Conditionable;
  9. /**
  10. * The name of the filesystem disk.
  11. *
  12. * @var string
  13. */
  14. protected $disk;
  15. /**
  16. * Indicates if signed URLs should serve corresponding files.
  17. *
  18. * @var bool
  19. */
  20. protected $shouldServeSignedUrls = false;
  21. /**
  22. * The Closure that should be used to resolve the URL generator.
  23. *
  24. * @var \Closure
  25. */
  26. protected $urlGeneratorResolver;
  27. /**
  28. * Determine if temporary URLs can be generated.
  29. *
  30. * @return bool
  31. */
  32. public function providesTemporaryUrls()
  33. {
  34. return $this->temporaryUrlCallback || (
  35. $this->shouldServeSignedUrls && $this->urlGeneratorResolver instanceof Closure
  36. );
  37. }
  38. /**
  39. * Determine if temporary upload URLs can be generated.
  40. *
  41. * @return bool
  42. */
  43. public function providesTemporaryUploadUrls()
  44. {
  45. return $this->temporaryUploadUrlCallback || (
  46. $this->shouldServeSignedUrls && $this->urlGeneratorResolver instanceof Closure
  47. );
  48. }
  49. /**
  50. * Get a temporary URL for the file at the given path.
  51. *
  52. * @param string $path
  53. * @param \DateTimeInterface $expiration
  54. * @param array $options
  55. * @return string
  56. */
  57. public function temporaryUrl($path, $expiration, array $options = [])
  58. {
  59. if ($this->temporaryUrlCallback) {
  60. return $this->temporaryUrlCallback->bindTo($this, static::class)(
  61. $path, $expiration, $options
  62. );
  63. }
  64. if (! $this->providesTemporaryUrls()) {
  65. throw new RuntimeException('This driver does not support creating temporary URLs.');
  66. }
  67. $url = call_user_func($this->urlGeneratorResolver);
  68. return $url->to($url->temporarySignedRoute(
  69. 'storage.'.$this->disk,
  70. $expiration,
  71. ['path' => strtr(rawurlencode($path), ['%2F' => '/'])],
  72. absolute: false
  73. ));
  74. }
  75. /**
  76. * Get a temporary upload URL for the file at the given path.
  77. *
  78. * @param string $path
  79. * @param \DateTimeInterface $expiration
  80. * @param array $options
  81. * @return array
  82. */
  83. public function temporaryUploadUrl($path, $expiration, array $options = [])
  84. {
  85. if ($this->temporaryUploadUrlCallback) {
  86. return $this->temporaryUploadUrlCallback->bindTo($this, static::class)(
  87. $path, $expiration, $options
  88. );
  89. }
  90. if (! $this->providesTemporaryUploadUrls()) {
  91. throw new RuntimeException('This driver does not support creating temporary upload URLs.');
  92. }
  93. $url = call_user_func($this->urlGeneratorResolver);
  94. return [
  95. 'url' => $url->to($url->temporarySignedRoute(
  96. 'storage.'.$this->disk.'.upload',
  97. $expiration,
  98. ['path' => strtr(rawurlencode($path), ['%2F' => '/']), 'upload' => true],
  99. absolute: false
  100. )),
  101. 'headers' => [],
  102. ];
  103. }
  104. /**
  105. * Specify the name of the disk the adapter is managing.
  106. *
  107. * @param string $disk
  108. * @return $this
  109. */
  110. public function diskName(string $disk)
  111. {
  112. $this->disk = $disk;
  113. return $this;
  114. }
  115. /**
  116. * Indicate that signed URLs should serve the corresponding files.
  117. *
  118. * @param bool $serve
  119. * @param \Closure|null $urlGeneratorResolver
  120. * @return $this
  121. */
  122. public function shouldServeSignedUrls(bool $serve = true, ?Closure $urlGeneratorResolver = null)
  123. {
  124. $this->shouldServeSignedUrls = $serve;
  125. $this->urlGeneratorResolver = $urlGeneratorResolver;
  126. return $this;
  127. }
  128. }