Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Maatwebsite\Excel\Files\TemporaryFile::copyFrom calls removed exists() method instead of fileExists() #4223

Open
1 task done
joe-hart opened this issue Oct 21, 2024 · 0 comments
Labels

Comments

@joe-hart
Copy link

Is the bug applicable and reproducable to the latest version of the package and hasn't it been reported before?

  • Yes, it's still reproducable

What version of Laravel Excel are you using?

3.1.58

What version of Laravel are you using?

10.48.22

What version of PHP are you using?

8.3.12

Describe your issue

Error: Call to undefined method League\Flysystem\Filesystem::exists() in /var/www/vendor/maatwebsite/excel/src/Files/TemporaryFile.php:62
 Stack trace:
 #0 /var/www/vendor/maatwebsite/excel/src/Reader.php(439): Maatwebsite\Excel\Files\TemporaryFile->copyFrom()
 #1 /var/www/vendor/maatwebsite/excel/src/Reader.php(102): Maatwebsite\Excel\Reader->getReader()
 #2 /var/www/vendor/maatwebsite/excel/src/Excel.php(155): Maatwebsite\Excel\Reader->read()
 #3 /var/www/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(355): Maatwebsite\Excel\Excel->import()

When using import TemporaryFile::copyFrom an exception is thrown because line:
if (!$diskInstance->exists($filePath)) {
presumes the existence of a deprecated method that was removed from \League\Flysystem\FilesystemAdapter with v3

This line should call fileExists:
if (!$diskInstance->fileExists($filePath)) {

I am importing a file from Azure Blob, but

How can the issue be reproduced?

create a new laravel project:

laravel new flysystem-repro
cd flysystem-repro

install maatwebsite/excel:
composer require maatwebsite/excel
create an import class:
php artisan make:import UsersImport --model=User
call from a controller:

use Maatwebsite\Excel\Facades\Excel;
use App\Imports\UsersImport;

public function import()
{
    $filePath = storage_path('app/test.xlsx');  // Use a simple XLSX file
    Excel::import(new UsersImport, $filePath, 'azure-blob', \Maatwebsite\Excel\Excel::XLSX);
}

What should be the expected behaviour?

I would expect the package to use fileExists from Flysystem v3, which is a requirement for Laravel 10, as the package claims support for Laravel 10.

I am not sure if you are trying to maintain backwards-compatibility for versions of Laravel 8 that used the original version of flysystem, but if so, I would suggest this change to Maatwebsite\Excel\Files\TemporaryFile::copyFrom:

/**
 * @param  string|UploadedFile  $filePath
 * @param  string|null  $disk
 * @return TemporaryFile
 */
public function copyFrom($filePath, string $disk = null): TemporaryFile
{
    if ($filePath instanceof UploadedFile) {
        $readStream = fopen($filePath->getRealPath(), 'rb');
    } elseif ($disk === null && realpath($filePath) !== false) {
        $readStream = fopen($filePath, 'rb');
    } else {
        $diskInstance = app('filesystem')->disk($disk);

        // Check for Flysystem v3 (fileExists) and fallback to Flysystem v2 (exists)
        if (method_exists($diskInstance, 'fileExists')) {
            if (!$diskInstance->fileExists($filePath)) {
                $logPath = '[' . $filePath . ']';

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

                throw new FileNotFoundException('File ' . $logPath . ' does not exist and cannot be imported.');
            }
        } else {
            // For Flysystem v2
            if (!$diskInstance->exists($filePath)) {
                $logPath = '[' . $filePath . ']';

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

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

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

    $this->put($readStream);

    if (is_resource($readStream)) {
        fclose($readStream);
    }

    return $this->sync();
}

Otherwise, if you are only supporting newer Laravel versions that use Flysystem v3:

/**
 * @param  string|UploadedFile  $filePath
 * @param  string|null  $disk
 * @return TemporaryFile
 */
public function copyFrom($filePath, string $disk = null): TemporaryFile
{
    if ($filePath instanceof UploadedFile) {
        $readStream = fopen($filePath->getRealPath(), 'rb');
    } elseif ($disk === null && realpath($filePath) !== false) {
        $readStream = fopen($filePath, 'rb');
    } else {
        $diskInstance = app('filesystem')->disk($disk);

        // For Flysystem v3: Use fileExists() instead of exists()
        if (!$diskInstance->fileExists($filePath)) {
            $logPath = '[' . $filePath . ']';

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

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

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

    $this->put($readStream);

    if (is_resource($readStream)) {
        fclose($readStream);
    }

    return $this->sync();
}

@joe-hart joe-hart added the bug label Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant