Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refinement & Fix bugs #3540

Merged
merged 5 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/MediaCollections/FileAdderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 4 additions & 10 deletions src/Support/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/Support/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down