UploadedFile.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Illuminate\Http;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Container\Container;
  5. use Illuminate\Support\Traits\Macroable;
  6. use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory;
  7. use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyUploadedFile;
  8. class UploadedFile extends SymfonyUploadedFile
  9. {
  10. use FileHelpers, Macroable;
  11. /**
  12. * Begin creating a new file fake.
  13. *
  14. * @return \Illuminate\Http\Testing\FileFactory
  15. */
  16. public static function fake()
  17. {
  18. return new Testing\FileFactory;
  19. }
  20. /**
  21. * Store the uploaded file on a filesystem disk.
  22. *
  23. * @param string $path
  24. * @param array|string $options
  25. * @return string|false
  26. */
  27. public function store($path, $options = [])
  28. {
  29. return $this->storeAs($path, $this->hashName(), $this->parseOptions($options));
  30. }
  31. /**
  32. * Store the uploaded file on a filesystem disk with public visibility.
  33. *
  34. * @param string $path
  35. * @param array|string $options
  36. * @return string|false
  37. */
  38. public function storePublicly($path, $options = [])
  39. {
  40. $options = $this->parseOptions($options);
  41. $options['visibility'] = 'public';
  42. return $this->storeAs($path, $this->hashName(), $options);
  43. }
  44. /**
  45. * Store the uploaded file on a filesystem disk with public visibility.
  46. *
  47. * @param string $path
  48. * @param string $name
  49. * @param array|string $options
  50. * @return string|false
  51. */
  52. public function storePubliclyAs($path, $name, $options = [])
  53. {
  54. $options = $this->parseOptions($options);
  55. $options['visibility'] = 'public';
  56. return $this->storeAs($path, $name, $options);
  57. }
  58. /**
  59. * Store the uploaded file on a filesystem disk.
  60. *
  61. * @param string $path
  62. * @param string $name
  63. * @param array|string $options
  64. * @return string|false
  65. */
  66. public function storeAs($path, $name, $options = [])
  67. {
  68. $options = $this->parseOptions($options);
  69. $disk = Arr::pull($options, 'disk');
  70. return Container::getInstance()->make(FilesystemFactory::class)->disk($disk)->putFileAs(
  71. $path, $this, $name, $options
  72. );
  73. }
  74. /**
  75. * Create a new file instance from a base instance.
  76. *
  77. * @param \Symfony\Component\HttpFoundation\File\UploadedFile $file
  78. * @param bool $test
  79. * @return static
  80. */
  81. public static function createFromBase(SymfonyUploadedFile $file, $test = false)
  82. {
  83. return $file instanceof static ? $file : new static(
  84. $file->getPathname(),
  85. $file->getClientOriginalName(),
  86. $file->getClientMimeType(),
  87. $file->getClientSize(),
  88. $file->getError(),
  89. $test
  90. );
  91. }
  92. /**
  93. * Parse and format the given options.
  94. *
  95. * @param array|string $options
  96. * @return array
  97. */
  98. protected function parseOptions($options)
  99. {
  100. if (is_string($options)) {
  101. $options = ['disk' => $options];
  102. }
  103. return $options;
  104. }
  105. }