Example #1
0
    /// <summary>
    /// Execute a set of options against a previous uploaded/shrinked file.
    /// </summary>
    /// <param name="response">Response object from a upload/shrink operation.</param>
    /// <param name="width">Width to scale within.</param>
    /// <param name="height">Height to scale within.</param>
    /// <param name="method">Scale method to apply.</param>
    /// <param name="outputFile">Filename to save to.</param>
    /// <param name="store">Amazon S3 credentials and information.</param>
    /// <returns>Stream</returns>
    private Stream executeOption(TinifyResponse response, int?width, int?height, string method = null, string outputFile = null, TinifyOptionsStore store = null)
    {
        var options = new TinifyOptions();

        if (method != null &&
            (width.HasValue ||
             height.HasValue))
        {
            options.resize = new TinifyOptionsResize {
                method = method
            };

            if (width.HasValue)
            {
                options.resize.width = width.Value;
            }

            if (height.HasValue)
            {
                options.resize.height = height.Value;
            }
        }

        if (store != null)
        {
            options.store = store;
        }

        var stream = request(
            "POST",
            response.output.url,
            null,
            options);

        writeStreamToDisk(
            stream,
            outputFile);

        return(stream);
    }
Example #2
0
    /// <summary>
    /// Performs the actual communication towards the Tinify API.
    /// </summary>
    /// <param name="method">HTTP method to perform.</param>
    /// <param name="url">URL to request.</param>
    /// <param name="binaryFile">File to upload.</param>
    /// <param name="options">Options to pass along to the API.</param>
    /// <returns>Stream</returns>
    private Stream request(string method = "POST", string url = null, string binaryFile = null, TinifyOptions options = null)
    {
        if (url == null)
            url = "https://api.tinify.com/shrink";

        var request = WebRequest.Create(url) as HttpWebRequest;

        if (request == null)
            throw new WebException("Could not create webrequest.");

        request.Method = method;
        request.Headers.Add(
            "Authorization",
            "Basic " + this.base64ApiKey);

        if (!string.IsNullOrEmpty(binaryFile)) {
            var requestStream = request.GetRequestStream();
            var bytes = File.ReadAllBytes(binaryFile);
            requestStream.Write(bytes, 0, bytes.Length);
        }

        if (options != null) {
            request.ContentType = "application/json";

            var requestStream = request.GetRequestStream();
            var json = new JavaScriptSerializer().Serialize(options);
            var bytes = Encoding.UTF8.GetBytes(json);

            requestStream.Write(bytes, 0, bytes.Length);
        }

        this.LastHttpStatusCode = 0;
        this.LastHttpStatusDescription = null;

        HttpWebResponse response = null;

        try {
            response = request.GetResponse() as HttpWebResponse;

            if (response == null)
                throw new Exception("Request returned NULL response.");

            this.LastHttpStatusCode = (int) response.StatusCode;
            this.LastHttpStatusDescription = response.StatusDescription;
        }
        catch (WebException ex) {
            var erres = ex.Response as HttpWebResponse;

            if (erres == null)
                throw new Exception("Request returned NULL response.");

            this.LastHttpStatusCode = (int) erres.StatusCode;
            this.LastHttpStatusDescription = erres.StatusDescription;
        }

        return response != null
            ? response.GetResponseStream()
            : null;
    }
Example #3
0
    /// <summary>
    /// Execute a set of options against a previous uploaded/shrinked file.
    /// </summary>
    /// <param name="response">Response object from a upload/shrink operation.</param>
    /// <param name="width">Width to scale within.</param>
    /// <param name="height">Height to scale within.</param>
    /// <param name="method">Scale method to apply.</param>
    /// <param name="outputFile">Filename to save to.</param>
    /// <param name="store">Amazon S3 credentials and information.</param>
    /// <returns>Stream</returns>
    private Stream executeOption(TinifyResponse response, int? width, int? height, string method = null, string outputFile = null, TinifyOptionsStore store = null)
    {
        var options = new TinifyOptions();

        if (method != null &&
            (width.HasValue ||
             height.HasValue)) {
            options.resize = new TinifyOptionsResize {
                method = method
            };

            if (width.HasValue)
                options.resize.width = width.Value;

            if (height.HasValue)
                options.resize.height = height.Value;
        }

        if (store != null)
            options.store = store;

        var stream = request(
            "POST",
            response.output.url,
            null,
            options);

        writeStreamToDisk(
            stream,
            outputFile);

        return stream;
    }
Example #4
0
    /// <summary>
    /// Performs the actual communication towards the Tinify API.
    /// </summary>
    /// <param name="method">HTTP method to perform.</param>
    /// <param name="url">URL to request.</param>
    /// <param name="binaryFile">File to upload.</param>
    /// <param name="options">Options to pass along to the API.</param>
    /// <returns>Stream</returns>
    private Stream request(string method = "POST", string url = null, string binaryFile = null, TinifyOptions options = null)
    {
        if (url == null)
        {
            url = "https://api.tinify.com/shrink";
        }

        var request = WebRequest.Create(url) as HttpWebRequest;

        if (request == null)
        {
            throw new WebException("Could not create webrequest.");
        }

        request.Method = method;
        request.Headers.Add(
            "Authorization",
            "Basic " + this.base64ApiKey);

        if (!string.IsNullOrEmpty(binaryFile))
        {
            var requestStream = request.GetRequestStream();
            var bytes         = File.ReadAllBytes(binaryFile);
            requestStream.Write(bytes, 0, bytes.Length);
        }

        if (options != null)
        {
            request.ContentType = "application/json";

            var requestStream = request.GetRequestStream();
            var json          = new JavaScriptSerializer().Serialize(options);
            var bytes         = Encoding.UTF8.GetBytes(json);

            requestStream.Write(bytes, 0, bytes.Length);
        }

        this.LastHttpStatusCode        = 0;
        this.LastHttpStatusDescription = null;

        HttpWebResponse response = null;

        try {
            response = request.GetResponse() as HttpWebResponse;

            if (response == null)
            {
                throw new Exception("Request returned NULL response.");
            }

            this.LastHttpStatusCode        = (int)response.StatusCode;
            this.LastHttpStatusDescription = response.StatusDescription;
        }
        catch (WebException ex) {
            var erres = ex.Response as HttpWebResponse;

            if (erres == null)
            {
                throw new Exception("Request returned NULL response.");
            }

            this.LastHttpStatusCode        = (int)erres.StatusCode;
            this.LastHttpStatusDescription = erres.StatusDescription;
        }

        return(response != null
                        ? response.GetResponseStream()
                        : null);
    }