public void ValidateRejectsInvalidCredentials() { CloudStorageCredentials credentials = new CloudStorageCredentials(); Assert.Throws <InvalidParameterException>(() => credentials.ThrowIfInvalid(CloudStorageCredentialsRequirements.Token)); Assert.Throws <InvalidParameterException>(() => credentials.ThrowIfInvalid(CloudStorageCredentialsRequirements.UsernamePassword)); Assert.Throws <InvalidParameterException>(() => credentials.ThrowIfInvalid(CloudStorageCredentialsRequirements.UsernamePasswordUrl)); Assert.Throws <InvalidParameterException>(() => credentials.ThrowIfInvalid(CloudStorageCredentialsRequirements.UsernamePasswordUrlSecure)); credentials = null; Assert.Throws <InvalidParameterException>(() => credentials.ThrowIfInvalid(CloudStorageCredentialsRequirements.Token)); }
/// <inheritdoc/> public override async Task <byte[]> DownloadFileAsync(string filename, CloudStorageCredentials credentials) { credentials.ThrowIfInvalid(CredentialsRequirements); try { // First find the id of the file string fileId = await FindFileIdAsync(credentials.Token.AccessToken, filename); if (fileId == null) { throw new ConnectionFailedException(string.Format("The file '{0}' does not exist.", filename), null); } byte[] responseData = await Flurl.Request(DownloadUrl, fileId) .WithOAuthBearerToken(credentials.Token.AccessToken) .SetQueryParam("alt", "media") .GetAsync() .ReceiveBytes(); return(responseData); } catch (Exception ex) { throw ConvertToCloudStorageException(ex); } }
/// <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); } }
/// <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); } }
/// <inheritdoc/> public override async Task UploadFileAsync(string filename, byte[] fileContent, CloudStorageCredentials credentials) { credentials.ThrowIfInvalid(CredentialsRequirements); try { string jsonPathParameter = JsonConvert.SerializeObject(new { path = EnsureLeadingSlash(filename), mode = "overwrite", autorename = false, }); HttpContent content = new ByteArrayContent(fileContent); await Flurl.Request(UploadUrl) .WithOAuthBearerToken(credentials.Token.AccessToken) .WithHeader("Dropbox-API-Arg", jsonPathParameter) .WithHeader("Content-Type", "application/octet-stream") .PostAsync(content); } catch (Exception ex) { throw ConvertToCloudStorageException(ex); } }
/// <inheritdoc/> public override async Task DeleteFileAsync(string filename, CloudStorageCredentials credentials) { credentials.ThrowIfInvalid(CredentialsRequirements); try { Url fileUrl = new Url(credentials.Url).AppendPathSegment(filename); using (WebClient webClient = new CustomFtpWebClient(request => { request.Timeout = (int)TimeSpan.FromSeconds(DeleteTimeoutSeconds).TotalMilliseconds; request.Method = WebRequestMethods.Ftp.DeleteFile; // "DELE" request.EnableSsl = credentials.Secure; })) { webClient.Credentials = new NetworkCredential(credentials.Username, credentials.Password); if (!IsInTestMode) { await webClient.DownloadDataTaskAsync(fileUrl); } } } catch (Exception ex) { throw ConvertToCloudStorageException(ex); } }
/// <inheritdoc/> public override async Task <List <string> > ListFileNamesAsync(CloudStorageCredentials credentials) { credentials.ThrowIfInvalid(CredentialsRequirements); try { List <string> result = new List <string>(); string url = ListUrl; while (url != null) { string jsonResponse = await Flurl.Request(url) .WithOAuthBearerToken(credentials.Token.AccessToken) .SetQueryParam("$select", "name,file") .GetAsync() .ReceiveString(); JsonFolderEntries entries = JsonConvert.DeserializeObject <JsonFolderEntries>(jsonResponse); result.AddRange(entries.Entries .Where(item => item.File != null) .Select(item => item.Name)); url = entries.NextLink; } return(result); } catch (Exception ex) { throw ConvertToCloudStorageException(ex); } }
/// <inheritdoc/> public override async Task <List <string> > ListFileNamesAsync(CloudStorageCredentials credentials) { credentials.ThrowIfInvalid(CredentialsRequirements); try { List <string> result = new List <string>(); JsonFolderEntries entries = null; do { string jsonResponse = await Flurl.Request(ListUrl) .WithOAuthBearerToken(credentials.Token.AccessToken) .SetQueryParam("spaces", DataFolder) .SetQueryParam("pageToken", entries?.Cursor) .SetQueryParam("fields", "nextPageToken, incompleteSearch, files(kind, id, name)") .GetAsync() .ReceiveString(); entries = JsonConvert.DeserializeObject <JsonFolderEntries>(jsonResponse); result.AddRange(entries.Entries .Where(item => item.Kind == "drive#file") .Select(item => item.Name)); }while (entries?.Cursor != null); return(result); } catch (Exception ex) { throw ConvertToCloudStorageException(ex); } }
/// <inheritdoc/> public override async Task UploadFileAsync(string filename, byte[] fileContent, CloudStorageCredentials credentials) { credentials.ThrowIfInvalid(CredentialsRequirements); try { await InitializeAppRootFolderAsync(credentials.Token.AccessToken); string url = string.Format(UploadUrl, Url.Encode(filename)); byte[] requestBytes = Encoding.UTF8.GetBytes("{ \"item\": { \"@microsoft.graph.conflictBehavior\": \"replace\" } }"); HttpContent sessionContent = new ByteArrayContent(requestBytes); // Using an upload session is the recommended approach for reliable file transfer, // not only for big files. string jsonResponse = await Flurl.Request(url) .WithOAuthBearerToken(credentials.Token.AccessToken) .WithHeader("Content-Type", "application/json") .PostAsync(sessionContent) .ReceiveString(); JsonUploadSession session = JsonConvert.DeserializeObject <JsonUploadSession>(jsonResponse); // Now that we have got the session, the file can be uploaded HttpContent content = new ByteArrayContent(fileContent); HttpResponseMessage uploadResponse = await Flurl.Request(session.UploadUrl) .WithHeader("Content-Length", fileContent.Length) .WithHeader("Content-Range", string.Format("bytes 0-{0}/{1}", fileContent.Length - 1, fileContent.Length)) .PutAsync(content); } catch (Exception ex) { throw ConvertToCloudStorageException(ex); } }
/// <inheritdoc/> public override async Task <List <string> > ListFileNamesAsync(CloudStorageCredentials credentials) { credentials.ThrowIfInvalid(CredentialsRequirements); try { // Request only displayname and resourcetype to reduce network traffic byte[] requestBytes = Encoding.UTF8.GetBytes( @"<?xml version='1.0' encoding='utf-8'?> <D:propfind xmlns:D='DAV:'> <D:prop> <D:displayname/> <D:resourcetype/> </D:prop> </D:propfind>"); HttpContent content = new ByteArrayContent(requestBytes); XDocument responseXml; using (Stream responseStream = await Flurl.Request(credentials.Url) .WithBasicAuth(credentials.Username, credentials.UnprotectedPassword) .WithHeader("Depth", "1") .SendAsync(new HttpMethod("PROPFIND"), content) .ReceiveStream()) { responseXml = XDocument.Load(responseStream); } // Files have an empty resourcetype element, folders a child element "collection" return(ParseWebdavResponseForFileNames(responseXml)); } catch (Exception ex) { throw ConvertToCloudStorageException(ex); } }
public void ValidateAcceptsValidCredentials() { CloudStorageCredentials credentials = new CloudStorageCredentials { CloudStorageId = "Dropbox", Token = new CloudStorageToken(), Username = "******", UnprotectedPassword = "******", Url = "url", Secure = false }; Assert.DoesNotThrow(() => credentials.ThrowIfInvalid(CloudStorageCredentialsRequirements.Token)); Assert.DoesNotThrow(() => credentials.ThrowIfInvalid(CloudStorageCredentialsRequirements.UsernamePassword)); Assert.DoesNotThrow(() => credentials.ThrowIfInvalid(CloudStorageCredentialsRequirements.UsernamePasswordUrl)); Assert.DoesNotThrow(() => credentials.ThrowIfInvalid(CloudStorageCredentialsRequirements.UsernamePasswordUrlSecure)); }
/// <inheritdoc/> public override async Task DeleteFileAsync(string filename, CloudStorageCredentials credentials) { credentials.ThrowIfInvalid(CredentialsRequirements); try { await Flurl.Request(credentials.Url, filename) .WithBasicAuth(credentials.Username, credentials.UnprotectedPassword) .DeleteAsync(); } catch (Exception ex) { throw ConvertToCloudStorageException(ex); } }
/// <inheritdoc/> public override async Task <byte[]> DownloadFileAsync(string filename, CloudStorageCredentials credentials) { credentials.ThrowIfInvalid(CredentialsRequirements, true); try { return(await Flurl.Request(credentials.Url, filename) .WithBasicAuthOrAnonymous(credentials.Username, credentials.UnprotectedPassword) .GetBytesAsync()); } catch (Exception ex) { throw ConvertToCloudStorageException(ex); } }
/// <inheritdoc/> public override async Task UploadFileAsync(string filename, byte[] fileContent, CloudStorageCredentials credentials) { credentials.ThrowIfInvalid(CredentialsRequirements); try { HttpContent content = new ByteArrayContent(fileContent); await Flurl.Request(credentials.Url, filename) .WithBasicAuth(credentials.Username, credentials.UnprotectedPassword) .PutAsync(content); } catch (Exception ex) { throw ConvertToCloudStorageException(ex); } }
/// <inheritdoc/> public override async Task DeleteFileAsync(string filename, CloudStorageCredentials credentials) { credentials.ThrowIfInvalid(CredentialsRequirements); try { string url = string.Format(DeleteUrl, Url.Encode(filename)); await Flurl.Request(url) .WithOAuthBearerToken(credentials.Token.AccessToken) .DeleteAsync(); } catch (Exception ex) { throw ConvertToCloudStorageException(ex); } }
/// <inheritdoc/> public override async Task <List <string> > ListFileNamesAsync(CloudStorageCredentials credentials) { credentials.ThrowIfInvalid(CredentialsRequirements); try { string jsonResponse = await Flurl.Request(ListUrl) .WithOAuthBearerToken(credentials.Token.AccessToken) .PostJsonAsync(new { path = string.Empty, recursive = false, include_deleted = false, include_has_explicit_shared_members = false, include_mounted_folders = true, include_non_downloadable_files = false }) .ReceiveString(); JsonFolderEntries entries = JsonConvert.DeserializeObject <JsonFolderEntries>(jsonResponse); List <string> result = new List <string>(); result.AddRange(entries.Entries .Where(item => item.Tag == "file") .Select(item => item.Name)); // Check whether there are more file names to get and make consecutive requests. while (entries.HasMore) { jsonResponse = await Flurl.Request(ListContinueUrl) .WithOAuthBearerToken(credentials.Token.AccessToken) .PostJsonAsync(new { cursor = entries.Cursor }) .ReceiveString(); entries = JsonConvert.DeserializeObject <JsonFolderEntries>(jsonResponse); result.AddRange(entries.Entries .Where(item => item.Tag == "file") .Select(item => item.Name)); } return(result); } catch (Exception ex) { throw ConvertToCloudStorageException(ex); } }
/// <inheritdoc/> public override async Task DeleteFileAsync(string filename, CloudStorageCredentials credentials) { credentials.ThrowIfInvalid(CredentialsRequirements); try { await Flurl.Request(DeleteUrl) .WithOAuthBearerToken(credentials.Token.AccessToken) .PostJsonAsync(new { path = EnsureLeadingSlash(filename) }); } catch (Exception ex) { throw ConvertToCloudStorageException(ex); } }
/// <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); } }
/// <inheritdoc/> public override async Task <byte[]> DownloadFileAsync(string filename, CloudStorageCredentials credentials) { credentials.ThrowIfInvalid(CredentialsRequirements); try { string url = string.Format(DownloadUrl, Url.Encode(filename)); byte[] responseData = await Flurl.Request(url) .WithOAuthBearerToken(credentials.Token.AccessToken) .GetAsync() .ReceiveBytes(); return(responseData); } catch (Exception ex) { throw ConvertToCloudStorageException(ex); } }
/// <inheritdoc/> public override async Task DeleteFileAsync(string filename, CloudStorageCredentials credentials) { credentials.ThrowIfInvalid(CredentialsRequirements); try { // First find the id of the file string fileId = await FindFileIdAsync(credentials.Token.AccessToken, filename); if (fileId == null) { throw new ConnectionFailedException(string.Format("The file '{0}' does not exist.", filename), null); } await Flurl.Request(DeleteUrl, fileId) .WithOAuthBearerToken(credentials.Token.AccessToken) .DeleteAsync(); } catch (Exception ex) { throw ConvertToCloudStorageException(ex); } }
/// <inheritdoc/> public override async Task UploadFileAsync(string filename, byte[] fileContent, CloudStorageCredentials credentials) { credentials.ThrowIfInvalid(CredentialsRequirements); try { // First we check whether this file already exists, creating new files with the // same name will generate different versions. string fileId = await FindFileIdAsync(credentials.Token.AccessToken, filename); if (fileId == null) { await CreateNewFile(filename, fileContent, credentials); } else { await UpdateExistingFile(fileId, fileContent, credentials); } } catch (Exception ex) { throw ConvertToCloudStorageException(ex); } }
/// <inheritdoc/> public override async Task <byte[]> DownloadFileAsync(string filename, CloudStorageCredentials credentials) { credentials.ThrowIfInvalid(CredentialsRequirements); try { string jsonPathParameter = JsonConvert.SerializeObject(new { path = EnsureLeadingSlash(filename) }); byte[] result = await Flurl.Request(DownloadUrl) .WithOAuthBearerToken(credentials.Token.AccessToken) .WithHeader("Dropbox-API-Arg", jsonPathParameter) .PostAsync(null) .ReceiveBytes(); return(result); } catch (Exception ex) { throw ConvertToCloudStorageException(ex); } }