public async Task <File> Save(JObject data) { dynamic fileDto = data; string oldUrl = ""; int? fileId = fileDto.Id; var file = new File() { Id = fileId ?? 0 }; var currentFile = await _contentManagementContext.Files.AsNoTracking() .SingleOrDefaultAsync(fl => fl.Id == file.Id); try { if (currentFile == null) { throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.FileNotFound)); } oldUrl = currentFile.Url; file = currentFile; file.RowVersion = fileDto.RowVersion; _contentManagementContext.Files.Attach(file); } catch (Exception) { _contentManagementContext.Files.Add(file); } try { file.Guid = fileDto.Guid; } catch (Exception) { file.Guid = SecureGuid.NewGuid(); } string filePathUrl = fileDto.Url; if (filePathUrl.IndexOf(Helper.RootUrl, StringComparison.Ordinal) != 0) { filePathUrl = Helper.RootUrl + filePathUrl; } if (filePathUrl.LastIndexOf(Helper.RootUrl, StringComparison.Ordinal) == filePathUrl.Length - 1) { filePathUrl = filePathUrl.Substring(0, filePathUrl.Length - 1); } file.Name = fileDto.Name; file.Description = fileDto.Description; file.TypeCode = fileDto.TypeCode; var repeatedLink = await _contentManagementContext.FilePaths.Where(fp => fp.Url == filePathUrl).CountAsync(); if ((repeatedLink > 0 && oldUrl == "") || (repeatedLink > 1 && oldUrl == "")) { throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.RepeatedValue, filePathUrl)); } file.ContentType = _fileSystemManager.GetExtension(filePathUrl); file.Url = filePathUrl; file.Size = _fileSystemManager.GetFileSize(filePathUrl); file.Language = Config.DefaultsLanguage; file.Content = _fileSystemManager.FileToByte(filePathUrl); //if(service.IsLeaf) if (currentFile != null) { file.ViewRoleId = currentFile.ViewRoleId; file.ModifyRoleId = currentFile.ModifyRoleId; file.AccessRoleId = currentFile.AccessRoleId; } AuthorizeManager.SetAndCheckModifyAndAccessRole(file, fileDto); file.Status = fileDto.Status; await _contentManagementContext.SaveChangesAsync(); return(file); }