PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/livewire/livewire/src/TemporaryUploadedFile.php

https://gitlab.com/jjpa2018/dashboard
PHP | 197 lines | 154 code | 42 blank | 1 comment | 12 complexity | 1e70ea05a816a03fcb31e905ed5b3934 MD5 | raw file
  1. <?php
  2. namespace Livewire;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Http\UploadedFile;
  5. use Illuminate\Support\Facades\URL;
  6. use Illuminate\Support\Facades\Storage;
  7. class TemporaryUploadedFile extends UploadedFile
  8. {
  9. protected $storage;
  10. protected $path;
  11. public function __construct($path, $disk)
  12. {
  13. $this->disk = $disk;
  14. $this->storage = Storage::disk($this->disk);
  15. $this->path = FileUploadConfiguration::path($path, false);
  16. $tmpFile = tmpfile();
  17. parent::__construct(stream_get_meta_data($tmpFile)['uri'], $this->path);
  18. }
  19. public function getPath()
  20. {
  21. return $this->storage->path(FileUploadConfiguration::directory());
  22. }
  23. public function isValid()
  24. {
  25. return true;
  26. }
  27. public function getSize()
  28. {
  29. if (app()->runningUnitTests() && str($this->getfilename())->contains('-size=')) {
  30. return (int) str($this->getFilename())->between('-size=', '.')->__toString();
  31. }
  32. return (int) $this->storage->size($this->path);
  33. }
  34. public function getMimeType()
  35. {
  36. return $this->storage->mimeType($this->path);
  37. }
  38. public function getFilename()
  39. {
  40. return $this->getName($this->path);
  41. }
  42. public function getRealPath()
  43. {
  44. return $this->storage->path($this->path);
  45. }
  46. public function getClientOriginalName()
  47. {
  48. return $this->extractOriginalNameFromFilePath($this->path);
  49. }
  50. public function temporaryUrl()
  51. {
  52. if ((FileUploadConfiguration::isUsingS3() or FileUploadConfiguration::isUsingGCS()) && ! app()->runningUnitTests()) {
  53. return $this->storage->temporaryUrl(
  54. $this->path,
  55. now()->addDay(),
  56. ['ResponseContentDisposition' => 'filename="' . $this->getClientOriginalName() . '"']
  57. );
  58. }
  59. if (method_exists($this->storage->getAdapter(), 'getTemporaryUrl') || ! $this->isPreviewable()) {
  60. // This will throw an error because it's not used with S3.
  61. return $this->storage->temporaryUrl($this->path, now()->addDay());
  62. }
  63. return URL::temporarySignedRoute(
  64. 'livewire.preview-file', now()->addMinutes(30), ['filename' => $this->getFilename()]
  65. );
  66. }
  67. public function isPreviewable()
  68. {
  69. $supportedPreviewTypes = config('livewire.temporary_file_upload.preview_mimes', [
  70. 'png', 'gif', 'bmp', 'svg', 'wav', 'mp4',
  71. 'mov', 'avi', 'wmv', 'mp3', 'm4a',
  72. 'jpg', 'jpeg', 'mpga', 'webp', 'wma',
  73. ]);
  74. return in_array($this->guessExtension(), $supportedPreviewTypes);
  75. }
  76. public function readStream()
  77. {
  78. return $this->storage->readStream($this->path);
  79. }
  80. public function exists()
  81. {
  82. return $this->storage->exists($this->path);
  83. }
  84. public function get()
  85. {
  86. return $this->storage->get($this->path);
  87. }
  88. public function delete()
  89. {
  90. return $this->storage->delete($this->path);
  91. }
  92. public function storeAs($path, $name, $options = [])
  93. {
  94. $options = $this->parseOptions($options);
  95. $disk = Arr::pull($options, 'disk') ?: $this->disk;
  96. $newPath = trim($path.'/'.$name, '/');
  97. Storage::disk($disk)->put(
  98. $newPath, $this->storage->readStream($this->path), $options
  99. );
  100. return $newPath;
  101. }
  102. public static function generateHashNameWithOriginalNameEmbedded($file)
  103. {
  104. $hash = str()->random(30);
  105. $meta = str('-meta'.base64_encode($file->getClientOriginalName()).'-')->replace('/', '_');
  106. $extension = '.'.$file->guessExtension();
  107. return $hash.$meta.$extension;
  108. }
  109. public function extractOriginalNameFromFilePath($path)
  110. {
  111. return base64_decode(head(explode('-', last(explode('-meta', str($path)->replace('_', '/'))))));
  112. }
  113. public static function createFromLivewire($filePath)
  114. {
  115. return new static($filePath, FileUploadConfiguration::disk());
  116. }
  117. public static function canUnserialize($subject)
  118. {
  119. if (is_string($subject)) {
  120. return (string) str($subject)->startsWith(['livewire-file:', 'livewire-files:']);
  121. }
  122. if (is_array($subject)) {
  123. return collect($subject)->contains(function ($value) {
  124. return static::canUnserialize($value);
  125. });
  126. }
  127. return false;
  128. }
  129. public static function unserializeFromLivewireRequest($subject)
  130. {
  131. if (is_string($subject)) {
  132. if (str($subject)->startsWith('livewire-file:')) {
  133. return static::createFromLivewire(str($subject)->after('livewire-file:'));
  134. }
  135. if (str($subject)->startsWith('livewire-files:')) {
  136. $paths = json_decode(str($subject)->after('livewire-files:'), true);
  137. return collect($paths)->map(function ($path) { return static::createFromLivewire($path); })->toArray();
  138. }
  139. }
  140. if (is_array($subject)) {
  141. foreach ($subject as $key => $value) {
  142. $subject[$key] = static::unserializeFromLivewireRequest($value);
  143. }
  144. }
  145. return $subject;
  146. }
  147. public function serializeForLivewireResponse()
  148. {
  149. return 'livewire-file:'.$this->getFilename();
  150. }
  151. public static function serializeMultipleForLivewireResponse($files)
  152. {
  153. return 'livewire-files:'.json_encode(collect($files)->map->getFilename());
  154. }
  155. }