Skip to content

Commit

Permalink
add: file size format
Browse files Browse the repository at this point in the history
  • Loading branch information
ousid committed Sep 17, 2022
1 parent b30d665 commit cd0351d
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 19 deletions.
14 changes: 14 additions & 0 deletions config/laravel_csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,18 @@
|
*/
'layout' => 'tailwindcss',

/*
|--------------------------------------------------------------------------
| Max Upload File Size
|--------------------------------------------------------------------------
|
| This package came with file validation for uploaded files,
| and by default the file should not be greater than 20MB. If
| you wish to increase/decrease this value, you may change the
| value below.
| Note that the value is defined by "KB".
|
*/
'file_upload_size' => 20000,
];
8 changes: 5 additions & 3 deletions resources/views/livewire/tailwindcss/csv-importer.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@
</svg>
<div class="flex text-sm text-gray-600">
<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">
<span>Upload a file</span>
<span>{{ __('Upload a file') }}</span>
<input id="file" wire:model="file" name="file" type="file" class="sr-only">
</label>
<p class="pl-1">or drag and drop</p>
<p class="pl-1">{{ __('or drag and drop') }}</p>
</div>
<p class="text-xs text-gray-500">
CSV file up to 50MB
{{ __('CSV file up to :size', [
'size' => $fileSize,
]) }}
</p>
</div>
</div>
Expand Down
13 changes: 13 additions & 0 deletions src/Facades/LaravelCsv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Coderflex\LaravelCsv\Facades;

use Illuminate\Support\Facades\Facade;

class LaravelCsv extends Facade
{
protected static function getFacadeAccessor()
{
return 'laravel-csv';
}
}
12 changes: 8 additions & 4 deletions src/Http/Livewire/CsvImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Coderflex\LaravelCsv\Http\Livewire;

use Coderflex\LaravelCsv\Concerns;
use Coderflex\LaravelCsv\Facades\LaravelCsv;

use function Coderflex\LaravelCsv\csv_view_path;
use Coderflex\LaravelCsv\Jobs\ImportCsv;
use Coderflex\LaravelCsv\Utilities\ChunkIterator;
Expand Down Expand Up @@ -92,9 +94,11 @@ public function toggle()

public function render()
{
return view(
csv_view_path('csv-importer')
);
return view(csv_view_path('csv-importer'), [
'fileSize' => LaravelCsv::formatFileSize(
config('laravel_csv.file_upload_size', 20000)
),
]);
}

protected function validationAttributes()
Expand All @@ -105,7 +109,7 @@ protected function validationAttributes()
protected function rules()
{
return [
'file' => 'required|file|mimes:csv,txt',
'file' => 'required|file|mimes:csv,txt|max:'. config('laravel_csv.file_upload_size', '20000'),
] + $this->requiredColumns;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Http/Livewire/HandleImports.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class HandleImports extends Component
{
/** @var string */
protected $model;
public $model;

/** @var array */
protected $listeners = [
Expand Down
28 changes: 28 additions & 0 deletions src/LaravelCsvManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Coderflex\LaravelCsv;

class LaravelCsvManager
{

/**
* Get the given size and formated it.
*
* @param int $number
* @param int $precision
* @return string
*/
public function formatFileSize(int $size, int $precision = 2): string
{
if ($size <= 0) {
return $size;
}

$base = log((int) $size) / log(1024);
$suffixes = ['KB', 'MB', 'GB', 'TB'];

return round(
pow(1024, $base - floor($base)), $precision
) . $suffixes[floor($base)];
}
}
5 changes: 5 additions & 0 deletions src/LaravelCsvServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function bootingPackage()
$this->registerBladeDirectives();
}

public function registeringPackage()
{
$this->app->bind('laravel-csv', fn() => new LaravelCsvManager);
}

/**
* Configure Laravel CSV Blade components
*
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @param string|null $view
* @return string
*/
function csv_view_path($view)
function csv_view_path($view): string
{
return 'laravel-csv::livewire.'.config('laravel_csv.layout').'.'.$view;
}
Expand Down
5 changes: 0 additions & 5 deletions tests/CsvImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@

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

it('renders import CSV component', function () {
livewire(CsvImporter::class)
->assertSuccessful();
});

it('renders import CSV component with model', function () {
$model = Customer::class;

Expand Down
5 changes: 0 additions & 5 deletions tests/HandleImportsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
use Coderflex\LaravelCsv\Tests\Models\User;
use function Pest\Livewire\livewire;

it('renders handle imports component', function () {
livewire(HandleImports::class)
->assertSuccessful();
});

it('renders handle imports component with model', function () {
$this->actingAs(User::factory()->create());

Expand Down

0 comments on commit cd0351d

Please sign in to comment.