Example #1
0
        public async Task <IRemoteFileMetadata> UploadFileAsync(ILocalResource source, IRemoteResource target)
        {
            await this.FetchCsrfTokenAsync().ConfigureAwait(false);

            HttpResponseMessage responseMessage;
            string content;

            var subPath = await target.GetSubPathAsync();

            try
            {
                var request = new HttpRequestMessage();

                request.RequestUri = new Uri(subPath, UriKind.Relative);
                request.Method     = System.Net.Http.HttpMethod.Post;
                request.Headers.Add("x-csrf-token", this.CSRFToken);
                request.Headers.Add("Accept", "application/xml");
                request.Headers.Add("odata-no-response-payload", "true");

                string requestcontent = "{\"TypeCode\": \"" + target.TypeCode + "\",\"Name\": \"" + source.GetFileName() + "\",\"CategoryCode\": \"2\",\"Binary\": \"" + source.GetBase64SourceString() + "\"}";

                request.Content = new StringContent(requestcontent, Encoding.UTF8, "application/json");

                responseMessage = await this.httpClient.SendAsync(request);

                responseMessage.EnsureSuccessStatusCode();

                await responseMessage.Content.ReadAsStringAsync();
            }
            catch (Exception ex)
            {
                throw new C4CClientException("Error occured while uploading file", ex);
            }

            // we send a second request to read the metadata of the newly uploaded file
            HttpHeaders          headers = responseMessage.Headers;
            IEnumerable <string> values;
            string metadataUri = string.Empty;

            if (headers.TryGetValues("Location", out values))
            {
                metadataUri = values.First();
            }

            if (string.IsNullOrEmpty(metadataUri))
            {
                throw new C4CClientException("Location-header not provided by remote host");
            }

            // read back file metadata
            string metadataRequestUri = metadataUri + "?$select=UUID,MimeType,Name,DocumentLink,CategoryCode,LastUpdatedOn";

            responseMessage = await this.httpClient.GetAsync(metadataRequestUri);

            content = await responseMessage.Content.ReadAsStringAsync();

            C4CRemoteFileMetadata metadata = new C4CRemoteFileMetadata(content);

            return(metadata);
        }
Example #2
0
        public async Task DownloadFileAsync(IRemoteFileMetadata source, ILocalResource target)
        {
            // await fetchCsrfTokenAsync();
            try
            {
                var responseMessage = await this.httpClient.GetAsync(source.DownloadURI);

                responseMessage.EnsureSuccessStatusCode();

                await target.WriteNewFile(responseMessage.Content);
            }
            catch (Exception ex)
            {
                throw new C4CClientException("An error occured while downloading file.", ex);
            }
        }
Example #3
0
 public bool Equals(ILocalResource other)
 {
     return(Path == other.Path);
 }