Exemplo n.º 1
0
        /// <inheritdoc/>
        public override async Task UploadFileAsync(string filename, byte[] fileContent, CloudStorageCredentials credentials)
        {
            credentials.ThrowIfInvalid(CredentialsRequirements, true);
            SanitizeCredentials(credentials);

            try
            {
                Url fileUrl = new Url(credentials.Url).AppendPathSegment(filename);
                using (var certificateAcceptor = new CertificateAcceptor(credentials.AcceptInvalidCertificate))
                    using (WebClient webClient = new CustomFtpWebClient(request =>
                    {
                        request.Timeout = (int)TimeSpan.FromSeconds(UploadTimeoutSeconds).TotalMilliseconds;
                        request.UseBinary = true;
                        request.EnableSsl = credentials.Secure;
                    }))
                    {
                        webClient.Credentials = new NetworkCredential(credentials.Username, credentials.Password);
                        if (!IsInTestMode)
                        {
                            await webClient.UploadDataTaskAsync(fileUrl, fileContent);
                        }
                    }
            }
            catch (Exception ex)
            {
                throw ConvertToCloudStorageException(ex);
            }
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public override async Task <byte[]> DownloadFileAsync(string filename, CloudStorageCredentials credentials)
        {
            credentials.ThrowIfInvalid(CredentialsRequirements, true);
            SanitizeCredentials(credentials);

            try
            {
                Url    fileUrl = new Url(credentials.Url).AppendPathSegment(filename);
                byte[] responseData;
                using (var certificateAcceptor = new CertificateAcceptor(credentials.AcceptInvalidCertificate))
                    using (WebClient webClient = new CustomFtpWebClient(request =>
                    {
                        request.Timeout = (int)TimeSpan.FromSeconds(DownloadTimeoutSeconds).TotalMilliseconds;
                        request.UseBinary = true;
                        request.EnableSsl = credentials.Secure;
                    }))
                    {
                        webClient.Credentials = new NetworkCredential(credentials.Username, credentials.Password);
                        if (IsInTestMode)
                        {
                            responseData = _fakeResponse.GetFakeServerResponseBytes(fileUrl);
                        }
                        else
                        {
                            responseData = await webClient.DownloadDataTaskAsync(fileUrl);
                        }
                    }
                return(responseData);
            }
            catch (Exception ex)
            {
                throw ConvertToCloudStorageException(ex);
            }
        }
Exemplo n.º 3
0
        /// <inheritdoc/>
        public override async Task <List <string> > ListFileNamesAsync(CloudStorageCredentials credentials)
        {
            credentials.ThrowIfInvalid(CredentialsRequirements, true);
            SanitizeCredentials(credentials);

            try
            {
                // Call the list command
                Uri    directoryUri = new Uri(IncludeTrailingSlash(credentials.Url));
                string responseData = null;
                using (var certificateAcceptor = new CertificateAcceptor(credentials.AcceptInvalidCertificate))
                    using (WebClient webClient = new CustomFtpWebClient(request =>
                    {
                        request.Timeout = (int)TimeSpan.FromSeconds(ListTimeoutSeconds).TotalMilliseconds;
                        request.Proxy = WebRequest.DefaultWebProxy;
                        request.Method = WebRequestMethods.Ftp.ListDirectory; // "NLST"
                        request.EnableSsl = credentials.Secure;
                    }))
                    {
                        webClient.Credentials = new NetworkCredential(credentials.Username, credentials.Password);
                        if (IsInTestMode)
                        {
                            responseData = _fakeResponse.GetFakeServerResponseString(credentials.Url);
                        }
                        else
                        {
                            responseData = await webClient.DownloadStringTaskAsync(directoryUri);
                        }
                    }

                // Interpret the response
                string   unixDelimitedResponse = responseData.Replace("\r\n", "\n");
                string[] fileNames             = unixDelimitedResponse.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

                List <string> result = new List <string>(fileNames);
                result.Remove("..");
                result.Remove(".");
                return(result);
            }
            catch (Exception ex)
            {
                throw ConvertToCloudStorageException(ex);
            }
        }