diff --git a/src/MediaCollections/FileAdderFactory.php b/src/MediaCollections/FileAdderFactory.php index 58ddc848a..164780c9e 100644 --- a/src/MediaCollections/FileAdderFactory.php +++ b/src/MediaCollections/FileAdderFactory.php @@ -40,10 +40,7 @@ public static function createMultipleFromRequest(Model $subject, array $keys = [ { return collect($keys) ->map(function (string $key) use ($subject) { - $search = ['[', ']', '"', "'"]; - $replace = ['.', '', '', '']; - - $key = str_replace($search, $replace, $key); + $key = trim(basename($key), './'); if (! request()->hasFile($key)) { throw RequestDoesNotHaveFile::create($key); diff --git a/src/Support/File.php b/src/Support/File.php index ee9be6ea3..ff8e93858 100644 --- a/src/Support/File.php +++ b/src/Support/File.php @@ -6,19 +6,13 @@ class File { - public static function getHumanReadableSize(int $sizeInBytes): string + public static function getHumanReadableSize(int|float $sizeInBytes): string { - $units = ['B', 'KB', 'MB', 'GB', 'TB']; + $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; - if ($sizeInBytes == 0) { - return '0 '.$units[1]; - } + $index = min(count($units) - 1, floor(log(abs($sizeInBytes), 1024))); - for ($i = 0; $sizeInBytes > 1024; $i++) { - $sizeInBytes /= 1024; - } - - return round($sizeInBytes, 2).' '.$units[$i]; + return sprintf("%s %s", round(num: abs($sizeInBytes) / (1024 ** $index), precision: 2), $units[$index]); } public static function getMimeType(string $path): string diff --git a/tests/Support/FileTest.php b/tests/Support/FileTest.php index b7c2bf695..6d681bfc2 100644 --- a/tests/Support/FileTest.php +++ b/tests/Support/FileTest.php @@ -7,9 +7,17 @@ expect(File::getHumanReadableSize(100))->toEqual('100 B'); expect(File::getHumanReadableSize(1000))->toEqual('1000 B'); expect(File::getHumanReadableSize(10000))->toEqual('9.77 KB'); + expect(File::getHumanReadableSize(10000))->toEqual('9.77 KB'); + expect(File::getHumanReadableSize(-10000))->toEqual('9.77 KB'); $this->assertEquals('976.56 KB', File::getHumanReadableSize(1_000_000)); $this->assertEquals('9.54 MB', File::getHumanReadableSize(10_000_000)); $this->assertEquals('9.31 GB', File::getHumanReadableSize(10_000_000_000)); + $this->assertEquals('9.09 TB', File::getHumanReadableSize(10_000_000_000_000)); + $this->assertEquals('8.88 PB', File::getHumanReadableSize(10_000_000_000_000_000)); + $this->assertEquals('86.74 EB', File::getHumanReadableSize(100_000_000_000_000_000_000)); + $this->assertEquals('84.7 ZB', File::getHumanReadableSize(100_000_000_000_000_000_000_000)); + $this->assertEquals('82.72 YB', File::getHumanReadableSize(100_000_000_000_000_000_000_000_000)); + $this->assertEquals('82.72 YB', File::getHumanReadableSize(-100_000_000_000_000_000_000_000_000)); }); it('can determine the mime type of a file', function () {