Ejemplo n.º 1
0
        /// <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);
            }
        }
Ejemplo n.º 2
0
        /// <inheritdoc/>
        public override async Task <List <string> > ListFileNamesAsync(CloudStorageCredentials credentials)
        {
            credentials.ThrowIfInvalid(CredentialsRequirements);

            try
            {
                // Request only 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:resourcetype/>
</D:prop>
</D:propfind>");

                HttpContent content = new ByteArrayContent(requestBytes);
                XDocument   responseXml;
                Url         url = new Url(IncludeTrailingSlash(credentials.Url));
                using (Stream responseStream = await Flurl.Request(url)
                                               .WithBasicAuth(credentials.Username, credentials.UnprotectedPassword)
                                               .WithHeader("Depth", "1")
                                               .WithTimeout(20)
                                               .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);
            }
        }
Ejemplo n.º 3
0
        /// <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);
            }
        }
Ejemplo n.º 4
0
        /// <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);
            }
        }
Ejemplo n.º 5
0
 public override Task DeleteFileAsync(string filename, CloudStorageCredentials credentials)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 6
0
 public override Task UploadFileAsync(string filename, byte[] fileContent, CloudStorageCredentials credentials)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 7
0
 public override Task <List <string> > ListFileNamesAsync(CloudStorageCredentials credentials)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 8
0
 /// <inheritdoc/>
 public override Task <byte[]> DownloadFileAsync(string filename, CloudStorageCredentials credentials)
 {
     return(base.DownloadFileAsync(filename, WithPredefinedUrl(credentials)));
 }
Ejemplo n.º 9
0
 private async Task DeleteFileWorksAsync(string fileName, CloudStorageCredentials credentials, IFtpFakeResponse fakeResponse)
 {
     ICloudStorageClient client = new FtpCloudStorageClient(fakeResponse);
     await client.DeleteFileAsync(fileName, credentials);
 }
        /// <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);
            }
        }
Ejemplo n.º 11
0
 private async Task UploadFileWorksAsync(string fileName, byte[] fileContent, IFtpFakeResponse fakeResponse)
 {
     ICloudStorageClient     client      = new FtpCloudStorageClient(fakeResponse);
     CloudStorageCredentials credentials = GetCredentials();
     await client.UploadFileAsync(fileName, fileContent, credentials);
 }
        /// <inheritdoc/>
        public override async Task <bool> ExistsFileAsync(string filename, CloudStorageCredentials credentials)
        {
            string id = await FindFileIdAsync(credentials.Token.AccessToken, filename);

            return(!string.IsNullOrEmpty(id));
        }
Ejemplo n.º 13
0
 /// <inheritdoc/>
 public override Task <List <string> > ListFileNamesAsync(CloudStorageCredentials credentials)
 {
     return(base.ListFileNamesAsync(WithPredefinedUrl(credentials)));
 }
Ejemplo n.º 14
0
 /// <inheritdoc/>
 public override Task DeleteFileAsync(string filename, CloudStorageCredentials credentials)
 {
     return(base.DeleteFileAsync(filename, WithPredefinedUrl(credentials)));
 }
Ejemplo n.º 15
0
 public static IServiceCollection AddCloudStorageService(this IServiceCollection services, CloudStorageCredentials credentials)
 {
     services.AddCloudStorageContext(credentials.DbConnectionString);
     if (credentials.UseStorageSsl)
     {
         services.AddSingleton(x => new MinioClient(credentials.Url, credentials.AccessKey, credentials.SecretKey).WithSSL());
     }
     else
     {
         services.AddSingleton(x => new MinioClient(credentials.Url, credentials.AccessKey, credentials.SecretKey));
     }
     services.AddTransient <IReportPdfBucketService, ReportPdfBucketService>();
     return(services);
 }
Ejemplo n.º 16
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);
            }
        }
Ejemplo n.º 17
0
        /// <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);
            }
        }
Ejemplo n.º 18
0
 /// <inheritdoc/>
 public override Task UploadFileAsync(string filename, byte[] fileContent, CloudStorageCredentials credentials)
 {
     return(base.UploadFileAsync(filename, fileContent, WithPredefinedUrl(credentials)));
 }