diff --git a/src/Storage/SizeUnit.php b/src/Storage/SizeUnit.php new file mode 100644 index 0000000..8356b8e --- /dev/null +++ b/src/Storage/SizeUnit.php @@ -0,0 +1,65 @@ + self::Bytes, + 'K' => self::KiloBytes, + 'M' => self::MegaBytes, + 'G' => self::GigaBytes, + 'T' => self::TeraBytes, + 'P' => self::PetaBytes, + default => throw new \InvalidArgumentException("Invalid unit: $unit"), + }; + } + + public static function getIdealUnit(float $size): self + { + $unit = self::Bytes; + + while ($size >= 1024 && $unit !== self::PetaBytes) { + $size /= 1024; + $unit = match ($unit) { + self::Bytes => self::KiloBytes, + self::KiloBytes => self::MegaBytes, + self::MegaBytes => self::GigaBytes, + self::GigaBytes => self::TeraBytes, + }; + } + + return $unit; + } + + public static function convert(float $size, SizeUnit $sourceUnit, SizeUnit $targetUnit): float + { + return $size * $sourceUnit->getMultiplier($targetUnit); + } + + public function getMultiplier(SizeUnit $targetUnit): float + { + $units = [ + self::Bytes->value => 1, + self::KiloBytes->value => 1024, + self::MegaBytes->value => 1024 ** 2, + self::GigaBytes->value => 1024 ** 3, + self::TeraBytes->value => 1024 ** 4, + self::PetaBytes->value => 1024 ** 5, + ]; + + return $units[$this->value] / $units[$targetUnit->value]; + } +} \ No newline at end of file diff --git a/src/Storage/StorageHelper.php b/src/Storage/StorageHelper.php new file mode 100644 index 0000000..4028461 --- /dev/null +++ b/src/Storage/StorageHelper.php @@ -0,0 +1,23 @@ +getMultiplier($targetUnit) * $iniSize; + } + + public static function isFileSizeOk(UploadedFile $file): bool + { + return $file->getSize() <= self::getMaxFileSize(); + } +} \ No newline at end of file