<commit_before><?php
/**
 * Imgix plugin for Craft CMS
 *
 * Imgix Service
 *
 * @author    <NAME>
 * @copyright Copyright (c) 2016 <NAME>
 * @link      http://sjelfull.no
 * @package   Imgix
 * @since     1.0.0
 */

namespace Craft;

use Guzzle\Http\Client;
use Imgix\UrlBuilder;

class ImgixService extends BaseApplicationComponent
{
    protected $builder;

    public function init ()
    {
        parent::init();

        $imgixDomain = $this->getSetting('imgixDomain');

        $this->builder = new UrlBuilder($imgixDomain);
    }

    public function onSaveAsset (AssetFileModel $asset)
    {
        craft()->tasks->createTask('Imgix_Purge', 'Purging images', [ 'assetIds' => [ $asset->id ] ]);
    }

    public function onDeleteAsset (AssetFileModel $asset)
    {
        craft()->tasks->createTask('Imgix_PurgeUrl', 'Purging images', [ 'urls' => [ $this->getImgixUrl($asset) ] ]);
    }

    /**
     */
    public function transformImage ($asset = null, $transforms = null, $defaultOptions = [])
    {
        if ( !$asset ) {
            return null;
        }

        $pathsModel = new ImgixModel($asset, $transforms, $defaultOptions);

        return $pathsModel;
    }

    public function shouldUpdate ($element)
    {
        return $element->getElementType() === 'Asset' && $element->kind === 'image' && ($element->extension !== 'svg' && $element->mimeType !== 'image/svg+xml');
    }

    public function purge (AssetFileModel $asset)
    {
        $url = $this->getImgixUrl($asset);

        ImgixPlugin::log(Craft::t('Purging asset #{id}: {url}', [ 'id' => $asset->id, 'url' => $url ]));

        return $this->purgeUrl($url);
    }

    public function purgeUrl ($url = null)
    {
        try {
            $client  = new Client();
            $request = $client->post('https://api.imgix.com/v2/image/purger', [], [
                'url' => $url,
            ], [
                'timeout' => 15,
            ]);
            $request->setAuth($this->getSetting('apiKey'));

            return $request->send();
        }
        catch (\Exception $e) {
            ImgixPlugin::log(Craft::t('Failed to purge {url}', [ 'url' => $url ]), LogLevel::Error);

            return true;
        }
    }

    public function getImgixUrl (AssetFileModel $asset)
    {
        $source       = $asset->source;
        $sourceHandle = $source->handle;
        $domains      = $this->getSetting('imgixDomains');
        $domain       = array_key_exists($sourceHandle, $domains) ? $domains[ $sourceHandle ] : null;

        if ( !$domain ) {
            return null;
        }

        $builder = new UrlBuilder($domain);<commit_msg>Strip query string when purging<commit_after><?php
/**
 * Imgix plugin for Craft CMS
 *
 * Imgix Service
 *
 * @author    <NAME>
 * @copyright Copyright (c) 2016 <NAME>
 * @link      http://sjelfull.no
 * @package   Imgix
 * @since     1.0.0
 */

namespace Craft;

use Guzzle\Http\Client;
use Imgix\UrlBuilder;

class ImgixService extends BaseApplicationComponent
{
    protected $builder;

    public function init ()
    {
        parent::init();

        $imgixDomain = $this->getSetting('imgixDomain');

        $this->builder = new UrlBuilder($imgixDomain);
    }

    public function onSaveAsset (AssetFileModel $asset)
    {
        craft()->tasks->createTask('Imgix_Purge', 'Purging images', [ 'assetIds' => [ $asset->id ] ]);
    }

    public function onDeleteAsset (AssetFileModel $asset)
    {
        craft()->tasks->createTask('Imgix_PurgeUrl', 'Purging images', [ 'urls' => [ $this->getImgixUrl($asset) ] ]);
    }

    /**
     */
    public function transformImage ($asset = null, $transforms = null, $defaultOptions = [])
    {
        if ( !$asset ) {
            return null;
        }

        $pathsModel = new ImgixModel($asset, $transforms, $defaultOptions);

        return $pathsModel;
    }

    public function shouldUpdate ($element)
    {
        return $element->getElementType() === 'Asset' && $element->kind === 'image' && ($element->extension !== 'svg' && $element->mimeType !== 'image/svg+xml');
    }

    public function purge (AssetFileModel $asset)
    {
        $url = $this->getImgixUrl($asset);

        ImgixPlugin::log(Craft::t('Purging asset #{id}: {url}', [ 'id' => $asset->id, 'url' => $url ]));

        return $this->purgeUrl($url);
    }

    public function purgeUrl ($url = null)
    {
        try {
            $client  = new Client();
            $request = $client->post('https://api.imgix.com/v2/image/purger', [], [
                'url' => UrlHelper::stripQueryString($url),
            ], [
                'timeout' => 15,
            ]);
            $request->setAuth($this->getSetting('apiKey'));

            return $request->send();
        }
        catch (\Exception $e) {
            ImgixPlugin::log(Craft::t('Failed to purge {url}', [ 'url' => $url ]), LogLevel::Error);

            return true;
        }
    }

    public function getImgixUrl (AssetFileModel $asset)
    {
        $source       = $asset->source;
        $sourceHandle = $source->handle;
        $domains      = $this->getSetting('imgixDomains');
        $domain       = array_key_exists($sourceHandle, $domains) ? $domains[ $sourceHandle ] : null;

        if ( !$domain ) {
            return null;
        }

        $builder = new UrlBuilder($domain);
{'fraction_non_alphanumeric': 0.16295186364487732, 'fraction_numerical': 0.0037460198539052258, 'mean_word_length': 2.2821143208358943, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 65, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2818911', 'n_tokens_mistral': 1682, 'n_tokens_neox': 1605, 'n_words': 333}