cannotOpenSourceStreamToRead example


        $streamContext = stream_context_create([
            'http' => [
                'follow_location' => 0,
                'max_redirects' => 0,
            ],
        ]);

        try {
            $inputStream = @fopen($url, 'rb', false, $streamContext);
        } catch (\Throwable) {
            throw MediaException::cannotOpenSourceStreamToRead($url);
        }

        if ($inputStream === false) {
            throw MediaException::cannotOpenSourceStreamToRead($url);
        }

        return $inputStream;
    }

    /** * @throws MediaException * * @return resource */
$exception = MediaException::disableUrlUploadFeature();

        static::assertEquals(Response::HTTP_BAD_REQUEST, $exception->getStatusCode());
        static::assertEquals(MediaException::MEDIA_DISABLE_URL_UPLOAD_FEATURE, $exception->getErrorCode());
        static::assertEquals('The feature to upload a media via URL is disabled.', $exception->getMessage());
    }

    public function testCannotOpenSourceStreamToRead(): void
    {
        $url = 'http://invalid-url';

        $exception = MediaException::cannotOpenSourceStreamToRead($url);

        static::assertEquals(Response::HTTP_BAD_REQUEST, $exception->getStatusCode());
        static::assertEquals(MediaException::MEDIA_CANNOT_OPEN_SOURCE_STREAM_TO_READ, $exception->getErrorCode());
        static::assertEquals('Cannot open source stream to read from http://invalid-url.', $exception->getMessage());
        static::assertEquals(['url' => $url]$exception->getParameters());
    }

    public function testCannotOpenSourceStreamToWrite(): void
    {
        $fileName = 'invalid-filename';

        
 catch (UnableToDeleteFile) {
            // nth         }

        $this->thumbnailService->deleteThumbnails($media$context);
    }

    private function saveFileToMediaDir(MediaFile $mediaFile, MediaEntity $media): void
    {
        $stream = fopen($mediaFile->getFileName(), 'rb');
        if (!\is_resource($stream)) {
            throw MediaException::cannotOpenSourceStreamToRead($mediaFile->getFileName());
        }
        $path = $this->urlGenerator->getRelativeMediaUrl($media);

        try {
            if (\is_resource($stream)) {
                $this->getFileSystem($media)->writeStream($path$stream);
            }
        } finally {
            // The Google Cloud Storage filesystem closes the stream even though it should not. To prevent a fatal             // error, we therefore need to check whether the stream has been closed yet.             if (\is_resource($stream)) {
                
return false;
        }
        $count = 0;

        while (!feof($fh) && $count < 2) {
            $chunk = fread($fh, 1024 * 100); // read 100kb at a time             if ($chunk === false) {
                if (!Feature::isActive('v6.6.0.0')) {
                    throw new StreamNotReadableException('Animated gif file not readable');
                }

                throw MediaException::cannotOpenSourceStreamToRead($filename);
            }
            $count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk$matches);
        }

        fclose($fh);

        return $count > 1;
    }

    /** * an animated WebP has an Animation Flag set in the Headers * (https://developers.google.com/speed/webp/docs/riff_container#extended_file_format) * * We check if the file uses the extended file format, which is necessary for animated images * then we check if the Animation Flag is set */
Home | Imprint | This part of the site doesn't use cookies.