Example #1
0
        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();
        }
Example #2
0
        public ActionResult CreateSong(SongViewModel collection, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                Song s = new Song();
                s.AddDate = DateTime.Now;
                Album album = db.Albums.Find(collection.SelectedAlbumId);
                s.AlbumName = album.Name;
                Artist artist = db.Artists.Find(collection.SelectedArtistId);
                s.ArtistName = artist.Name;
                s.Genre = collection.Song.Genre;
                s.NumberOfPlays = 0;
                s.Position = -1;
                s.Rating = 0;
                s.Title = collection.Song.Title;
                s.Voters = new List<Voting>();

                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    string path1 = Server.MapPath("/Music/");

                    if (!Directory.Exists(path1 + artist.Name))
                        Directory.CreateDirectory(path1 + artist.Name);

                    if (!Directory.Exists(path1 + artist.Name + "/" + album.Name))
                        Directory.CreateDirectory(path1 + artist.Name + "/" + album.Name);

                    var path = Server.MapPath("/Music/" + artist.Name + "/" + album.Name + "/" + fileName);
                    file.SaveAs(path);

                    s.FilePath = artist.Name + "\\" + album.Name + "\\" + fileName;
                }

                album.Songs.Add(s);
                db.Entry(album).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Album", new { id = album.Id });
            }

            collection.Artists = db.Artists.ToList().Select(x => new SelectListItem
            {
                Text = x.Name,
                Value = x.Id.ToString()
            });

            Artist art = (from s in db.Artists
                          where s.Id == collection.SelectedArtistId
                          select s).FirstOrDefault();
            if (art != null)
            {
                collection.Albums = art.Albums.ToList().Select(x => new SelectListItem
                {
                    Text = x.Name,
                    Value = x.Id.ToString()
                });
            }

            return View(collection);
        }