public ActionResult Upload() { var uploaded = new List <ImageViewModel>(); var userId = User.Identity.GetUserId(); var user = _context.Users.Find(userId); var images = user.Images; var mode = ViewMode.IsValid(Request.Form["mode"]) ? Request.Form["mode"] : ViewMode.Public; try { Directory.CreateDirectory(Path.Combine(Server.MapPath("~/Uploads/"), userId)); for (int i = 0; i < Request.Files.Count; i++) { var file = Request.Files[i]; var fileName = file.FileName; var imageExists = images.FirstOrDefault(m => m.Name == fileName); var path = Path.Combine(Server.MapPath("~/Uploads/"), User.Identity.GetUserId(), fileName); if (imageExists == null) { var Image = new Image { Name = fileName, Type = mode, Posted = DateTime.Now, HasAlbum = false }; _context.Images.Add(Image); user.Images.Add(Image); file.SaveAs(path); _context.SaveChanges(); uploaded.Add(new ImageViewModel { Id = Image.Id, Name = fileName, Posted = Image.Posted.ToShortTimeString() + "-" + Image.Posted.ToShortDateString(), OwnerId = Image.ApplicationUserId, Mode = Image.Type }); } } } catch (Exception e) { } return(Json(new { files = uploaded.ToArray() })); }
public ActionResult CreateAlbum(string name, string mode, string selected) { var userId = User.Identity.GetUserId(); var user = _context.Users.Find(userId); var selectedArray = selected.Split(',').Select(x => Int32.Parse(x)).ToArray(); if (selectedArray.Length == 0) { return(new HttpStatusCodeResult(HttpStatusCode.Forbidden)); } var album = new Album { Name = !String.IsNullOrEmpty(name) ? name : "Unnamed", Type = ViewMode.IsValid(mode) ? mode : ViewMode.Public }; var images = new List <Image>(); foreach (var index in selectedArray) { var image = user.Images.FirstOrDefault(m => m.Id == index); image.HasAlbum = true; images.Add(image); } album.Images = images; _context.Albums.Add(album); user.Albums.Add(album); _context.SaveChanges(); return(Json(new { Id = album.Id, Name = album.Name, ImageCount = album.Images.Count, Mode = album.Type })); }
public ActionResult UploadAlbum() { var userId = User.Identity.GetUserId(); var user = _context.Users.Find(userId); var images = user.Images; var mode = ViewMode.IsValid(Request.Form["mode"]) ? Request.Form["mode"] : ViewMode.Public; var uploaded = new List <ImageViewModel>(); var temp = new List <Image>(); Album album = new Album(); try { for (int i = 0; i < Request.Files.Count; i++) { var file = Request.Files[i]; var fileName = file.FileName; var imageExists = images.FirstOrDefault(m => m.Name == fileName); var path = Path.Combine(Server.MapPath("~/Uploads/"), User.Identity.GetUserId(), fileName); if (imageExists == null) { var Image = new Image { Name = fileName, Type = ViewMode.Private, Posted = DateTime.Now, HasAlbum = true }; temp.Add(Image); _context.Images.Add(Image); user.Images.Add(Image); _context.SaveChanges(); uploaded.Add(new ImageViewModel { Id = Image.Id, Name = fileName, Posted = Image.Posted.ToShortTimeString() + "-" + Image.Posted.ToShortDateString(), OwnerId = userId, Mode = Image.Type }); file.SaveAs(path); } } album.Name = Request.Form["albumName"]; album.Images = temp; album.Type = mode; _context.Albums.Add(album); user.Albums.Add(album); } catch (Exception e) { } _context.SaveChanges(); return(Json(new { Images = uploaded.ToArray(), ImageCount = uploaded.Count, Id = album.Id, Name = album.Name, Mode = album.Type })); }