diff --git a/app/Livewire/Admin/ArtistImportData.php b/app/Livewire/Admin/ArtistImportData.php index c76e0d1..ce267fd 100644 --- a/app/Livewire/Admin/ArtistImportData.php +++ b/app/Livewire/Admin/ArtistImportData.php @@ -22,12 +22,14 @@ class ArtistImportData extends Component public bool $showModal = false; public $file; + public string $maxUploadSize; public string|null $tmpPath = null; public function mount() { $this->canCreate = Auth::user()?->can('song-edit') ?? false; + $this->maxUploadSize = $this->getMaxUploadSize(); } public function openModal() @@ -74,6 +76,37 @@ class ArtistImportData extends Component } } + private function getMaxUploadSize(): string + { + $uploadMax = $this->convertPHPSizeToBytes(ini_get('upload_max_filesize')); + $postMax = $this->convertPHPSizeToBytes(ini_get('post_max_size')); + $max = min($uploadMax, $postMax); + return $this->humanFileSize($max); + } + + private function convertPHPSizeToBytes(string $s): int + { + $s = trim($s); + $unit = strtolower($s[strlen($s) - 1]); + $bytes = (int) $s; + switch ($unit) { + case 'g': + $bytes *= 1024; + case 'm': + $bytes *= 1024; + case 'k': + $bytes *= 1024; + } + return $bytes; + } + + private function humanFileSize(int $bytes, int $decimals = 2): string + { + $sizes = ['B', 'KB', 'MB', 'GB']; + $factor = floor((strlen((string) $bytes) - 1) / 3); + return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . $sizes[$factor]; + } + public function render() { return view('livewire.admin.artist-import-data'); diff --git a/app/Livewire/Admin/SongImportData.php b/app/Livewire/Admin/SongImportData.php index 7960832..a823184 100644 --- a/app/Livewire/Admin/SongImportData.php +++ b/app/Livewire/Admin/SongImportData.php @@ -22,12 +22,14 @@ class SongImportData extends Component public bool $showModal = false; public $file; + public string $maxUploadSize; public string|null $tmpPath = null; public function mount() { $this->canCreate = Auth::user()?->can('song-edit') ?? false; + $this->maxUploadSize = $this->getMaxUploadSize(); } public function openModal() @@ -73,6 +75,37 @@ class SongImportData extends Component File::delete($this->tmpPath); } } + + private function getMaxUploadSize(): string + { + $uploadMax = $this->convertPHPSizeToBytes(ini_get('upload_max_filesize')); + $postMax = $this->convertPHPSizeToBytes(ini_get('post_max_size')); + $max = min($uploadMax, $postMax); + return $this->humanFileSize($max); + } + + private function convertPHPSizeToBytes(string $s): int + { + $s = trim($s); + $unit = strtolower($s[strlen($s) - 1]); + $bytes = (int) $s; + switch ($unit) { + case 'g': + $bytes *= 1024; + case 'm': + $bytes *= 1024; + case 'k': + $bytes *= 1024; + } + return $bytes; + } + + private function humanFileSize(int $bytes, int $decimals = 2): string + { + $sizes = ['B', 'KB', 'MB', 'GB']; + $factor = floor((strlen((string) $bytes) - 1) / 3); + return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . $sizes[$factor]; + } public function render() { diff --git a/resources/views/livewire/admin/artist-import-data.blade.php b/resources/views/livewire/admin/artist-import-data.blade.php index a138377..3b0707c 100644 --- a/resources/views/livewire/admin/artist-import-data.blade.php +++ b/resources/views/livewire/admin/artist-import-data.blade.php @@ -48,6 +48,9 @@ {{-- 檔案上傳 --}} +
+ 系統限制:最大上傳 {{ $maxUploadSize }} +
+ 系統限制:最大上傳 {{ $maxUploadSize }} +