<commit_before><?php
/**
 * ImboClient
 *
 * Copyright (c) 2011 <NAME> <<EMAIL>>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * * The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * @package ImboClient
 * @subpackage Client
 * @author <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2011, <NAME>
 * @license http://www.opensource.org/licenses/mit-license MIT License
 * @link https://github.com/christeredvartsen/imboclient-php
 */

namespace ImboClient;

use ImboClient\Client\Driver\DriverInterface;
use ImboClient\Client\Driver\Curl as DefaultDriver;
use ImboClient\Image\TransformationChain;

/**
 * Client that interacts with the server part of ImboClient
 *
 * This client includes methods that can be used to easily interact with a ImboClient server. All
 * requests made by the client goes through a driver.
 *
 * @package ImboClient
 * @subpackage Client
 * @author <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2011, <NAME>
 * @license http://www.opensource.org/licenses/mit-license MIT License
 * @link https://github.com/christeredvartsen/imboclient-php
 */
class Client {
    /**
     * The server URL
     *
     * @var string
     */
    private $serverUrl;

    /**
     * Driver used by the client
     *
     * @var ImboClient\Client\Driver\DriverInterface
     */
    private $driver;

    /**
     * Public key used for signed requests
     *
     * @var string
     */
    private $publicKey;

    /**
     * Private key used for signed requests
     *
     * @var string
     */
    private $privateKey;

    /**
     * Class constructor
     *
     * @param string $serverUrl The URL to the ImboClient server, including protocol
     * @param string $publicKey The public key to use
     * @param string $privateKey The private key to use
     * @param ImboClient\Client\Driver\DriverInterface $driver Optional driver to set
     */
    public function __construct($serverUrl, $publicKey, $privateKey, DriverInterface $driver = null) {
        $this->serverUrl  = rtrim($serverUrl, '/');
        $this->publicKey  = $publicKey;
        $this->privateKey = $privateKey;

        if ($driver === null) {
            // @codeCoverageIgnoreStart
            $driver = new DefaultDriver();
        }
        // @codeCoverageIgnoreEnd

        $this->driver = $driver;
    }

    /**
     * Get the complete url for a resource
     *
     * @param string $resourceIdentifier The resource identifier. For instance "<md5>.png" or
     *                                   "<md5>.png/meta"
     * @return string
     */
    private function getResourceUrl($resourceIdentifier) {
        return $this->serverUrl . '/' . $this->publicKey . '/' . $resourceIdentifier;
    }

    /**
     * Generate an MD5 image identifier for a given file
     *
     * @param string $path Path to the local image
     * @return string
     * @throws ImboClient\Client\Exception
     */
    private function getImageIdentifier($path) {
        if (!is_file($path)) {
            throw new Exception('File does not exist: ' . $path);
        }

        // Get file extension
        $info = getimagesize($path);

        if (!$info) {
            throw new Exception('Unknown image format of file: ' . $path);
        }

        $extension = image_type_to_extension($info[2], false);

        // Generate MD5 sum of the file
        return md5_file($path) . '.' . $extension;
    }

    /**
     * Add a new image to the server
     *
     * This method will first PUT the image on the server, and then POST the metadata if the PUT
     * was successful.
     *
     * @param string $path Path to the local image
     * @param array $metadata Metadata to attach to the image
     * @return ImboClient\Client\Response
     */
    public function addImage($path, array $metadata = null) {
        $imageIdentifier = $this->getImageIdentifier($path);

        $url = $this->getSignedResourceUrl(DriverInterface::PUT, $imageIdentifier);

        // Add the image, and then POST metadata
        $response = $this->driver->put($url, $path);

        if ($response->isSuccess()) {
            // Add metadata
            if ($metadata === null) {
                $metadata = array();
            }

            $metadata['filename'] = basename($path);

            $response = $this->editMetadata($imageIdentifier, $metadata);
        }

        return $response;
    }

    /**
     * Checks if a given image exists on the server already
     *
     * @param string $path Path to the local image
     * @return boolean
     */
    public function imageExists($path) {
        $imageIdentifier = $this->getImageIdentifier($path);

        $url = $this->getSignedResourceUrl(DriverInterface::HEAD, $imageIdentifier);

        $response = $this->driver->head($url);

        return $response->getStatusCode() == 200;
    }

    /**
     * Delete an image from the server
     *
     * @param string $imageIdentifier The image identifier
     * @return ImboClient\Client\Response
     */
    public function deleteImage($imageIdentifier) {
        $url = $this->getSignedResourceUrl(DriverInterface::DELETE, $imageIdentifier);

        return $this->driver->delete($url);
    }

