檔案傳檔加入系統限制

20250509
This commit is contained in:
allen.yan 2025-05-09 13:40:46 +08:00
parent 056b1eff73
commit c5c7dc0c14
4 changed files with 73 additions and 0 deletions

View File

@ -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');

View File

@ -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()
{

View File

@ -48,6 +48,9 @@
{{-- 檔案上傳 --}}
<input type="file" wire:model="file" accept=".csv, .xls, .xlsx" class="mb-4 w-full" />
<p class="text-xs text-gray-500 mb-2">
系統限制:最大上傳 {{ $maxUploadSize }}
</p>
<!-- 上傳中提示 -->
<div wire:loading wire:target="file" class="text-sm text-blue-500">
檔案上傳中,請稍候...
@ -58,6 +61,7 @@
<x-wireui:icon name="check-circle" class="w-5 h-5 text-green-500" />
</div>
<x-slot name="footer">
<div class="flex justify-between w-full">
<x-wireui:button flat label="{{ __('artists.cancel') }}" wire:click="closeModal" />

View File

@ -144,6 +144,9 @@
</div>
{{-- 檔案上傳 --}}
<input type="file" wire:model="file" accept=".csv, .xls, .xlsx" class="mb-4 w-full" />
<p class="text-xs text-gray-500 mb-2">
系統限制:最大上傳 {{ $maxUploadSize }}
</p>
<!-- 上傳中提示 -->
<div wire:loading wire:target="file" class="text-sm text-blue-500">
檔案上傳中,請稍候...