Exception
2 years ago
MimeType
2 years ago
File.php
2 years ago
Stream.php
2 years ago
UploadedFile.php
2 years ago
File.php
142 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | * |
| 11 | * Modified by impress-org on 23-January-2024 using Strauss. |
| 12 | * @see https://github.com/BrianHenryIE/strauss |
| 13 | */ |
| 14 | |
| 15 | namespace Give\Vendors\Symfony\Component\HttpFoundation\File; |
| 16 | |
| 17 | use Give\Vendors\Symfony\Component\HttpFoundation\File\Exception\FileException; |
| 18 | use Give\Vendors\Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException; |
| 19 | use Give\Vendors\Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser; |
| 20 | use Give\Vendors\Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser; |
| 21 | |
| 22 | /** |
| 23 | * A file in the file system. |
| 24 | * |
| 25 | * @author Bernhard Schussek <bschussek@gmail.com> |
| 26 | */ |
| 27 | class File extends \SplFileInfo |
| 28 | { |
| 29 | /** |
| 30 | * Constructs a new file from the given path. |
| 31 | * |
| 32 | * @param string $path The path to the file |
| 33 | * @param bool $checkPath Whether to check the path or not |
| 34 | * |
| 35 | * @throws FileNotFoundException If the given path is not a file |
| 36 | */ |
| 37 | public function __construct($path, $checkPath = true) |
| 38 | { |
| 39 | if ($checkPath && !is_file($path)) { |
| 40 | throw new FileNotFoundException($path); |
| 41 | } |
| 42 | |
| 43 | parent::__construct($path); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Returns the extension based on the mime type. |
| 48 | * |
| 49 | * If the mime type is unknown, returns null. |
| 50 | * |
| 51 | * This method uses the mime type as guessed by getMimeType() |
| 52 | * to guess the file extension. |
| 53 | * |
| 54 | * @return string|null The guessed extension or null if it cannot be guessed |
| 55 | * |
| 56 | * @see ExtensionGuesser |
| 57 | * @see getMimeType() |
| 58 | */ |
| 59 | public function guessExtension() |
| 60 | { |
| 61 | $type = $this->getMimeType(); |
| 62 | $guesser = ExtensionGuesser::getInstance(); |
| 63 | |
| 64 | return $guesser->guess($type); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns the mime type of the file. |
| 69 | * |
| 70 | * The mime type is guessed using a MimeTypeGuesser instance, which uses finfo(), |
| 71 | * mime_content_type() and the system binary "file" (in this order), depending on |
| 72 | * which of those are available. |
| 73 | * |
| 74 | * @return string|null The guessed mime type (e.g. "application/pdf") |
| 75 | * |
| 76 | * @see MimeTypeGuesser |
| 77 | */ |
| 78 | public function getMimeType() |
| 79 | { |
| 80 | $guesser = MimeTypeGuesser::getInstance(); |
| 81 | |
| 82 | return $guesser->guess($this->getPathname()); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Moves the file to a new location. |
| 87 | * |
| 88 | * @param string $directory The destination folder |
| 89 | * @param string $name The new file name |
| 90 | * |
| 91 | * @return self A File object representing the new file |
| 92 | * |
| 93 | * @throws FileException if the target file could not be created |
| 94 | */ |
| 95 | public function move($directory, $name = null) |
| 96 | { |
| 97 | $target = $this->getTargetFile($directory, $name); |
| 98 | |
| 99 | set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; }); |
| 100 | $renamed = rename($this->getPathname(), $target); |
| 101 | restore_error_handler(); |
| 102 | if (!$renamed) { |
| 103 | throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s).', $this->getPathname(), $target, strip_tags($error))); |
| 104 | } |
| 105 | |
| 106 | @chmod($target, 0666 & ~umask()); |
| 107 | |
| 108 | return $target; |
| 109 | } |
| 110 | |
| 111 | protected function getTargetFile($directory, $name = null) |
| 112 | { |
| 113 | if (!is_dir($directory)) { |
| 114 | if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) { |
| 115 | throw new FileException(sprintf('Unable to create the "%s" directory.', $directory)); |
| 116 | } |
| 117 | } elseif (!is_writable($directory)) { |
| 118 | throw new FileException(sprintf('Unable to write in the "%s" directory.', $directory)); |
| 119 | } |
| 120 | |
| 121 | $target = rtrim($directory, '/\\').\DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name)); |
| 122 | |
| 123 | return new self($target, false); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Returns locale independent base name of the given path. |
| 128 | * |
| 129 | * @param string $name The new file name |
| 130 | * |
| 131 | * @return string containing |
| 132 | */ |
| 133 | protected function getName($name) |
| 134 | { |
| 135 | $originalName = str_replace('\\', '/', $name); |
| 136 | $pos = strrpos($originalName, '/'); |
| 137 | $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1); |
| 138 | |
| 139 | return $originalName; |
| 140 | } |
| 141 | } |
| 142 |