Skip to content

Commit

Permalink
Explicitly fail when file does not exist on import
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickbrouwers committed Feb 19, 2024
1 parent 36ca9cb commit 53f9137
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .phpunit.cache/test-results

Large diffs are not rendered by default.

20 changes: 17 additions & 3 deletions src/Files/TemporaryFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Maatwebsite\Excel\Files;

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Symfony\Component\HttpFoundation\File\UploadedFile;

abstract class TemporaryFile
Expand Down Expand Up @@ -45,8 +46,9 @@ public function sync(): TemporaryFile
}

/**
* @param string|UploadedFile $filePath
* @param string|null $disk
* @param string|UploadedFile $filePath
* @param string|null $disk
*
* @return TemporaryFile
*/
public function copyFrom($filePath, string $disk = null): TemporaryFile
Expand All @@ -56,7 +58,19 @@ public function copyFrom($filePath, string $disk = null): TemporaryFile
} elseif ($disk === null && realpath($filePath) !== false) {
$readStream = fopen($filePath, 'rb');
} else {
$readStream = app('filesystem')->disk($disk)->readStream($filePath);
$diskInstance = app('filesystem')->disk($disk);

if (!$diskInstance->exists($filePath)) {
$logPath = '[' . $filePath . ']';

if ($disk) {
$logPath .= ' (' . $disk . ')';
}

throw new FileNotFoundException('File '. $logPath .' does not exist and can therefor not be imported.');
}

$readStream = $diskInstance->readStream($filePath);
}

$this->put($readStream);
Expand Down
13 changes: 13 additions & 0 deletions tests/Concerns/ImportableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Maatwebsite\Excel\Tests\Concerns;

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\ToArray;
use Maatwebsite\Excel\Excel;
Expand Down Expand Up @@ -131,4 +132,16 @@ public function array(array $array)

$this->assertInstanceOf(Importer::class, $imported);
}

public function test_cannot_import_a_non_existing_xlsx_file()
{
$this->expectException(FileNotFoundException::class);

$import = new class
{
use Importable;
};

$import->import('doesnotexistanywhere.xlsx');
}
}

0 comments on commit 53f9137

Please sign in to comment.