Controller/Adminhtml/FastlyCdn/ImageOptimization/PushImageSettings.php
public function execute()
    {
        $result = $this->resultJson->create();
        try {
            $activeVersion = $this->getRequest()->getParam('active_version');
            $activateVcl = $this->getRequest()->getParam('activate_flag');
            $imageQualityFlag = $this->getRequest()->getParam('image_quality_flag');
            $imageQuality = $this->image->getQuality();
            $service = $this->api->checkServiceDetails();
            $this->vcl->checkCurrentVersionActive($service->versions, $activeVersion);
            $currActiveVersion = $this->vcl->getCurrentVersion($service->versions);
            $clone = $this->api->cloneVersion($currActiveVersion);
            $checkIfSettingExists = $this->api->hasSnippet($activeVersion, Config::IMAGE_SETTING_NAME);
            $snippet = $this->config->getVclSnippets(Config::IO_VCL_SNIPPET_PATH, 'recv.vcl');

            if (!$checkIfSettingExists) {
                foreach ($snippet as $key => $value) {
                    $snippetData = [
                        'name' => Config::FASTLY_MAGENTO_MODULE . '_image_optimization_' . $key,
                        'type' => $key,
                        'dynamic' => "0",
                        'content' => $value,
                        'priority' => 10
                    ];

                    $this->api->uploadSnippet($clone->number, $snippetData);

                    $id = $service->id . '-' . $clone->number . '-imageopto';
                    # Make sure we set webp to auto default
                    $imageParams = [
                        'data' => [
                            'id'            => $id,
                            'type'          => 'io_settings',
                            'attributes'    => [
                                'webp'      => true
                            ]
                        ]
                    ];

                    # Set image quality to magento default quality if selected
                    if ($imageQualityFlag === 'true') {
                        $imageParams['data']['attributes']['webp_quality'] = $imageQuality;
                        $imageParams['data']['attributes']['jpeg_quality'] = $imageQuality;
                    }

                    $this->api->configureImageOptimizationDefaultConfigOptions(
                        json_encode($imageParams),
                        $clone->number
                    );
                }
            } else {
                // Remove image optimization snippet
                foreach ($snippet as $key => $value) {
                    $name = Config::FASTLY_MAGENTO_MODULE . '_image_optimization_' . $key;
                    if ($this->api->hasSnippet($clone->number, $name) == true) {
                        $this->api->removeSnippet($clone->number, $name);
                    }
                }
            }

            $this->api->validateServiceVersion($clone->number);

            if ($activateVcl === 'true') {
                $this->api->activateVersion($clone->number);
            }

            $this->sendWebhook($checkIfSettingExists, $clone);

            $comment = ['comment' => 'Magento Module pushed the Image Optimization snippet'];
            if ($checkIfSettingExists) {
                $comment = ['comment' => 'Magento Module removed the Image Optimization snippet'];
            }
            $this->api->addComment($clone->number, $comment);

            return $result->setData([
                'status' => true
            ]);
        } catch (\Exception $e) {
            return $result->setData([
                'status'    => false,
                'msg'       => $e->getMessage()
            ]);
        }
    }
public function execute()
    {
        $result = $this->resultJson->create();
        try {
            $activeVersion = $this->getRequest()->getParam('active_version');
            $activateVcl = $this->getRequest()->getParam('activate_flag');
            $imageQualityFlag = $this->getRequest()->getParam('image_quality_flag');
            $imageQuality = $this->image->getQuality();
            $service = $this->api->checkServiceDetails();
            $this->vcl->checkCurrentVersionActive($service->versions, $activeVersion);
            $currActiveVersion = $this->vcl->getCurrentVersion($service->versions);
            $clone = $this->api->cloneVersion($currActiveVersion);
            $checkIfSettingExists = $this->api->hasSnippet($activeVersion, Config::IMAGE_SETTING_NAME);
            $snippet = $this->config->getVclSnippets(Config::IO_VCL_SNIPPET_PATH, 'recv.vcl');

            if (!$checkIfSettingExists) {
                foreach ($snippet as $key => $value) {
                    $snippetData = [
                        'name' => Config::FASTLY_MAGENTO_MODULE . '_image_optimization_' . $key,
                        'type' => $key,
                        'dynamic' => "0",
                        'content' => $value,
                        'priority' => 10
                    ];

                    $this->api->uploadSnippet($clone->number, $snippetData);

                    $id = $service->id . '-' . $clone->number . '-imageopto';
                    # Make sure we set webp to auto default
                    $imageParams = [
                        'data' => [
                            'id'            => $id,
                            'type'          => 'io_settings',
                            'attributes'    => [
                                'webp'      => true
                            ]
                        ]
                    ];

                    # Set image quality to magento default quality if selected
                    if ($imageQualityFlag === 'true') {
                        $imageParams['data']['attributes']['webp_quality'] = $imageQuality;
                        $imageParams['data']['attributes']['jpeg_quality'] = $imageQuality;
                    }

                    $this->api->configureImageOptimizationDefaultConfigOptions(
                        json_encode($imageParams),
                        $clone->number
                    );
                }
            } else {
                // Remove image optimization snippet
                foreach ($snippet as $key => $value) {
                    $name = Config::FASTLY_MAGENTO_MODULE . '_image_optimization_' . $key;
                    if ($this->api->hasSnippet($clone->number, $name) == true) {
                        $this->api->removeSnippet($clone->number, $name);
                    }
                }
            }

            $this->api->validateServiceVersion($clone->number);

            if ($activateVcl === 'true') {
                $this->api->activateVersion($clone->number);
            }

            $this->sendWebhook($checkIfSettingExists, $clone);

            $comment = ['comment' => 'Magento Module pushed the Image Optimization snippet'];
            if ($checkIfSettingExists) {
                $comment = ['comment' => 'Magento Module removed the Image Optimization snippet'];
            }
            $this->api->addComment($clone->number, $comment);

            return $result->setData([
                'status' => true
            ]);
        } catch (\Exception $e) {
            return $result->setData([
                'status'    => false,
                'msg'       => $e->getMessage()
            ]);
        }
    }
Upload Image Optimization settings

@return $this|\Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface