/// <summary> /// Update file contents if changed on server /// </summary> /// <param name="file"></param> /// <param name="useRange">Use http range header</param> /// <returns>true if the file was modified</returns> public async Task <bool> UpdateFile(WebDAVFile file, bool useRange = true) { if (!file.HasContents) { file.Contents = (await GET(file.URI)).Contents; return(true); } else { var client = GetClient(); var request = new HttpRequestMessage(HttpMethod.Get, file.URI); if (useRange) { request.Headers.Range = new RangeHeaderValue(file.Length, null); } if (file.ETag != null) { request.Headers.TryAddWithoutValidation("If-None-Match", file.ETag); } var result = await client.SendAsync(request); if (result.StatusCode == HttpStatusCode.NotModified || result.StatusCode == HttpStatusCode.RequestedRangeNotSatisfiable) { return(false); } if (!result.IsSuccessStatusCode && result.StatusCode != HttpStatusCode.NotModified) { throw new HttpRequestException("Response does not indicate success: " + result.StatusCode); } var body = await result.Content.ReadAsByteArrayAsync(); if (body.Length == 0) { return(false); } var newContents = new byte[body.Length + file.Contents.Length]; Buffer.BlockCopy(file.Contents, 0, newContents, 0, file.Contents.Length); Buffer.BlockCopy(body, 0, newContents, file.Contents.Length, body.Length); file.Contents = newContents; file.Length = newContents.Length; return(true); } }
private WebDAVFile FileFromXML(XElement element) { var prop = element.Element(DAV + "propstat")?.Element(DAV + "prop"); var file = new WebDAVFile { URI = element.Element(DAV + "href")?.Value, Exists = true, Contents = null, CreationDate = DateTime.Parse(prop?.Element(DAV + "creationdate")?.Value), LastModifiedDate = DateTime.Parse(prop?.Element(DAV + "getlastmodified")?.Value), Filename = prop?.Element(DAV + "displayname")?.Value, Length = prop?.Element(DAV + "getcontentlength") != null ? Int32.Parse(prop?.Element(DAV + "getcontentlength")?.Value) : 0, ContentType = prop?.Element(DAV + "getcontenttype")?.Value, ETag = prop?.Element(DAV + "getetag")?.Value }; file.IsFile = prop?.Element(DAV + "resourcetype")?.Element(DAV + "collection") == null; return(file); }