public ActionResult Edit(Content content, ICollection<SelectListItem> categories) { if (ModelState.IsValid) { int[] categoryID = categories.Where<SelectListItem>(x => x.Selected).Select<SelectListItem, int>(x => Convert.ToInt32(x.Value)).ToArray<int>(); Content contentToUpdate = unitOfWork.ContentRepository.Get(x => x.ContentID == content.ContentID, includeProperties: "Categories").FirstOrDefault(); contentToUpdate.MediaFolderID = content.MediaFolderID; contentToUpdate.ContentID = content.ContentID; contentToUpdate.Extension = content.Extension; contentToUpdate.RelativePath = content.RelativePath; contentToUpdate.FileName = content.FileName; contentToUpdate.ContentType = content.ContentType; contentToUpdate.Type = content.Type; contentToUpdate.Title = content.Title; contentToUpdate.Width = content.Width; contentToUpdate.Height = content.Height; contentToUpdate.Data = content.Data; contentToUpdate.Description = content.Description; contentToUpdate.Categories.Clear(); List<Category> allCategories = unitOfWork.CategoryRepository.Get(x => categoryID.Contains<int>(x.CategoryID)).ToList<Category>(); foreach (var category in allCategories) { contentToUpdate.Categories.Add(category); } unitOfWork.ContentRepository.context.Entry(contentToUpdate).State = System.Data.Entity.EntityState.Modified; unitOfWork.Save(); return RedirectToAction("Index"); } ViewBag.MediaManagerCategory = mediaManagerCategory.GetSelectListOfCategories(); return View(content); }
// test multiple file uploade public void ProcessContent(int uploadFolderID) { FileInfo[] uploadFolderContent = null; //List<DirectoryInfo> mediaFolderDirectoryInfo = new List<DirectoryInfo>(); List<MediaFolder> mediaFoldersForUploadFolders = unitOfWork.MediaFolderRepository.Get(x => (x.UploadFolderID == uploadFolderID) || (uploadFolderID == 0)).ToList<MediaFolder>(); List<Category> catagories = unitOfWork.CategoryRepository.Get().ToList<Category>(); List<Content> newContentToAdd = new List<Content>(); List<AliasImage> aliasImages = new List<AliasImage>(); List<ContentAlia> newContentAliasesToAdd = new List<ContentAlia>(); List<ContentAlias> contentAliases; bool hasToUseResizer = false; string newFileName, newImageFullPath, newAliasName, newAliasImageFullPath; ResizeSettings resizeSettings = null; int[] catagoryIDs = null; //XElement mediaFolderProperties = null; MediaFolderPropertiesXML mediaFolderPropertiesXML = null;// new MediaFolderPropertiesXML(w); //Uri mediaFolderUri; //Move the content from the upload folder to the media folder foreach (var mediaFolder in mediaFoldersForUploadFolders) { //get the content in the upload folder for this media folder uploadFolderContent = GetContentInUploadFolder(mediaFolder.UploadFolderID); //mediaFolderProperties = null; mediaFolderPropertiesXML = null; hasToUseResizer = false; //process the XML config data if (mediaFolder.ContentSetupXML != null) { int wTemp, hTemp; mediaFolderPropertiesXML = new MediaFolderPropertiesXML(mediaFolder.ContentSetupXML); //mediaFolderProperties = XElement.Parse(mediaFolder.ContentSetupXML); resizeSettings = new ResizeSettings(); resizeSettings.Mode = FitMode.Max; //properties //widthTemp = mediaFolderProperties.Element("property").Element("size").Element("width").FirstNode.ToString(); //heightTemp = mediaFolderProperties.Element("property").Element("size").Element("height").FirstNode.ToString(); if (mediaFolderPropertiesXML.TryToGetImageWidth(out wTemp)) { hasToUseResizer = true; resizeSettings.MaxWidth = wTemp; } if (mediaFolderPropertiesXML.TryToGetImageHeight(out hTemp)) { hasToUseResizer = true; resizeSettings.MaxHeight = hTemp; } //caegories catagoryIDs = mediaFolderPropertiesXML.GetAllCategories(); //Content Aliases //foreach (XElement alias in mediaFolderProperties.Element("contentaliases").Elements("alias")) //{ // AliasImage newAlias = new AliasImage(); // newAlias.AliasName = alias.Element("name").Value; // widthTemp = alias.Element("property").Element("size").Element("width").FirstNode.ToString(); // heightTemp = alias.Element("property").Element("size").Element("height").FirstNode.ToString(); // newAlias.ResizeSettings = new ResizeSettings(); // if (Int32.TryParse(widthTemp, out wTemp)) // { // newAlias.ResizeSettings.MaxWidth = wTemp; // } // if (Int32.TryParse(widthTemp, out hTemp)) // { // newAlias.ResizeSettings.MaxHeight = hTemp; // } // aliasImages.Add(newAlias); //} } foreach (var folderContent in uploadFolderContent) { newFileName = Guid.NewGuid().ToString("D"); newImageFullPath = mediaFolder.MediaFolderDirectoryInfo.FullName + "\\" + newFileName + folderContent.Extension; if (hasToUseResizer) { ImageBuilder.Current.Build(folderContent.FullName, newImageFullPath, resizeSettings); } else { folderContent.CopyTo(newImageFullPath, true); } //save the content in the database Content newContent = new Content(); newContent.FileName = newFileName; newContent.Extension = folderContent.Extension; newContent.RelativePath = (ContentFolder.contentFolderURI.MakeRelativeUri(new Uri(newImageFullPath)).ToString()); newContent.MediaFolderID = mediaFolder.MediaFolderID; newContent.LastModified = DateTimeOffset.Now; //category assignment if (catagoryIDs != null) { //int catagoryID = 0; foreach (var categoryID in catagoryIDs) { newContent.Categories.Add(catagories.FirstOrDefault<Category>(x => x.CategoryID == categoryID)); } } if (mediaFolderPropertiesXML.TryGetContentAliases(out contentAliases)) { foreach (var alias in contentAliases) { newAliasName = Guid.NewGuid().ToString("D"); newAliasImageFullPath = mediaFolder.MediaFolderDirectoryInfo.FullName + "\\" + newAliasName + folderContent.Extension; ResizeSettings resizeSettingsAlias = null; if (alias.Width + alias.Height > 0) { resizeSettingsAlias = new ResizeSettings(); } if (alias.Width != 0) { resizeSettingsAlias.MaxWidth = alias.Width; } if (alias.Height != 0) { resizeSettingsAlias.MaxHeight = alias.Height; } if (resizeSettingsAlias != null) { ImageBuilder.Current.Build(folderContent.FullName, newAliasImageFullPath, resizeSettingsAlias); } else { folderContent.CopyTo(newAliasImageFullPath, true); } //save the contents alias in the database Content newContentAlias = new Content(); newContentAlias.FileName = newAliasName; newContentAlias.Title = alias.Name; newContentAlias.Extension = folderContent.Extension; newContentAlias.RelativePath = (ContentFolder.contentFolderURI.MakeRelativeUri(new Uri(newAliasImageFullPath)).ToString()); newContentAlias.MediaFolderID = mediaFolder.MediaFolderID; newContentAlias.IsAlias = true; newContentAlias.LastModified = DateTimeOffset.Now; //category assignment if (newContent.Categories != null) { foreach (var category in newContent.Categories) { newContentAlias.Categories.Add(category); } } newContentAlias.IsActive = true; newContentToAdd.Add(newContentAlias); newContent.ContentAlias.Add(new ContentAlia() { Content = newContent, Content1 = newContentAlias, AliasName = alias.Name}); } } newContent.IsActive = true; newContentToAdd.Add(newContent); } } if (newContentToAdd != null && newContentToAdd.Count > 0) { unitOfWork.ContentRepository.InsertMultiple(newContentToAdd); unitOfWork.Save(); DeleteContentFromUploadFolder(uploadFolderID); } }