aminyazdanpanah /
PHP-FFmpeg-video-streaming
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * This file is part of the PHP-FFmpeg-video-streaming package. |
||
| 5 | * |
||
| 6 | * (c) Amin Yazdanpanah <[email protected]> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view the LICENSE |
||
| 9 | * file that was distributed with this source code. |
||
| 10 | */ |
||
| 11 | |||
| 12 | |||
| 13 | namespace Streaming; |
||
| 14 | |||
| 15 | |||
| 16 | use Streaming\Exception\RuntimeException; |
||
| 17 | use Symfony\Component\Filesystem\Exception\IOExceptionInterface; |
||
| 18 | use Symfony\Component\Filesystem\Filesystem; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * File constructor. |
||
| 22 | * It is all about files |
||
| 23 | */ |
||
| 24 | |||
| 25 | class File |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @param $dirname |
||
| 29 | * @param int $mode |
||
| 30 | */ |
||
| 31 | public static function makeDir(string $dirname, int $mode = 0777): void |
||
| 32 | { |
||
| 33 | static::filesystem('mkdir', [$dirname, $mode]); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param $dir |
||
| 38 | * @return int|null |
||
| 39 | */ |
||
| 40 | public static function directorySize(string $dir): int |
||
| 41 | { |
||
| 42 | if (is_dir($dir)) { |
||
| 43 | $size = 0; |
||
| 44 | foreach (scandir($dir) as $file) { |
||
| 45 | if (in_array($file, [".", ".."])) continue; |
||
| 46 | $filename = $dir . DIRECTORY_SEPARATOR . $file; |
||
| 47 | $size += is_file($filename) ? filesize($filename) : static::directorySize($filename); |
||
| 48 | } |
||
| 49 | return $size; |
||
| 50 | } |
||
| 51 | |||
| 52 | return 0; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param $path |
||
| 57 | * @param $content |
||
| 58 | * @param bool $force |
||
| 59 | * @return void |
||
| 60 | */ |
||
| 61 | public static function put($path, $content, $force = true): void |
||
| 62 | { |
||
| 63 | if (file_exists($path) && !$force) { |
||
| 64 | throw new RuntimeException("File Already Exists"); |
||
| 65 | } |
||
| 66 | |||
| 67 | if (false === @file_put_contents($path, $content)) { |
||
| 68 | throw new RuntimeException("Unable to save the file"); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param string $prefix |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | public static function tmp($prefix = 'pfvs.file_'): string |
||
| 77 | { |
||
| 78 | for ($i = 0; $i < 10; ++$i) { |
||
| 79 | $tmpFile = static::tmpDirPath() . '/' . basename($prefix) . uniqid(mt_rand()); |
||
| 80 | $handle = @fopen($tmpFile, 'x+'); |
||
| 81 | |||
| 82 | if (false === $handle) { |
||
| 83 | continue; |
||
| 84 | } |
||
| 85 | |||
| 86 | @fclose($handle); |
||
|
0 ignored issues
–
show
|
|||
| 87 | |||
| 88 | return $tmpFile; |
||
| 89 | } |
||
| 90 | |||
| 91 | throw new RuntimeException("A temporary file could not be created."); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public static function tmpDir(): string |
||
| 98 | { |
||
| 99 | static::makeDir($tmp_dir = static::tmpDirPath() . DIRECTORY_SEPARATOR . uniqid() . DIRECTORY_SEPARATOR); |
||
| 100 | return $tmp_dir; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * clear all tmp files |
||
| 105 | */ |
||
| 106 | public static function cleanTmpFiles(): void |
||
| 107 | { |
||
| 108 | static::remove(static::tmpDirPath()); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | private static function tmpDirPath(): string |
||
| 115 | { |
||
| 116 | static::makeDir($tmp_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "php_ffmpeg_video_streaming"); |
||
| 117 | return $tmp_path; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param string $src |
||
| 122 | * @param string $dst |
||
| 123 | */ |
||
| 124 | public static function move(string $src, string $dst): void |
||
| 125 | { |
||
| 126 | static::filesystem('mirror', [$src, $dst]); |
||
| 127 | static::remove($src); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param string $src |
||
| 132 | * @param string $dst |
||
| 133 | * @param bool $force |
||
| 134 | */ |
||
| 135 | public static function copy(string $src, string $dst, bool $force = true): void |
||
| 136 | { |
||
| 137 | static::filesystem('copy', [$src, $dst, $force]); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param $dir |
||
| 142 | */ |
||
| 143 | public static function remove(string $dir): void |
||
| 144 | { |
||
| 145 | static::filesystem('remove', [$dir]); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param string $method |
||
| 150 | * @param array $params |
||
| 151 | * @return mixed |
||
| 152 | */ |
||
| 153 | private static function filesystem(string $method, array $params) |
||
| 154 | { |
||
| 155 | try { |
||
| 156 | return \call_user_func_array([new Filesystem, $method], $params); |
||
| 157 | } catch (IOExceptionInterface $e) { |
||
| 158 | throw new RuntimeException("Failed action" . $e->getPath(), $e->getCode(), $e); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | } |
If you suppress an error, we recommend checking for the error condition explicitly: