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

ErrorException: fclose(): supplied resource is not a valid stream resource #698

Closed
joshforbes opened this issue Aug 3, 2017 · 2 comments

Comments

@joshforbes
Copy link
Contributor

Hello, I am trying to use this package with Google Cloud storage using the flysystem adapter from superbalist: https://github.com/Superbalist/laravel-google-cloud-storage.

When I try to add media $profile->addMedia('test.png')->toMediaCollection(); the media does get persisted but I am left with the following error:

1) Test\Account\Integration\CompanyProfileTest::testNew
ErrorException: fclose(): supplied resource is not a valid stream resource

/Users/josh/code/navjobs/vendor/sentry/sentry/lib/Raven/Breadcrumbs/ErrorHandler.php:36
/Users/josh/code/navjobs/vendor/spatie/laravel-medialibrary/src/Filesystem/DefaultFilesystem.php:61
/Users/josh/code/navjobs/vendor/spatie/laravel-medialibrary/src/Filesystem/DefaultFilesystem.php:30
/Users/josh/code/navjobs/vendor/spatie/laravel-medialibrary/src/FileAdder/FileAdder.php:357
/Users/josh/code/navjobs/vendor/spatie/laravel-medialibrary/src/FileAdder/FileAdder.php:345
/Users/josh/code/navjobs/vendor/spatie/laravel-medialibrary/src/FileAdder/FileAdder.php:275
/Users/josh/code/navjobs/tests/Account/Integration/CompanyProfileTest.php:20

The issue, as far as I can tell, is that at the point that the DefaultFilesystem calls fclose($file) the file is already closed. If I dd($file) right before the fclose() I get Closed resource @2024. Is this unexpected behaviour that superbalist's google cloud adapter would have already closed the file?

@joshforbes
Copy link
Contributor Author

Looking into this a little more I don't think its anything the superbalist adapter is doing - they simply pass execution off to the official google cloud storage php library:

    /**
     * Uploads a file to the Google Cloud Storage service.
     *
     * @param string $path
     * @param string|resource $contents
     * @param Config $config
     *
     * @return array
     */
    protected function upload($path, $contents, Config $config)
    {
        $path = $this->applyPathPrefix($path);

        $options = $this->getOptionsFromConfig($config);
        $options['name'] = $path;

        $object = $this->bucket->upload($contents, $options);

        return $this->normaliseObject($object);
    }

where $this->bucket is part of the offical google library. I mention this because I wonder if you would want to address the possibility of the file already being closed in your package (since doesn't appear to be caused by a simple bug in a flysystem adapter). I don't know if this is the best approach but adding:

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

does solve the issue. I could submit a PR with this change if you would like or if it is not something you want to do I guess I could always override the DefaultFilesystem.

Thanks!

@freekmurze
Copy link
Member

This is fixed in the latest version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants