Esempio n. 1
0
        private void CreateDirectory(string filepath, WebDavClient webDav)
        {
            var pathItems = filepath.Replace("\\", "/").Split('/');
            var path      = "";

            for (int i = 0; i < pathItems.Length; i++)
            {
                path = path + pathItems[i];
                var propParam = new PropfindParameters();
                RetryIfFail(5, () =>
                {
                    var propFind = webDav.PropFind(new Uri($"{webDav.BaseAddress}{path}"), propParam);
                    if (!propFind.IsSuccessful)
                    {
                        if (propFind.StatusCode != 404)
                        {
                            throw new ApplicationException($"Unable read file props: --> {propFind.StatusCode} {propFind.Description}");
                        }

                        var folder = webDav.Mkcol(new Uri($"{webDav.BaseAddress}{path}"));
                        if (!folder.IsSuccessful)
                        {
                            throw new ApplicationException($"Unable create folder: --> {folder.StatusCode} {folder.Description}");
                        }
                    }
                    return(propFind);
                });
                path = path + "/";
            }
        }
Esempio n. 2
0
        public DateTime GetLastModified(string filename1)
        {
            var webDav = GetWebDavClient();
            var yaFile = new YandexDiscFile(filename1, Location, webDav);

            var propParams = new PropfindParameters();

            propParams.Namespaces.Add(new NamespaceAttr("u", "DataSync"));
            propParams.CustomProperties.Add(XName.Get("x-lastmodified", "DataSync"));

            var propFind = RetryIfFail(5, () =>
            {
                var propFind1 = webDav.PropFind(yaFile.Uri, propParams);
                if (!propFind1.IsSuccessful)
                {
                    throw new ApplicationException($"Unable read file props: --> {propFind1.StatusCode} {propFind1.Description}");
                }
                return(propFind1);
            });

            var resource = propFind
                           .Resources
                           .FirstOrDefault(m => m.Uri == $"/{yaFile.Location}");

            var prop = resource?.Properties.FirstOrDefault(m => m.Name.NamespaceName == "DataSync" && m.Name.LocalName == "x-lastmodified");

            if (prop != null && !string.IsNullOrEmpty(prop.Value))
            {
                return(DateTime.Parse(prop.Value).ToUniversalTime());
            }

            return(DateTime.MinValue);
        }
Esempio n. 3
0
        public static async Task TestPropfind(WebDavClient webDavClient)
        {
            var propfindParams = new PropfindParameters
            {
                CustomProperties = new XName[] { "testprop" },
                Namespaces       = new[] { new NamespaceAttr("http://example.com") }
            };
            var response = await webDavClient.Propfind("http://mywebdav:88", propfindParams);

            Console.WriteLine(response.ToString());
            foreach (var res in response.Resources)
            {
                Console.WriteLine("====================================================");
                Console.WriteLine("URI: {0}", res.Uri);
                Console.WriteLine("====================================================");
                foreach (var @lock in res.ActiveLocks)
                {
                    PrintActiveLock(@lock);
                }
                Console.WriteLine("IsCollection: {0}", res.IsCollection);
                Console.WriteLine("IsHidden: {0}", res.IsHidden);
                Console.WriteLine("CreationDate: {0}", res.CreationDate);
                Console.WriteLine("DisplayName: {0}", res.DisplayName);
                Console.WriteLine("ContentLanguage: {0}", res.ContentLanguage);
                Console.WriteLine("ContentLength: {0}", res.ContentLength);
                Console.WriteLine("ContentType: {0}", res.ContentType);
                Console.WriteLine("ETag: {0}", res.ETag);
                Console.WriteLine("LastModifiedDate: {0}", res.LastModifiedDate);
                Console.WriteLine("Properties: {0}", "[\r\n " + string.Join("\r\n ", res.Properties.Select(x => $"{x.Name}: {x.Value}")) + "\r\n]");
                Console.WriteLine("PropertyStatuses: {0}", "[\r\n " + string.Join("\r\n ", res.PropertyStatuses.Select(x => x.ToString())) + "\r\n]");
                Console.WriteLine();
            }
        }
        private async Task <IReadOnlyCollection <WebDavResource> > GetResourcesFromUri(string rootUri)
        {
            using var handler = new HttpClientHandler
                  {
                      ClientCertificateOptions = ClientCertificateOption.Manual,
                      SslProtocols             = SslProtocols.Tls12
                  };

            try
            {
                var certificate = CertificateHelper.GetCertificate(_storeName, _storeLocation, _thumbprint);
                handler.ClientCertificates.Add(certificate);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Fant ikke sertifikat med thumbprint {_thumbprint} StoreName: {_options.Value.StoreName} StoreLocation: {_options.Value.StoreLocation}");
                return(null);
            }

            using var httpClient = new HttpClient(handler)
                  {
                      BaseAddress = new Uri(_baseAddress),
                  };

            using var client = new WebDavClient(httpClient);

            var propfindParameters = new PropfindParameters();

            propfindParameters.ApplyTo = ApplyTo.Propfind.ResourceAndAncestors;

            var result = await client.Propfind(rootUri, propfindParameters);

            return(result.Resources);
        }
Esempio n. 5
0
        public bool IsFileExist(string filename1)
        {
            var webDav = GetWebDavClient();
            var yaFile = new YandexDiscFile(filename1, Location, webDav);

            var propParam = new PropfindParameters();

            RetryIfFail(5, () =>
            {
                var propFind1 = webDav.PropFind(yaFile.Uri, propParam);
                if (!propFind1.IsSuccessful)
                {
                    if (propFind1.StatusCode == 404)
                    {
                        return(false);
                    }

                    throw new ApplicationException($"Unable read file props: --> {propFind1.StatusCode} {propFind1.Description}");
                }
                return(true);
            });
            return(true);
        }