From c5c7dc0c147bf8fbe0ea3f2232f7fc4784ff3189 Mon Sep 17 00:00:00 2001 From: "allen.yan" Date: Fri, 9 May 2025 13:40:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AA=94=E6=A1=88=E5=82=B3=E6=AA=94=E5=8A=A0?= =?UTF-8?q?=E5=85=A5=E7=B3=BB=E7=B5=B1=E9=99=90=E5=88=B6=2020250509?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Livewire/Admin/ArtistImportData.php | 33 +++++++++++++++++++ app/Livewire/Admin/SongImportData.php | 33 +++++++++++++++++++ .../admin/artist-import-data.blade.php | 4 +++ .../livewire/admin/song-import-data.blade.php | 3 ++ 4 files changed, 73 insertions(+) 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 }} +

檔案上傳中,請稍候... @@ -58,6 +61,7 @@
+
diff --git a/resources/views/livewire/admin/song-import-data.blade.php b/resources/views/livewire/admin/song-import-data.blade.php index b268851..af399c8 100644 --- a/resources/views/livewire/admin/song-import-data.blade.php +++ b/resources/views/livewire/admin/song-import-data.blade.php @@ -144,6 +144,9 @@
{{-- 檔案上傳 --}} +

+ 系統限制:最大上傳 {{ $maxUploadSize }} +

檔案上傳中,請稍候...