Пример #1
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);
        }
Пример #2
0
        public void PutFile(string filename1, byte[] bytes, DateTime lastModified, ProgressCallback progressCallback)
        {
            var webDav = GetWebDavClient();
            var yaFile = new YandexDiscFile(filename1, Location, webDav);

            CreateDirectory(Path.GetDirectoryName(yaFile.Location), webDav);

            using (var stream = new MemoryStream(bytes))
            {
                var props = new PutFileParameters();
                props.OperationProgress = args =>
                {
                    progressCallback?.Invoke(args.Progress);
                };

                var response = webDav.PutFile(yaFile.Uri, stream, props);
                if (!response.IsSuccessful)
                {
                    throw new ApplicationException($"Unable save file: --> {response.StatusCode} {response.Description}");
                }
            }

            RetryIfFail(5, () =>
            {
                var name = XName.Get("x-lastmodified", "DataSync");

                var patch = new ProppatchParameters();
                patch.PropertiesToSet.Add(name, lastModified.ToString("u"));
                patch.Namespaces.Add(new NamespaceAttr("u", "DataSync"));

                var propPatch = webDav.Proppatch(yaFile.Uri, patch);
                if (!propPatch.IsSuccessful)
                {
                    throw new ApplicationException($"Unable update file properties: --> {propPatch.StatusCode} {propPatch.Description}");
                }

                return(true);
            });
        }
Пример #3
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);
        }
Пример #4
0
        public byte[] GetFile(string filename1, ProgressCallback progressCallback)
        {
            var webDav = GetWebDavClient();
            var yaFile = new YandexDiscFile(filename1, Location, webDav);

            var props = new GetFileParameters();

            props.OperationProgress = args =>
            {
                progressCallback?.Invoke(args.Progress);
            };

            using (var response = webDav.GetFile(yaFile.Uri, false, props))
            {
                if (!response.IsSuccessful)
                {
                    throw new ApplicationException($"Unable read file: --> {response.StatusCode} {response.Description}");
                }

                var data = new byte[response.Stream.Length];
                response.Stream.Read(data, 0, data.Length);
                return(data);
            }
        }