Skip to content

Commit cd0351d

Browse files
committed
add: file size format
1 parent b30d665 commit cd0351d

File tree

10 files changed

+75
-19
lines changed

10 files changed

+75
-19
lines changed

config/laravel_csv.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,18 @@
1414
|
1515
*/
1616
'layout' => 'tailwindcss',
17+
18+
/*
19+
|--------------------------------------------------------------------------
20+
| Max Upload File Size
21+
|--------------------------------------------------------------------------
22+
|
23+
| This package came with file validation for uploaded files,
24+
| and by default the file should not be greater than 20MB. If
25+
| you wish to increase/decrease this value, you may change the
26+
| value below.
27+
| Note that the value is defined by "KB".
28+
|
29+
*/
30+
'file_upload_size' => 20000,
1731
];

resources/views/livewire/tailwindcss/csv-importer.blade.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@
4747
</svg>
4848
<div class="flex text-sm text-gray-600">
4949
<label for="file" class="relative cursor-pointer bg-white rounded-md font-medium text-indigo-600 hover:text-indigo-500 focus-within:outline-none focus-within:ring-2 focus-within:ring-offset-2 focus-within:ring-indigo-500">
50-
<span>Upload a file</span>
50+
<span>{{ __('Upload a file') }}</span>
5151
<input id="file" wire:model="file" name="file" type="file" class="sr-only">
5252
</label>
53-
<p class="pl-1">or drag and drop</p>
53+
<p class="pl-1">{{ __('or drag and drop') }}</p>
5454
</div>
5555
<p class="text-xs text-gray-500">
56-
CSV file up to 50MB
56+
{{ __('CSV file up to :size', [
57+
'size' => $fileSize,
58+
]) }}
5759
</p>
5860
</div>
5961
</div>

src/Facades/LaravelCsv.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Coderflex\LaravelCsv\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class LaravelCsv extends Facade
8+
{
9+
protected static function getFacadeAccessor()
10+
{
11+
return 'laravel-csv';
12+
}
13+
}

src/Http/Livewire/CsvImporter.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Coderflex\LaravelCsv\Http\Livewire;
44

55
use Coderflex\LaravelCsv\Concerns;
6+
use Coderflex\LaravelCsv\Facades\LaravelCsv;
7+
68
use function Coderflex\LaravelCsv\csv_view_path;
79
use Coderflex\LaravelCsv\Jobs\ImportCsv;
810
use Coderflex\LaravelCsv\Utilities\ChunkIterator;
@@ -92,9 +94,11 @@ public function toggle()
9294

9395
public function render()
9496
{
95-
return view(
96-
csv_view_path('csv-importer')
97-
);
97+
return view(csv_view_path('csv-importer'), [
98+
'fileSize' => LaravelCsv::formatFileSize(
99+
config('laravel_csv.file_upload_size', 20000)
100+
),
101+
]);
98102
}
99103

100104
protected function validationAttributes()
@@ -105,7 +109,7 @@ protected function validationAttributes()
105109
protected function rules()
106110
{
107111
return [
108-
'file' => 'required|file|mimes:csv,txt',
112+
'file' => 'required|file|mimes:csv,txt|max:'. config('laravel_csv.file_upload_size', '20000'),
109113
] + $this->requiredColumns;
110114
}
111115

src/Http/Livewire/HandleImports.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class HandleImports extends Component
1010
{
1111
/** @var string */
12-
protected $model;
12+
public $model;
1313

1414
/** @var array */
1515
protected $listeners = [

src/LaravelCsvManager.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Coderflex\LaravelCsv;
4+
5+
class LaravelCsvManager
6+
{
7+
8+
/**
9+
* Get the given size and formated it.
10+
*
11+
* @param int $number
12+
* @param int $precision
13+
* @return string
14+
*/
15+
public function formatFileSize(int $size, int $precision = 2): string
16+
{
17+
if ($size <= 0) {
18+
return $size;
19+
}
20+
21+
$base = log((int) $size) / log(1024);
22+
$suffixes = ['KB', 'MB', 'GB', 'TB'];
23+
24+
return round(
25+
pow(1024, $base - floor($base)), $precision
26+
) . $suffixes[floor($base)];
27+
}
28+
}

src/LaravelCsvServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public function bootingPackage()
3838
$this->registerBladeDirectives();
3939
}
4040

41+
public function registeringPackage()
42+
{
43+
$this->app->bind('laravel-csv', fn() => new LaravelCsvManager);
44+
}
45+
4146
/**
4247
* Configure Laravel CSV Blade components
4348
*

src/helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @param string|null $view
1010
* @return string
1111
*/
12-
function csv_view_path($view)
12+
function csv_view_path($view): string
1313
{
1414
return 'laravel-csv::livewire.'.config('laravel_csv.layout').'.'.$view;
1515
}

tests/CsvImporterTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@
1212

1313
beforeEach(fn () => $this->actingAs(User::factory()->create()));
1414

15-
it('renders import CSV component', function () {
16-
livewire(CsvImporter::class)
17-
->assertSuccessful();
18-
});
19-
2015
it('renders import CSV component with model', function () {
2116
$model = Customer::class;
2217

tests/HandleImportsTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
use Coderflex\LaravelCsv\Tests\Models\User;
55
use function Pest\Livewire\livewire;
66

7-
it('renders handle imports component', function () {
8-
livewire(HandleImports::class)
9-
->assertSuccessful();
10-
});
11-
127
it('renders handle imports component with model', function () {
138
$this->actingAs(User::factory()->create());
149

0 commit comments

Comments
 (0)