Example #1
0
        public static void Dispose()
        {
            // Just in case
            if (TempCASTarget != null)
            {
                CredentialUtil.RemoveCredentials(TempCASTarget);
                TempCASTarget = null;
            }

            CASTarget = DefaultCASTarget;
            _downloadProgressTrackingHandler.Dispose();
            SessionClient.Dispose();
        }
Example #2
0
 public void Free()
 {
     if (ProgressHandler != null)
     {
         ProgressHandler.Dispose();
     }
     if (_handler != null)
     {
         _handler.Dispose();
     }
     if (_client != null)
     {
         _client.Dispose();
     }
 }
Example #3
0
 public virtual void Configure(Settings value, Config.Config config,
                               CancellationToken token = default(CancellationToken))
 {
     Config   = config;
     Token    = token;
     Settings = value;
     Cleaner.DefaultRandomFileDir = config.TmpDir;
     if (errorMap.Count == 0)
     {
         if (!String.IsNullOrEmpty(Config.AltUri))
         {
             Config.AltUri.Split(',').Each(x => {
                 errorMap.Add(new Uri(x.Trim()).ToString(), 0);
             });
         }
         else if (Config.BaseUrl != null)
         {
             errorMap.Add(Config.BaseUrl.ToString(), 0);
         }
     }
     if (Client != null)
     {
         Client.Dispose();
         Disposable.Remove(Client);
     }
     if (HttpProgress != null)
     {
         HttpProgress.Dispose();
         Disposable.Remove(HttpProgress);
         HttpProgress.HttpReceiveProgress -= ReceiveProgress;
     }
     if (Handler != null)
     {
         Handler.Dispose();
         Disposable.Remove(Handler);
     }
     Client = Settings.GetHttpClient(Config, ref HttpProgress, ref Handler);
     Disposable.Add(Client);
     Disposable.Add(HttpProgress);
     Disposable.Add(Handler);
     HttpProgress.HttpReceiveProgress += ReceiveProgress;
 }
        // ReSharper disable once CyclomaticComplexity
        internal async Task <FileSystemResult <IFile> > InternalCreateFile(string name, string type, bool overwrite, AmazonObject parent, Stream readstream, CancellationToken token, IProgress <FileProgress> progress, Dictionary <string, object> properties)
        {
            if (readstream.Length == 0)
            {
                throw new ArgumentException("input stream must have length");
            }
            string url = AmazonUpload.FormatRest(FS.OAuth.EndPoint.ContentUrl, "?suppress=deduplication");

            Json.Metadata j = null;
            if (!overwrite)
            {
                if (properties == null)
                {
                    properties = new Dictionary <string, object>();
                }
                j                             = new Json.Metadata();
                j.version                     = 0;
                j.contentProperties           = new Json.ContentProperties();
                j.contentProperties.size      = readstream.Length;
                j.contentProperties.version   = 0;
                j.contentProperties.extension = Path.GetExtension(name);
                string n = Extensions.ContentFromExtension(j.contentProperties.extension);
                j.contentProperties.contentType = !string.IsNullOrEmpty(n) ? n : "application/octet-stream";
                if (properties.Any(a => a.Key.Equals("ModifiedDate", StringComparison.InvariantCultureIgnoreCase)))
                {
                    j.modifiedDate =
                        (DateTime)
                        properties.First(
                            a => a.Key.Equals("ModifiedDate", StringComparison.InvariantCultureIgnoreCase)).Value;
                }
                if (properties.Any(a => a.Key.Equals("CreatedDate", StringComparison.InvariantCultureIgnoreCase)))
                {
                    j.createdDate =
                        (DateTime)
                        properties.First(
                            a => a.Key.Equals("CreatedDate", StringComparison.InvariantCultureIgnoreCase)).Value;
                }
                if (properties.Any(a => a.Key.Equals("Application", StringComparison.InvariantCultureIgnoreCase)))
                {
                    j.createdBy =
                        (string)
                        properties.First(
                            a => a.Key.Equals("Application", StringComparison.InvariantCultureIgnoreCase)).Value;
                }
                else
                {
                    j.createdBy = "CloudFileSystem";
                }
                if (properties.Any(a => a.Key.Equals("MD5", StringComparison.InvariantCultureIgnoreCase)))
                {
                    j.contentProperties.md5 =
                        (string)
                        properties.First(a => a.Key.Equals("MD5", StringComparison.InvariantCultureIgnoreCase))
                        .Value;
                }
                j.description = j.name = name;
                j.isShared    = false;
                j.kind        = type;
                j.parents     = new List <string>();
                j.parents.Add(parent.Id);
            }
            HttpRequestMessage msg      = new HttpRequestMessage(overwrite ? HttpMethod.Put : HttpMethod.Post, url);
            string             boundary = "--" + Guid.NewGuid().ToString();

            msg.Headers.UserAgent.ParseAdd(FS.OAuth.UserAgent);
            MultipartFormDataContent ct = new MultipartFormDataContent(boundary);

            if (!overwrite)
            {
                string        meta = JsonConvert.SerializeObject(j);
                StringContent sc   = new StringContent(meta, Encoding.UTF8);
                ct.Add(sc, "metadata");
            }
            StreamContent ssc = new StreamContent(readstream);

            ct.Add(ssc, "file", name);
            msg.Content = ct;
            HttpClientHandler      handler         = new HttpClientHandler();
            ProgressMessageHandler progresshandler = new ProgressMessageHandler(handler);

            progresshandler.HttpSendProgress += (a, b) =>
            {
                FileProgress u = new FileProgress();
                u.Percentage   = b.ProgressPercentage;
                u.TotalSize    = b.TotalBytes ?? 0;
                u.TransferSize = b.BytesTransferred;
                progress.Report(u);
            };
            handler.AllowAutoRedirect      = true;
            handler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
            await FS.CheckExpirations();

            HttpClient          cl       = new HttpClient(progresshandler);
            HttpResponseMessage response = null;

            try
            {
                response = await cl.SendAsync(msg, token);

                if (response.StatusCode == HttpStatusCode.Created)
                {
                    string dd = await response.Content.ReadAsStringAsync();

                    string          parentpath = string.Empty;
                    AmazonDirectory dir        = parent as AmazonDirectory;
                    if (dir == null || !dir.IsRoot)
                    {
                        parentpath = FullName;
                    }
                    AmazonFile file = new AmazonFile(parentpath, FS)
                    {
                        Parent = dir
                    };
                    file.SetData(dd);
                    progress.Report(new FileProgress
                    {
                        Percentage   = 100,
                        TotalSize    = file.Size,
                        TransferSize = file.Size
                    });
                    return(new FileSystemResult <IFile>(file));
                }
                return(new FileSystemResult <IFile>("Http Error : " + response.StatusCode));
            }
            catch (Exception e)
            {
                return(new FileSystemResult <IFile>("Exception Error : " + e.Message));
            }
            finally
            {
                response?.Dispose();
                cl?.Dispose();
                handler?.Dispose();
                progresshandler?.Dispose();
                msg?.Dispose();
            }
        }
