Kevin-Bronsdijk / kraken-net

Official Kraken.io .Net client
https://kraken.io
GNU General Public License v2.0
19 stars 5 forks source link

Possible Issue with uploading stream #1

Closed mvanhalen closed 8 years ago

mvanhalen commented 8 years ago

Hi,

great package. I have some image url functions working. But this snippet I cannot get to work. ` public string ResizeImage(Stream input,int width,int height,string filename,string folder) {

        string result = "";

            var connection = Connection.Create(ApiKey, ApiSecret);

            Client client = new Client(connection);
            var dataStore = new  Kraken.Model.Azure.DataStore(AzureAccount, AzureKey, AzureContainer, folder + "/" + filename);
            dataStore.AddHeaders("Cache-Control", "max-age=2222");

            using (MemoryStream ms = new MemoryStream())
            {
                input.Position = 0;
                input.CopyTo(ms);
                ms.Position = 0;

              var response =  client.OptimizeWait(ms.ToArray(),filename, new Kraken.Model.Azure.OptimizeUploadWaitRequest(dataStore) {
                    ResizeImage = new Kraken.Model.ResizeImage
                    {
                        Width = width,
                        Height = height,
                        Strategy= Kraken.Model.Strategy.Auto
                     }
                });

                if (response.Result.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    //response.Result.Body.KrakedUrl
                    result = AzureURL +"/"+ AzureContainer +"/"+ folder + "/" + filename;
                }
            }

        return result;

    }`

The stream contains data, any idea what I´m doing wrong?

Kevin-Bronsdijk commented 8 years ago

Hello!

I wrote a test which I've included below and have to say that it's working. The output can be found here: https://seamist.blob.core.windows.net/test/mvanhalen/test.png.

Are you sure that the stream contains valid data?

Regards, Kevin

        [TestMethod]
        public void GitHub_MvanHalenStreamIssue1_IsTrue()
        {
            var image = File.ReadAllBytes(TestData.LocalTestImage);
            MemoryStream stream = new MemoryStream(image);

            // Use a stream
            var result = ResizeImage(stream, 100, 100, "test.png", "mvanhalen");

            Assert.IsTrue(result.EndsWith("mvanhalen/test.png"));
        }

        public string ResizeImage(Stream input, int width, int height, string filename, string folder)
        {
            // Create a connection and client
            var client = HelperFunctions.CreateWorkingClient();

            string result = "";

            var dataStore = new DataStore(
               Settings.AzureAccount,
               Settings.AzureKey,
               Settings.AzureContainer, folder + "/" + filename);
            dataStore.AddHeaders("Cache-Control", "max-age=2222");

            using (MemoryStream ms = new MemoryStream())
            {
                input.Position = 0;
                input.CopyTo(ms);
                ms.Position = 0;

                var response = client.OptimizeWait(ms.ToArray(), filename, new OptimizeUploadWaitRequest(dataStore)
                {
                    ResizeImage = new ResizeImage
                    {
                        Width = width,
                        Height = height,
                        Strategy = Strategy.Auto
                    }
                });

                if (response.Result.StatusCode == HttpStatusCode.OK)
                {
                    result = response.Result.Body.KrakedUrl;
                   // result = AzureURL + "/" + AzureContainer + "/" + folder + "/" + filename;
                }
            }

            return result;
        }
mvanhalen commented 8 years ago

Thanks, Yeah, stream data looks fine, but I´ll test again with some other files. It feels like some kind of threading issue somehow like the process keeps waiting for something. I´ll let you know.

Kevin-Bronsdijk commented 8 years ago

Sounds interesting. What’s the execution context of your application? A IIS application pool, a service or something else?

mvanhalen commented 8 years ago

Sorry for the late reply. Many GitHub messages end up in my Spam folder.

It´s an ASP.Net MVC website I get the execution issue (like it never stops) when accessing the response object or something. In the end it will run as an Azure cloud service.

When running a test like you it works fine. So the execution context might be related to the issue. Not sure. It seems like the job is executed on Kraken side but accessing the response data leads to an issue. Weird right?

mvanhalen commented 8 years ago

I just tested it with not accessing the response object and the code runs fine. It´s executed correctly and the file is added to the Azure Storage Blob. Does that make any sense to You?

Kevin-Bronsdijk commented 8 years ago

No problem and thanks for providing the details. Yes, it does make sense; The response retrieval process is causing a deadlock.

      using (var responseMessage = await _client.PostAsync(_krakenApiUrl + apiRequest.Uri, content, cancellationToken)

Is missing ConfigureAwait(false)

Bit sloppy from my side and therefore including a hosted test within the repository asap.

To solve your issue, I will create a new release within a couple of minutes.

Kevin-Bronsdijk commented 8 years ago

New client available -> https://www.nuget.org/packages/kraken-net/1.1.1 or Install-Package kraken-net

mvanhalen commented 8 years ago

Thanks a lot Kevin. Your prompt response is gold. I´ll test the new package and let you know.

mvanhalen commented 8 years ago

The new update fixes the bug. Great work and thanks!