/// <summary> /// Updates an existing COR from a PUT when a "If-Match" header is included using the corresponding etag. /// </summary> /// <param name="propertiesAndHeaders"></param> /// <param name="response"></param> private async Task UpdateCalendarObjectResource(Dictionary <string, string> propertiesAndHeaders, HttpResponse response) { #region Extracting Properties string url; propertiesAndHeaders.TryGetValue("url", out url); string body; propertiesAndHeaders.TryGetValue("body", out body); //var headers = response.GetTypedHeaders(); #endregion //var iCal = new VCalendar(body); //Fill the resource //var resource = FillResource(propertiesAndHeaders, iCal, response); var etag = $"\"{Guid.NewGuid()}\""; response.Headers["etag"] = etag; //headers.ETag = new EntityTagHeaderValue(etag, false); var errorStack = new Stack <string>(); //updating the etag await _resourceRespository.CreateOrModifyProperty(url, "getetag", _namespacesSimple["D"], $"<D:getetag {_namespaces["D"]}>{etag}</D:getetag>", errorStack, true); //updating the ctag await _collectionRespository.CreateOrModifyProperty( url?.Remove(url.LastIndexOf("/", StringComparison.Ordinal) + 1), "getctag", _namespacesSimple["S"], $@"<S:getctag {_namespaces["S"]} >{Guid.NewGuid()}</S:getctag>", errorStack, true); //updating the lastmodified await _resourceRespository.CreateOrModifyProperty(url, "getlastmodified", _namespacesSimple["D"], $"<D:getlastmodified {_namespaces["D"]}>{DateTime.Now}</D:getlastmodified>", errorStack, true); //Removing old File StorageManagement.DeleteCalendarObjectResource(url); //Adding New File await StorageManagement.AddCalendarObjectResourceFile(url, body); await _resourceRespository.CreateOrModifyProperty(url, "getcontentlength", "DAV:", $"<D:getcontentlength {_namespaces["D"]}>{StorageManagement.GetFileSize(url)}</D:getcontentlength>", errorStack, true); //the response for this methos is NO CONTENT response.StatusCode = (int)HttpStatusCode.NoContent; //Adding to the dataBase await _resourceRespository.SaveChangeAsync(); }
public async Task <bool> DeleteCalendarObjectResource(Dictionary <string, string> propertiesAndHeaders, HttpResponse response) { #region Extracting Properties string url; propertiesAndHeaders.TryGetValue("url", out url); string ifmatch; var ifMatchEtags = new List <string>(); propertiesAndHeaders.TryGetValue("If-Match", out ifmatch); if (ifmatch != null) { ifMatchEtags = ifmatch.Split(',').ToList(); } #endregion //if the collection doesnt exist in the user folder // the can't do anything var collectionUrl = url?.Remove(url.LastIndexOf("/", StringComparison.Ordinal) + 1); if (!StorageManagement.ExistCalendarCollection(collectionUrl) && !await _collectionRespository.Exist(collectionUrl)) { return(true); } var resource = _resourceRespository.Get(url); if (ifMatchEtags.Count > 0) { if (resource != null) { var resourceEtag = XmlTreeStructure.Parse(resource.Properties.FirstOrDefault(x => x.Name == "getetag")?.Value) .Value; if (resourceEtag != null && ifMatchEtags.Contains(resourceEtag)) { response.StatusCode = (int)HttpStatusCode.NoContent; await _resourceRespository.Remove(resource); //updating the ctag var stack = new Stack <string>(); await _collectionRespository.CreateOrModifyProperty(collectionUrl, "getctag", _namespacesSimple["S"], $@"<S:getctag {_namespaces["S"]} >{Guid.NewGuid()}</S:getctag>", stack, true); return(StorageManagement.DeleteCalendarObjectResource(url)); } } } if (resource != null) { response.StatusCode = (int)HttpStatusCode.NoContent; await _resourceRespository.Remove(resource); //updating the ctag var stack = new Stack <string>(); await _collectionRespository.CreateOrModifyProperty(collectionUrl, "getctag", _namespacesSimple["S"], $@"<S:getctag {_namespaces["S"]} >{Guid.NewGuid()}</S:getctag>", stack, true); return(StorageManagement.DeleteCalendarObjectResource(url)); } return(StorageManagement.DeleteCalendarObjectResource(url)); }