Example #5
0
        public async Task <MultipartUploadResult> MultipartUpload(MultiUploadRequestData multiUploadObject,
                                                                  Action <HttpProcessData> uploadProcessCallback = null, CancellationToken?cancellationToken = null)
        {
            MultipartUploadResult result = null;

            HttpClientHandler      hand = null;
            ProgressMessageHandler processMessageHander = null;
            HttpClient             localHttpClient      = null;
            OssHttpRequestMessage  httpRequestMessage   = null;
            HttpResponseMessage    response             = null;

            try
            {
                hand = new HttpClientHandler();
                processMessageHander     = new ProgressMessageHandler(hand);
                localHttpClient          = new HttpClient(processMessageHander);
                localHttpClient.Timeout += new TimeSpan(2 * TimeSpan.TicksPerHour);

                Dictionary <string, string> parameters = new Dictionary <string, string>();
                parameters.Add("partNumber", multiUploadObject.PartNumber);
                parameters.Add("uploadId", multiUploadObject.UploadId);

                httpRequestMessage = new OssHttpRequestMessage(multiUploadObject.Bucket, multiUploadObject.Key, parameters);

                httpRequestMessage.Method       = HttpMethod.Put;
                httpRequestMessage.Headers.Date = DateTime.UtcNow;
                httpRequestMessage.Content      = new StreamContent(multiUploadObject.Content);

                if (uploadProcessCallback != null)
                {
                    processMessageHander.HttpSendProgress += (sender, e) =>
                    {
                        uploadProcessCallback(new HttpProcessData()
                        {
                            TotalBytes         = e.TotalBytes,
                            BytesTransferred   = e.BytesTransferred,
                            ProgressPercentage = e.ProgressPercentage
                        });
                    };
                }


                OssRequestSigner.Sign(httpRequestMessage, networkCredential);

                if (cancellationToken != null)
                {
                    response = await localHttpClient.SendAsync(httpRequestMessage, (CancellationToken)cancellationToken);
                }
                else
                {
                    response = await localHttpClient.SendAsync(httpRequestMessage);
                }

                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }
                var deseserializer = DeserializerFactory.GetFactory().CreateMultipartUploadDeserializer();
                result = deseserializer.Deserialize(response);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (hand != null)
                {
                    hand.Dispose();
                }

                if (processMessageHander != null)
                {
                    processMessageHander.Dispose();
                }

                if (localHttpClient != null)
                {
                    localHttpClient.Dispose();
                }

                if (httpRequestMessage != null)
                {
                    httpRequestMessage.Dispose();
                }


                if (response != null)
                {
                    response.Dispose();
                }
            }
            return(result);
        }
