jacobcyl / Aliyun-oss-storage

阿里云OSS laravel storage Filesystem adapter, 打造Laravel最好的OSS Storage扩展.
http://jacobcyl.github.io/Aliyun-oss-storage/
MIT License
522 stars 161 forks source link

rewind() expects parameter 1 to be resource, string given #16

Open jyj1993126 opened 7 years ago

jyj1993126 commented 7 years ago

AliOssAdapter : 428

    /**
     * {@inheritdoc}
     */
    public function readStream($path)
    {
        $result = $this->readObject($path);
        $result['stream'] = $result['raw_contents'];
        rewind($result['stream']);
        // Ensure the EntityBody object destruction doesn't close the stream
        $result['raw_contents']->detachStream();
        unset($result['raw_contents']);

        return $result;
    }

其中 raw_contents 是 string

jacobcyl commented 7 years ago

你是如何出现这个错误的,请提供一下重现方法或者你是使用哪个接口导致这个错误的,我这边测试暂时没有报这个错误

sherryt400 commented 6 years ago

hi @jacobcyl i am having same issue while using with https://packagist.org/packages/league/glide-laravel i hope you can resolve it soon thanks

sherryt400 commented 6 years ago

This is my code

    public function render($file, $path, $width = 350, $height = 350) {
        if ($file == null) {
            return null;
        }

        $server = \League\Glide\ServerFactory::create([
            'cache' => Storage::disk('oss')
                              ->getDriver(),
            'cache_path_prefix' => $path . '/cache/',
            'source' => Storage::disk('oss')
                               ->getDriver(),
            'source_path_prefix' => $path,
            'response' => new SymfonyResponseFactory(),
            'defaults' => [
                'q' => 80,
            ]
        ]);

        // Set API
        $api = new \League\Glide\Api\Api(new \Intervention\Image\ImageManager([
            'driver' => self::DRIVER,
        ]), [
            //                new \League\Glide\Manipulators\Orientation(),
            new \League\Glide\Manipulators\Crop(),
            new \League\Glide\Manipulators\Size(2000 * 2000),
            new \League\Glide\Manipulators\Brightness(),
            new \League\Glide\Manipulators\Contrast(),
            new \League\Glide\Manipulators\Gamma(),
            new \League\Glide\Manipulators\Sharpen(),
            new \League\Glide\Manipulators\Filter(),
            new \League\Glide\Manipulators\Blur(),
            new \League\Glide\Manipulators\Pixelate(),
            new \League\Glide\Manipulators\Background(),
            new \League\Glide\Manipulators\Border(),
            new \League\Glide\Manipulators\Encode(),
        ]);

        $server->setApi($api);

        try {
            return $server->getImageResponse($file, ["w" => $width, "h" => $height]);

        } catch (\Exception $e) {
            LogManager::e($e);
        }

        return null;

    }

and this is an error i am getting

ErrorException: rewind() expects parameter 1 to be resource, string given in ...\vendor\jacobcyl\ali-oss-storage\src\AliOssAdapter.php:453
fengliang2011 commented 6 years ago

@jacobcyl I composer your jacobcyl/ali-oss-storage 2.1 package, call the following mehtod in laravel 5.5. return Storage::download( $export->file_path, $export->start_day->format('Ymd').'-'.$export->end_day->format('Ymd').'.xlsx' );

I dig into the code in jacobcyl/ali-oss-storage/src/AliOssAdapter.php, found the "$result['raw_contents']" is a string, not resource.

public function readStream($path) { $result = $this->readObject($path); $result['stream'] = $result['raw_contents']; rewind($result['stream']); // Ensure the EntityBody object destruction doesn't close the stream $result['raw_contents']->detachStream(); unset($result['raw_contents']);

    return $result;
}
dmitryuk commented 6 years ago

This trouble too, please accept pull request https://github.com/jacobcyl/Aliyun-oss-storage/pull/50

fengliang2011 commented 6 years ago

hi, xakzona. how did you fix this bug? I fix the Jacobcyl\AliOSS\AliOssAdapter::readStream() as below public function readStream($path) { $result = $this->readObject($path); $result['stream'] = fopen($this->getUrl($path), 'r'); // $result['stream'] = $result['raw_contents']; // rewind($result['stream']); // Ensure the EntityBody object destruction doesn't close the stream // $result['raw_contents']->detachStream(); unset($result['raw_contents']);

    return $result;
}

@xakzona

dmitryuk commented 6 years ago

This pull request is not mine, you can read solution here but it in merging state

fengliang2011 commented 6 years ago

the solution still has a bug on line 455, php resource is not object, so it has no member mehtod detachStream(). @xakzona

dmitryuk commented 6 years ago

Have you seen my own merge request about this problem? https://github.com/jacobcyl/Aliyun-oss-storage/pull/54

dmitryuk commented 6 years ago

I have checked it by condition is_resource($result['raw_contents']) before rewind