Ejemplo n.º 1
0
        public IActionResult GetFile(string id)
        {
            if (HttpContext.Session.GetString("loggedIn") != "yes")
            {
                return(Redirect("/"));
            }

            try
            {
                DownloadTransaction downloadTrans = dataRepository.DownloadTransaction.First(d => d.Token == id);
                Song song = dataRepository.Songs.First(s => s.SongId == downloadTrans.SongId);

                if (downloadTrans == null)
                {
                    return(View("Error"));
                }

                if (!downloadTrans.HasDownloaded && DateTime.Now < downloadTrans.Expiration)
                {
                    try
                    {
                        System.IO.FileStream stream = new System.IO.FileStream("wwwroot/assets/songs/" + song.SongId + ".mp3", System.IO.FileMode.Open);

                        downloadTrans.HasDownloaded = true;
                        dataRepository.UpdateDownloadTransaction(downloadTrans);

                        return(File(stream, "audio/mpeg", song.Artist + " - " + song.Title + ".mp3"));
                    }
                    catch (Exception)
                    {
                        return(View("Error"));
                    }
                }
                else
                {
                    ViewBag.Message = "Link has either expired or you have already downloaded";
                    return(View("Error"));
                }
            }
            catch (Exception)
            {
                return(View("Error"));
            }
        }
Ejemplo n.º 2
0
        // GET: /<controller>/
        public IActionResult Details(string id)
        {
            if (HttpContext.Session.GetString("loggedIn") != "yes")
            {
                return(Redirect("/"));
            }

            try
            {
                Song song = dataRepository.Songs.First(s => s.SongId == id);

                if (song != null)
                {
                    // create dl transaction & add
                    DownloadTransaction downloadTrans = new DownloadTransaction()
                    {
                        SongId     = song.SongId,
                        UserId     = HttpContext.Session.GetString("userId"),
                        Expiration = DateTime.Now.AddMinutes(5.0)
                    };
                    dataRepository.AddDownloadTransaction(downloadTrans);

                    // create view model to be passed to the view
                    DownloadSongViewModel viewModel = new DownloadSongViewModel()
                    {
                        Song  = song,
                        Token = downloadTrans.Token
                    };

                    return(View(viewModel));
                }
                else
                {
                    return(View("Error"));
                }
            }
            catch (Exception)
            {
                // show error page!
                return(View("Error"));
            }
        }
Ejemplo n.º 3
0
        public IActionResult Download(string id)
        {
            if (HttpContext.Session.GetString("loggedIn") != "yes")
            {
                return(Redirect("/"));
            }

            try
            {
                DownloadTransaction downloadTrans = dataRepository.DownloadTransaction.First(d => d.Token == id);

                if (downloadTrans == null)
                {
                    return(View("Error"));
                }

                Song song = dataRepository.Songs.First(s => s.SongId == downloadTrans.SongId);

                // check expiration
                if (DateTime.Now > downloadTrans.Expiration)
                {
                    ViewBag.Expired = true;
                }
                else
                {
                    ViewBag.Expired = false;
                }

                // create view model to be passed to the view
                DownloadSongViewModel viewModel = new DownloadSongViewModel()
                {
                    Song  = song,
                    Token = downloadTrans.Token
                };
                return(View(viewModel));
            }
            catch (Exception)
            {
                return(View("Error"));
            }
        }