Example #6
0
        public async Task <OssObject> GetObject(GetObjectRequest getObjectRequest,
                                                Action <HttpProcessData> downloadProcessCallback = null, CancellationToken?cancellationToken = null)
        {
            OssObject result = null;

            HttpClientHandler      hand = null;
            ProgressMessageHandler processMessageHander = null;
            HttpClient             localHttpClient      = null;
            OssHttpRequestMessage  httpRequestMessage   = null;
            HttpResponseMessage    response             = null;

            try
            {
                hand = new HttpClientHandler();
                processMessageHander     = new ProgressMessageHandler(hand);
                localHttpClient          = new HttpClient(processMessageHander);
                localHttpClient.Timeout += new TimeSpan(2 * TimeSpan.TicksPerHour);

                httpRequestMessage = new OssHttpRequestMessage(getObjectRequest.BucketName, getObjectRequest.Key);
                getObjectRequest.ResponseHeaders.Populate(httpRequestMessage.Headers);
                getObjectRequest.Populate(httpRequestMessage.Headers);

                httpRequestMessage.Method       = HttpMethod.Get;
                httpRequestMessage.Headers.Date = DateTime.UtcNow;

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                if (downloadProcessCallback != null)
                {
                    processMessageHander.HttpReceiveProgress += (sender, e) =>
                    {
                        downloadProcessCallback(new HttpProcessData()
                        {
                            TotalBytes         = e.TotalBytes,
                            BytesTransferred   = e.BytesTransferred,
                            ProgressPercentage = e.ProgressPercentage
                        });;
                    };
                }

                if (cancellationToken != null)
                {
                    response = await localHttpClient.SendAsync(httpRequestMessage, (CancellationToken)cancellationToken);
                }
                else
                {
                    response = await localHttpClient.SendAsync(httpRequestMessage);
                }


                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }

                var temp = DeserializerFactory.GetFactory().CreateGetObjectResultDeserializer(getObjectRequest);
                result = await temp.Deserialize(response);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (hand != null)
                {
                    hand.Dispose();
                }

                if (processMessageHander != null)
                {
                    processMessageHander.Dispose();
                }

                if (localHttpClient != null)
                {
                    localHttpClient.Dispose();
                }

                if (httpRequestMessage != null)
                {
                    httpRequestMessage.Dispose();
                }
            }


            return(result);
        }
Example #7
0
        public async Task <PutObjectResult> PutObject(string bucketName, string key, Stream content, ObjectMetadata metadata,
                                                      Action <HttpProcessData> uploadProcessCallback = null, CancellationToken?cancellationToken = null)
        {
            PutObjectResult        result = null;
            HttpClientHandler      hand   = null;
            ProgressMessageHandler processMessageHander = null;
            HttpClient             localHttpClient      = null;
            OssHttpRequestMessage  httpRequestMessage   = null;
            HttpResponseMessage    response             = null;

            try
            {
                hand = new HttpClientHandler();
                processMessageHander     = new ProgressMessageHandler(hand);
                localHttpClient          = new HttpClient(processMessageHander);
                localHttpClient.Timeout += new TimeSpan(2 * TimeSpan.TicksPerHour);
                httpRequestMessage       = new OssHttpRequestMessage(bucketName, key);



                httpRequestMessage.Method       = HttpMethod.Put;
                httpRequestMessage.Headers.Date = DateTime.UtcNow;
                httpRequestMessage.Content      = new StreamContent(content);


                OssClientHelper.initialHttpRequestMessage(httpRequestMessage, metadata);



                OssRequestSigner.Sign(httpRequestMessage, networkCredential);

                if (uploadProcessCallback != null)
                {
                    processMessageHander.HttpSendProgress += (sender, e) =>
                    {
                        uploadProcessCallback(new HttpProcessData()
                        {
                            TotalBytes         = e.TotalBytes,
                            BytesTransferred   = e.BytesTransferred,
                            ProgressPercentage = e.ProgressPercentage
                        });
                    };
                }


                if (cancellationToken != null)
                {
                    response = await localHttpClient.SendAsync(httpRequestMessage, (CancellationToken)cancellationToken);
                }
                else
                {
                    response = await localHttpClient.SendAsync(httpRequestMessage);
                }

                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }

                var temp = DeserializerFactory.GetFactory().CreatePutObjectReusltDeserializer();
                result = temp.Deserialize(response);
                //localHttpClient.Dispose();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (hand != null)
                {
                    hand.Dispose();
                }

                if (processMessageHander != null)
                {
                    processMessageHander.Dispose();
                }

                if (localHttpClient != null)
                {
                    localHttpClient.Dispose();
                }

                if (httpRequestMessage != null)
                {
                    httpRequestMessage.Dispose();
                }


                if (response != null)
                {
                    response.Dispose();
                }
            }

            return(result);
        }