public ActionResult Create(Gallery gallery, string path, string[] ids, string[] titles, string[] descriptions, string[] filenames) { if (ModelState.IsValid) { // Gallery ID int id = GetNextGalleryId(); // Check and move images var imagesPath = Server.MapPath("~/Images/Galleries/tempGallery"); var copyPath = Server.MapPath("~/Images/Galleries/" + id.ToString()); CreateCopyPath(copyPath); MoveFilesCreateImages(gallery, ids, titles, descriptions, filenames, imagesPath, copyPath); gallery.Category = GenerateGalleryCategories(path); db.GallerySet.Add(gallery); db.SaveChanges(); SendMessage(MessageType.Success, "The gallery successfully created!"); return RedirectToAction("Index"); } SendMessage(MessageType.Error, "Something went wrong! :( Try it again"); return View(gallery); }
public ActionResult Create(Gallery gallery, string path, string[] ids, string[] titles, string[] descriptions, string[] filenames) { if (ModelState.IsValid) { // Gallery ID int id; try { id = db.GallerySet.OrderByDescending(e => e.Id).Select(e => e.Id).First() + 1; } catch (Exception) { id = 0; } // Check and move images var imagesPath = Server.MapPath("~/Images/Galleries/tempGallery"); var copyPath = Server.MapPath("~/Images/Galleries/" + id.ToString()); { //Create directory if (!Directory.Exists(copyPath)) { Directory.CreateDirectory(copyPath); } } { //Move files and create image objects var files = Directory.GetFiles(imagesPath); foreach (var file in files) { for (int i = 0; i < ids.Length; i++) { if (filenames[i] == Path.GetFileName(file)) { var image = new Image { Description = descriptions[i], Title = titles[i], ImageId = int.Parse(ids[i]), Gallery = gallery }; db.ImageSet.Add(image); gallery.Images.Add(image); var destSource = Path.Combine(copyPath, ids[i]); System.IO.File.Move(file, destSource); } } } } gallery.Category = GenerateGalleryCategories(path); db.GallerySet.Add(gallery); db.SaveChanges(); SendMessage(MessageType.Success, "The gallery successfully created!"); return RedirectToAction("Index"); } SendMessage(MessageType.Error, "Something went wrong! :( Try it again"); return View(gallery); }
public ActionResult Edit(Gallery gallery, string path, string[] ids, string[] titles, string[] descriptions, string[] filenames) { if (ModelState.IsValid) { // Gallery ID var id = gallery.Id; // Check and move images var imagesPath = Server.MapPath("~/Images/Galleries/tempGallery"); var copyPath = Server.MapPath("~/Images/Galleries/" + id.ToString()); { //Create directory if (!Directory.Exists(copyPath)) { Directory.CreateDirectory(copyPath); } } { //Move files and create image objects var files = Directory.GetFiles(imagesPath); foreach (var file in files) { for (int i = 0; i < ids.Length; i++) { if (filenames[i] == Path.GetFileName(file)) { var image = new Image { Description = descriptions[i], Title = titles[i], ImageId = int.Parse(ids[i]), Gallery = gallery }; db.ImageSet.Add(image); gallery.Images.Add(image); var destSource = Path.Combine(copyPath, ids[i]); System.IO.File.Move(file, destSource); } } } //Delete deleted object's file files = Directory.GetFiles(copyPath); foreach (var file in files) { if (!ids.Contains(Path.GetFileName(file))) System.IO.File.Delete(file); } } gallery.Category = GenerateGalleryCategories(path); DeleteNotUsingCategories(); db.Entry(gallery).State = EntityState.Modified; db.SaveChanges(); SendMessage(MessageType.Success, "The gallery successfully modified!"); return RedirectToAction("Index"); } SendMessage(MessageType.Error, "Something went wrong! :( Try it again"); return View(gallery); }
private void MoveFilesCreateImages(Gallery gallery, string[] ids, string[] titles, string[] descriptions, string[] filenames, string imagesPath, string copyPath) { //Move files and create image objects var files = Directory.GetFiles(imagesPath); foreach (var file in files) { for (int i = 0; i < ids.Length; i++) { if (filenames[i] == Path.GetFileName(file)) { var image = new Image { Description = descriptions[i], Title = titles[i], ImageId = int.Parse(ids[i]), Gallery = gallery }; db.ImageSet.Add(image); gallery.Images.Add(image); var destSource = Path.Combine(copyPath, ids[i]); try { System.IO.File.Move(file, destSource); } catch (Exception) { SendMessage(MessageType.Warning, "Some picture did not uploaded correctly! Try them again!"); } } } } }
private static string GenerateGalleryPath(Gallery gallery) { string path = ""; GalleryCategory category = gallery.Category; while (category != null) { path = "/" + category.Title + path; category = category.Parent; } return path; }
public ActionResult Edit(Gallery gallery, string path, string[] ids, string[] titles, string[] descriptions, string[] filenames) { if (ModelState.IsValid) { // Gallery ID var id = gallery.Id; // Check and move images var imagesPath = Server.MapPath("~/Images/Galleries/tempGallery"); var copyPath = Server.MapPath("~/Images/Galleries/" + id.ToString()); CreateCopyPath(copyPath); MoveFilesCreateImages(gallery, ids, titles, descriptions, filenames, imagesPath, copyPath); DeleteDeletedObjectFiles(ids, copyPath); var category = GenerateGalleryCategories(path); gallery.Category = category; gallery.GalleryCategoryId = category.Id; db.Entry(gallery).State = EntityState.Modified; db.SaveChanges(); DeleteNotUsingCategories(); db.SaveChanges(); SendMessage(MessageType.Success, "The gallery successfully modified!"); return RedirectToAction("Index"); } SendMessage(MessageType.Error, "Something went wrong! :( Try it again"); return View(gallery); }