Revision 283809f3 trunk/Pithos.Network/CloudFilesClient.cs

b/trunk/Pithos.Network/CloudFilesClient.cs
372 372
        /// </summary>
373 373
        /// <param name="container"></param>
374 374
        /// <param name="objectName"></param>
375
        /// <param name="fileName"></param>
375 376
        /// <returns></returns>
376 377
        /// <remarks>>This method should have no timeout or a very long one</remarks>
377
        public Stream GetObject(string container, string objectName)
378
        public Task GetObject(string container, string objectName, string fileName)
378 379
        {
379 380
            if (String.IsNullOrWhiteSpace(container))
380 381
                throw new ArgumentNullException("container", "The container property can't be empty");
381 382
            if (String.IsNullOrWhiteSpace(objectName))
382 383
                throw new ArgumentNullException("objectName", "The objectName property can't be empty");
383 384

  
384
            var request = new RestRequest { Path = container + "/" + objectName, Method = WebMethod.Get };
385
/*
386
            if (DownloadPercentLimit > 0)
387
                request.TaskOptions = new TaskOptions<int> { RateLimitPercent = DownloadPercentLimit };
388
*/
389
            
390
            var response = _client.Request(request);
391
            
392
            if (response.StatusCode == HttpStatusCode.NotFound)
393
                throw new FileNotFoundException();
394
            if (response.StatusCode == HttpStatusCode.OK)
385
            var request = new RestRequest {Path = container + "/" + objectName, Method = WebMethod.Get};
386
            /*
387
                        if (DownloadPercentLimit > 0)
388
                            request.TaskOptions = new TaskOptions<int> { RateLimitPercent = DownloadPercentLimit };
389
            */
390
            try
391
            {
392
                var url = String.Join("/", new[] {_client.Authority, container, objectName});
393
                var uri = new Uri(url);
394

  
395
                var client = new WebClient();
396
                if (!String.IsNullOrWhiteSpace(_client.Proxy))
397
                    client.Proxy = new WebProxy(_client.Proxy);
398

  
399
                CopyHeaders(_client, client);
400

  
401
                Trace.TraceInformation("[GET] START {0}", objectName);
402
                client.DownloadProgressChanged += (sender, args) => 
403
                    Trace.TraceInformation("[GET PROGRESS] {0} {1}% {2} of {3}",
404
                                    fileName, args.ProgressPercentage,
405
                                    args.BytesReceived,
406
                                    args.TotalBytesToReceive);
407
                
408
                return client.DownloadFileTask(uri, fileName)
409
                    .ContinueWith(download =>
410
                                      {
411
                                          client.Dispose();
412

  
413
                                          if (download.IsFaulted)
414
                                          {
415
                                              Trace.TraceError("[GET] FAIL for {0} with \r{1}", objectName,
416
                                                               download.Exception);
417
                                          }
418
                                          else
419
                                          {
420
                                              Trace.TraceInformation("[GET] END {0}", objectName);                                             
421
                                          }
422
                                      });
423
            }
424
            catch (Exception exc)
395 425
            {
396
                return response.ContentStream;
426
                Trace.TraceError("[GET] END {0} with {1}", objectName, exc);
427
                throw;
397 428
            }
398
            else
399
                throw new WebException(String.Format("GetObject failed with unexpected status code {0}", response.StatusCode));
429

  
430

  
431

  
400 432
        }
401 433

  
402 434
        /// <summary>
......
438 470
                Trace.TraceInformation("[PUT] START {0}", objectName);
439 471
                client.UploadProgressChanged += (sender, args) =>
440 472
                {
441
                    Trace.TraceInformation("[PROGRESS] {0} {1}% {2} of {3}", fileName, args.ProgressPercentage, args.BytesSent, args.TotalBytesToSend);
473
                    Trace.TraceInformation("[PUT PROGRESS] {0} {1}% {2} of {3}", fileName, args.ProgressPercentage, args.BytesSent, args.TotalBytesToSend);
442 474
                };
443 475
               
444 476
                return client.UploadFileTask(uri, "PUT", fileName)

Also available in: Unified diff