Beispiel #1
0
        public static async Task <ServerStatus> GetServerStatusAsync(string input)
        {
            var url = _BuildStatusUrl(input);

            if (url == null)
            {
                return(null);
            }
            WebDavRequest request  = new WebDavRequest(new NetworkCredential(string.Empty, string.Empty), url, HttpMethod.Get);
            var           response = await request.SendAsync();

            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();

                Debug.WriteLine($"Serverstatus response: {content}");
                try
                {
                    var status = Newtonsoft.Json.JsonConvert.DeserializeObject <ServerStatus>(content);
                    status.ResponseCode = response.StatusCode.ToString();
                    return(status);
                }
                catch (JsonException)
                {
                    return(new ServerStatus()
                    {
                        ResponseCode = HttpStatusCode.NotFound.ToString()
                    });
                }
            }
            return(new ServerStatus()
            {
                ResponseCode = response.StatusCode.ToString()
            });
        }
        public async Task <bool> Exists(Uri url)
        {
            if (!url.IsAbsoluteUri)
            {
                url = new Uri(_serverUrl, url);
            }
            var delRequest = new WebDavRequest(_credential, url, HttpMethod.Head);
            var response   = await delRequest.SendAsync();

            return(response.StatusCode == HttpStatusCode.Ok);
        }
        public async Task Delete(Uri url)
        {
            if (!url.IsAbsoluteUri)
            {
                url = new Uri(_serverUrl, url);
            }
            var delRequest = new WebDavRequest(_credential, url, HttpMethod.Delete);
            var response   = await delRequest.SendAsync();

            if (response.IsSuccessStatusCode)
            {
                return;
            }
            throw new WebDavException(response.StatusCode, response.ReasonPhrase, null);
        }
Beispiel #4
0
        public async Task <HttpStatusCode> CheckUserLoginAsync()
        {
            var url     = GetWebDavUrl(_serverUrl.ToString());
            var request = new WebDavRequest(_credential, new Uri(url), HttpMethod.Head);

            Windows.Web.Http.HttpResponseMessage response;
            try
            {
                response = await request.SendAsync();
            }
            catch (Exception)
            {
                return(HttpStatusCode.SeeOther);
            }
            return(response.StatusCode);
        }
        public async Task <List <DavItem> > ListFolder(Uri url)
        {
            if (!url.IsAbsoluteUri)
            {
                url = new Uri(_serverUrl, url);
            }
            var propRequest = new WebDavRequest(_credential, url, new HttpMethod("PROPFIND"));
            var response    = await propRequest.SendAsync();

            if (response.IsSuccessStatusCode)
            {
                var inputStream = await response.Content.ReadAsInputStreamAsync();

                return(XmlParser.ParsePropfind(inputStream.AsStreamForRead()));
            }
            throw new WebDavException(response.StatusCode, response.ReasonPhrase, null);
        }
        public async Task <DavItem> CreateFolder(Uri url)
        {
            if (!url.IsAbsoluteUri)
            {
                url = new Uri(_serverUrl, url);
            }
            var mkcolRequest = new WebDavRequest(_credential, url, new HttpMethod("MKCOL"));
            var response     = await mkcolRequest.SendAsync();

            if (response.IsSuccessStatusCode)
            {
                var items = await ListFolder(url);

                return(items.FirstOrDefault());
            }
            throw new WebDavException(response.StatusCode, response.ReasonPhrase, null);
        }
        public async Task Download(Uri url, StorageFile targetFile, CancellationToken token, Progress <HttpProgress> progress)
        {
            if (!url.IsAbsoluteUri)
            {
                url = new Uri(_serverUrl, url);
            }
            WebDavRequest request  = new WebDavRequest(_credential, url, HttpMethod.Get);
            var           response = await request.SendAsync(token, progress);

            if (response.IsSuccessStatusCode)
            {
                var inputStream = await response.Content.ReadAsInputStreamAsync();

                IBuffer buffer = new Buffer(16 * 1024);

                using (var writingStream = await targetFile.OpenStreamForWriteAsync())
                {
                    ulong received = 0;
                    do
                    {
                        buffer = await inputStream.ReadAsync(buffer, buffer.Capacity, InputStreamOptions.ReadAhead).AsTask(token);

                        received += buffer.Length;
                        await writingStream.WriteAsync(buffer.ToArray(), 0, (int)buffer.Length, token);

                        ((IProgress <HttpProgress>)progress).Report(new HttpProgress
                        {
                            BytesReceived       = received,
                            TotalBytesToReceive = response.Content.Headers.ContentLength,
                            Stage = HttpProgressStage.ReceivingContent
                        });
                        if (token.IsCancellationRequested)
                        {
                            return;
                        }
                    } while (buffer.Length > 0);
                }
            }
            else
            {
                throw new WebDavException(response.StatusCode, response.ReasonPhrase, null);
            }
        }
        public async Task <DavItem> Upload(Uri url, StorageFile file, CancellationToken token, Progress <HttpProgress> progress)
        {
            if (!url.IsAbsoluteUri)
            {
                url = new Uri(_serverUrl, url);
            }
            var stream = await file.OpenStreamForReadAsync();

            var postRequest = new WebDavRequest(_credential, url, HttpMethod.Put, stream);
            var response    = await postRequest.SendAsync(token, progress);

            if (response.IsSuccessStatusCode)
            {
                var items = await ListFolder(url);

                return(items.FirstOrDefault());
            }
            throw new WebDavException(response.StatusCode, response.ReasonPhrase, null);
        }
        public async Task Move(Uri sourceUrl, Uri destinationUrl)
        {
            if (!sourceUrl.IsAbsoluteUri)
            {
                sourceUrl = new Uri(_serverUrl, sourceUrl);
            }
            if (!destinationUrl.IsAbsoluteUri)
            {
                destinationUrl = new Uri(_serverUrl, destinationUrl);
            }
            var headers = new Dictionary <string, string> {
                { "Destination", destinationUrl.ToString() }
            };
            var moveRequest = new WebDavRequest(_credential, sourceUrl, new HttpMethod("MOVE"), Stream.Null, headers);
            var response    = await moveRequest.SendAsync();

            if (response.IsSuccessStatusCode)
            {
                return;
            }
            throw new WebDavException(response.StatusCode, response.ReasonPhrase, null);
        }