示例#1
0
        public async Task <C4CRemoteFileListing> GetFileListingAsync(IRemoteResource source)
        {
            // await fetchCsrfTokenAsync();
            HttpResponseMessage responseMessage;

            string query = "?$select=UUID,MimeType,Name,DocumentLink,CategoryCode,LastUpdatedOn&$orderby=Name";

            string sourceSubPath = await source.GetSubPathAsync();

            try
            {
                responseMessage = await this.httpClient.GetAsync(sourceSubPath + query);

                responseMessage.EnsureSuccessStatusCode();
            }
            catch (Exception ex)
            {
                throw new C4CClientException("Error occured while receiving file listing", ex);
            }

            var content = await responseMessage.Content.ReadAsStringAsync();

            C4CRemoteFileListing fileListing = new C4CRemoteFileListing(content);

            return(fileListing);
        }
示例#2
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);
        }
示例#3
0
        public virtual void UpdateResource(RemoteOperationContext context, string path, IRemoteResource resource, IReadOnlyDictionary <string, object> properties)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            // properties can be null

            // rename is when the resource name doesn't match the path
            bool rename = resource.DisplayName != null && !resource.DisplayName.EqualsIgnoreCase(Path.GetFileName(path));
            var  uri    = GetUri(path);

            using (var client = CreateWebClient())
            {
                if (client == null)
                {
                    throw new InvalidOperationException();
                }

                SetupWebClient(client);

                string xml;
                if (rename)
                {
                    client.Headers["Overwrite"] = "t";
                    var newPath = Path.Combine(Path.GetDirectoryName(path), resource.DisplayName);
                    var newUri  = GetUri(newPath);
                    client.Headers["Destination"] = newUri.AbsolutePath;

                    try
                    {
                        xml = client.UploadString(uri, "MOVE", string.Empty);
                    }
                    catch (Exception e)
                    {
                        context.AddError(e);
                        context.Log(TraceLevel.Error, "Error on MOVE " + uri + ": " + e.Message);
                        throw;
                    }
                }
                else
                {
                    var inputDoc = new XmlDocument();
                    var pud      = inputDoc.CreateElement(null, "propertyupdate", DavNamespaceUri);
                    inputDoc.AppendChild(pud);
                    var set = inputDoc.CreateElement(null, "set", DavNamespaceUri);
                    pud.AppendChild(set);
                    var prop = inputDoc.CreateElement(null, "prop", DavNamespaceUri);
                    set.AppendChild(prop);

                    AddProperty(prop, "Win32FileAttributes", MsNamespaceUri, ((int)resource.Attributes).ToHex());

                    if (resource.CreationTimeUtc != DateTime.MinValue)
                    {
                        AddProperty(prop, "Win32CreationTime", MsNamespaceUri, resource.CreationTimeUtc);
                    }

                    if (resource.LastWriteTimeUtc != DateTime.MinValue)
                    {
                        AddProperty(prop, "Win32LastModifiedTime", MsNamespaceUri, resource.LastWriteTimeUtc);
                    }

                    try
                    {
                        xml = client.UploadString(uri, "PROPPATCH", inputDoc.OuterXml);
                    }
                    catch (Exception e)
                    {
                        context.AddError(e);
                        context.Log(TraceLevel.Error, "Error on PROPPATCH " + uri + ": " + e.Message);
                        throw;
                    }
                }

                var doc = new XmlDocument();
                if (!string.IsNullOrWhiteSpace(xml))
                {
                    try
                    {
                        doc.LoadXml(xml);
                    }
                    catch (Exception e)
                    {
                        context.AddError(e);
                        context.Log(TraceLevel.Error, "Error on LoadXml " + uri + ": " + e.Message);
                        throw;
                    }
                }
            }
        }