private void SetSongTagsOnUpload(Song currentSong, SongUploadViewModel song, ApplicationDbContext db)
        {
            if (song.Tags == null)
            {
                song.Tags = string.Empty;
            }

            var tagsStringSplit = song.Tags.Split(" ,".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                                  .OrderBy(t => t)
                                  .Select(t => t.ToLower())
                                  .Distinct();

            currentSong.Tags.Clear();

            foreach (var tagString in tagsStringSplit)
            {
                Tag tag = db.Tags.FirstOrDefault(t => t.Name == tagString);

                if (tag == null)
                {
                    tag = new Tag()
                    {
                        Name = tagString
                    };
                    db.Tags.Add(tag);
                }

                currentSong.Tags.Add(tag);
            }
        }
        public ActionResult Upload(SongUploadViewModel song, HttpPostedFileBase file)
        {
            if (file != null)
            {
                var validTypes = new[]
                {
                    "audio/mpeg",
                    "audio/mp3",
                    "audio/wav",
                    "audio/flac",
                    "audio/wv"
                };

                if (validTypes.Contains(file.ContentType))
                {
                    var currentUser    = this.User.Identity.GetUserId();
                    var songsPath      = "~/Uploads/Songs/";
                    var mappedPath     = HttpContext.Server.MapPath(songsPath);
                    var uploadFilename = Path.GetFileName(file.FileName);
                    var randomHash     = Guid.NewGuid().ToString().Substring(0, 6);

                    var fileName = randomHash + "_" + uploadFilename;

                    var absoluteFilePath = mappedPath + fileName;

                    bool isGenreIdValid = ValidateGenre(song.Genre);

                    if (!isGenreIdValid || song.Genre == 0)
                    {
                        this.AddNotification("Invalid genre selected.", NotificationType.ERROR);
                        return(RedirectToAction("Upload"));
                    }

                    var currentSong = new Song
                    {
                        Artist      = song.Artist,
                        Title       = song.Title,
                        Description = song.Description,
                        UploaderId  = currentUser,
                        SongPath    = $"/Uploads/Songs/{fileName}",
                        UploadDate  = DateTime.Now,
                        GenreId     = song.Genre,
                    };

                    this.SetSongTagsOnUpload(currentSong, song, db);

                    if (!Directory.Exists(mappedPath))
                    {
                        Directory.CreateDirectory(mappedPath);
                    }

                    file.SaveAs(absoluteFilePath);

                    db.Songs.Add(currentSong);
                    db.SaveChanges();

                    this.AddNotification($"Song {song.Artist} - {song.Title} has been upload.", NotificationType.INFO);

                    return(RedirectToAction("MySongs", "Songs"));
                }

                this.AddNotification("Invalid file type!", NotificationType.ERROR);
                this.AddNotification("Valid file types:", NotificationType.INFO);
                this.AddNotification("* mpeg", NotificationType.INFO);
                this.AddNotification("* mp3", NotificationType.INFO);
                this.AddNotification("* wav", NotificationType.INFO);
                this.AddNotification("* flac", NotificationType.INFO);
                this.AddNotification("* wv", NotificationType.INFO);

                return(RedirectToAction("Upload"));
            }

            this.AddNotification("Please select file.", NotificationType.ERROR);

            return(RedirectToAction("Upload"));
        }