Exemplo n.º 1
0
        public virtual string DownloadImage(MediaItem currentItem, ImageUpload img)
        {
            var client = new RestClient(img.Location);

            client.Authenticator = new HttpBasicAuthenticator("Api", ImageCompressionSettings.GetApiEndpointKey());

            var request = new RestRequest(Method.GET);

            request.AddHeader("Cache-Control", "no-cache");
            request.AddHeader("Accept", "*/*");
            client.Timeout = 300000;

            try
            {
                byte [] responseData = client.DownloadData(request);
                string  sizeBefore   = currentItem.InnerItem.Fields["Size"].Value;
                UpdateImageFile(currentItem, responseData);
                string sizeAfter = currentItem.InnerItem.Fields["Size"].Value;
                UpdateImageInformation(currentItem, sizeBefore, sizeAfter, ImageCompressionConstants.Messages.OPTIMISED_BY, ImageCompressionSettings.GetInformationField());
            }
            catch (Exception ex)
            {
                Diagnostics.Log.Error(TIMY_CONNETION_ERROR, ex);
                RecordError(currentItem, ex.Message);
            }
            return("API ISSUE");
        }
Exemplo n.º 2
0
        public virtual ImageUpload SendToTinyForCompression(Item currentItem)
        {
            var client = new RestClient(ImageCompressionSettings.GetApiEndpoint());

            client.Authenticator = new HttpBasicAuthenticator("Api", ImageCompressionSettings.GetApiEndpointKey());
            var request = CreateUploadRequest(currentItem, client);

            client.Timeout = 300000;

            try
            {
                var response = client.Execute <ImageUpload>(request);
                var content  = response.Content;

                if (response.StatusCode != System.Net.HttpStatusCode.OK && response.StatusCode != System.Net.HttpStatusCode.Created)
                {
                    Sitecore.Diagnostics.Log.Info($"Image Upload failed {response.StatusCode} | response content: {response.Content}", this);
                    ShouldContinue = false;
                    return(null);
                }

                response.Data.Location = GetHeader(response, LOCATON_RESPONSE);

                if (string.IsNullOrEmpty(response.Data.Location))
                {
                    return(null);
                }

                Diagnostics.Log.Info($"Image Uploaded to Tiny PNG {response.Data.Location} | Compression count so far: {GetHeader(response, COMPRESSION_COUNT)}", this);

                return(response.Data);
            }
            catch (Exception ex)
            {
                Diagnostics.Log.Error(REMOTE_ERROR, ex);
                RecordError(currentItem, ex.Message);
            }
            return(null);
        }