    /**
     * Edit an image
     *
     * @param string $imageIdentifier The image identifier
     * @param array $metadata An array of metadata
     * @return ImboClient\Client\Response
     */<commit_msg>No need for a signed url together with HEAD<commit_after><?php
/**
 * ImboClient
 *
 * Copyright (c) 2011 <NAME> <<EMAIL>>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * * The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * @package ImboClient
 * @subpackage Client
 * @author <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2011, <NAME>
 * @license http://www.opensource.org/licenses/mit-license MIT License
 * @link https://github.com/christeredvartsen/imboclient-php
 */

namespace ImboClient;

use ImboClient\Client\Driver\DriverInterface;
use ImboClient\Client\Driver\Curl as DefaultDriver;
use ImboClient\Image\TransformationChain;

/**
 * Client that interacts with the server part of ImboClient
 *
 * This client includes methods that can be used to easily interact with a ImboClient server. All
 * requests made by the client goes through a driver.
 *
 * @package ImboClient
 * @subpackage Client
 * @author <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2011, <NAME>
 * @license http://www.opensource.org/licenses/mit-license MIT License
 * @link https://github.com/christeredvartsen/imboclient-php
 */
class Client {
    /**
     * The server URL
     *
     * @var string
     */
    private $serverUrl;

    /**
     * Driver used by the client
     *
     * @var ImboClient\Client\Driver\DriverInterface
     */
    private $driver;

    /**
     * Public key used for signed requests
     *
     * @var string
     */
    private $publicKey;

    /**
     * Private key used for signed requests
     *
     * @var string
     */
    private $privateKey;

    /**
     * Class constructor
     *
     * @param string $serverUrl The URL to the ImboClient server, including protocol
     * @param string $publicKey The public key to use
     * @param string $privateKey The private key to use
     * @param ImboClient\Client\Driver\DriverInterface $driver Optional driver to set
     */
    public function __construct($serverUrl, $publicKey, $privateKey, DriverInterface $driver = null) {
        $this->serverUrl  = rtrim($serverUrl, '/');
        $this->publicKey  = $publicKey;
        $this->privateKey = $privateKey;

        if ($driver === null) {
            // @codeCoverageIgnoreStart
            $driver = new DefaultDriver();
        }
        // @codeCoverageIgnoreEnd

        $this->driver = $driver;
    }

    /**
     * Get the complete url for a resource
     *
     * @param string $resourceIdentifier The resource identifier. For instance "<md5>.png" or
     *                                   "<md5>.png/meta"
     * @return string
     */
    private function getResourceUrl($resourceIdentifier) {
        return $this->serverUrl . '/' . $this->publicKey . '/' . $resourceIdentifier;
    }

    /**
     * Generate an MD5 image identifier for a given file
     *
     * @param string $path Path to the local image
     * @return string
     * @throws ImboClient\Client\Exception
     */
    private function getImageIdentifier($path) {
        if (!is_file($path)) {
            throw new Exception('File does not exist: ' . $path);
        }

        // Get file extension
        $info = getimagesize($path);

        if (!$info) {
            throw new Exception('Unknown image format of file: ' . $path);
        }

        $extension = image_type_to_extension($info[2], false);

        // Generate MD5 sum of the file
        return md5_file($path) . '.' . $extension;
    }

    /**
     * Add a new image to the server
     *
     * This method will first PUT the image on the server, and then POST the metadata if the PUT
     * was successful.
     *
     * @param string $path Path to the local image
     * @param array $metadata Metadata to attach to the image
     * @return ImboClient\Client\Response
     */
    public function addImage($path, array $metadata = null) {
        $imageIdentifier = $this->getImageIdentifier($path);

        $url = $this->getSignedResourceUrl(DriverInterface::PUT, $imageIdentifier);

        // Add the image, and then POST metadata
        $response = $this->driver->put($url, $path);

        if ($response->isSuccess()) {
            // Add metadata
            if ($metadata === null) {
                $metadata = array();
            }

            $metadata['filename'] = basename($path);

            $response = $this->editMetadata($imageIdentifier, $metadata);
        }

        return $response;
    }

    /**
     * Checks if a given image exists on the server already
     *
     * @param string $path Path to the local image
     * @return boolean
     */
    public function imageExists($path) {
        $imageIdentifier = $this->getImageIdentifier($path);

        $url = $this->getResourceUrl($imageIdentifier);

        $response = $this->driver->head($url);

        return $response->getStatusCode() == 200;
    }

    /**
     * Delete an image from the server
     *
     * @param string $imageIdentifier The image identifier
     * @return ImboClient\Client\Response
     */
    public function deleteImage($imageIdentifier) {
        $url = $this->getSignedResourceUrl(DriverInterface::DELETE, $imageIdentifier);

        return $this->driver->delete($url);
    }

    /**
     * Edit an image
     *
     * @param string $imageIdentifier The image identifier
     * @param array $metadata An array of metadata
     * @return ImboClient\Client\Response
     */
{'fraction_non_alphanumeric': 0.10400755845996379, 'fraction_numerical': 0.003306826234154791, 'mean_word_length': 2.563973063973064, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 69, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4901817', 'n_tokens_mistral': 3502, 'n_tokens_neox': 3372, 'n_words': 1187}