public ActionResult CreateSongFromTags(HttpPostedFileBase file) { if (ModelState.IsValid) { if (file != null && file.ContentLength > 0) { string cover = "/Content/images/covers/unknown_album.png"; string picture = "/Content/images/artists/unknown_artist.png"; var fileName = Path.GetFileName(file.FileName); string path1 = Server.MapPath("/Music/"); file.SaveAs(path1 + "/file1.mp3"); TagLib.File f = TagLib.File.Create(path1 + "/file1.mp3"); string albumName = f.Tag.Album; string artistName = f.Tag.FirstPerformer; if (!Directory.Exists(path1 + artistName)) Directory.CreateDirectory(path1 + artistName); if (!Directory.Exists(path1 + artistName + "/" + albumName)) Directory.CreateDirectory(path1 + artistName + "/" + albumName); var path = Server.MapPath("/Music/" + artistName + "/" + albumName + "/" + fileName); file.SaveAs(path); string filePath = artistName + "\\" + albumName + "\\" + fileName; Song s = new Song() { Title = f.Tag.Title, ArtistName = artistName, AlbumName = albumName, Genre = f.Tag.FirstGenre, FilePath = filePath, AddDate = DateTime.Now, Rating = 0, Voters = new List<Voting>(), NumberOfPlays = 0, Position = -1 }; List<Song> Songs = new List<Song>(); Songs.Add(s); Album Album = new Album() { Name = albumName, ArtistName = artistName, Position = -1, PositionImg = "", Cover = cover, Songs = Songs }; List<Album> Albums = new List<Models.Album>(); Albums.Add(Album); Artist artist = (from a in db.Artists where a.Name == artistName select a).FirstOrDefault(); if (artist == null) { artist = new Artist() { Name = artistName, Position = -1, PositionImg = "", Picture = picture, Albums = Albums }; db.Artists.Add(artist); } else { Album aa = (from a in artist.Albums where a.Name == albumName select a).FirstOrDefault(); if (aa == null) { artist.Albums.Add(Album); db.Entry(artist).State = System.Data.EntityState.Modified; } else { aa.Songs.Add(s); db.Entry(aa).State = System.Data.EntityState.Modified; } } db.SaveChanges(); Album aaa = (from a in db.Albums where a.Name == albumName select a).FirstOrDefault(); return RedirectToAction("Album", new { id = aaa.Id }); } } return View(); }
public ActionResult EditArtist(Artist artist, HttpPostedFileBase file) { Artist a = artist; if (artist != null) { a = db.Artists.Find(artist.Id); a.Name = artist.Name; } if (ModelState.IsValid) { if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); string path1 = Server.MapPath("/Content/images/artists/"); if (!Directory.Exists(path1 + artist.Name)) Directory.CreateDirectory(path1 + artist.Name); var path = Server.MapPath("/Content/images/artists/" + artist.Name + "/" + fileName); file.SaveAs(path); a.Picture = "/Content/images/artists/" + artist.Name + "/" + fileName; } foreach (var item in a.Albums) { foreach (var item1 in item.Songs) { item1.ArtistName = a.Name; } item.ArtistName = a.Name; } db.Entry(a).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Artist", new { artist = a.Name }); } return View(a); }
public ActionResult CreateArtist(Artist artist, HttpPostedFileBase file) { if (ModelState.IsValid) { if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); string path1 = Server.MapPath("/Content/images/artists/"); if (!Directory.Exists(path1 + artist.Name)) Directory.CreateDirectory(path1 + artist.Name); var path = Server.MapPath("/Content/images/artists/" + artist.Name + "/" + fileName); file.SaveAs(path); artist.Picture = "/Content/images/artists/" + artist.Name + "/" + fileName; } else artist.Picture = "/Content/images/artists/unknown_artist.png"; artist.Position = -1; artist.Albums = new List<Album>(); db.Artists.Add(artist); db.SaveChanges(); return RedirectToAction("Artist", new { artist = artist.Name }); } return View(artist); }