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")); } }
// 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")); } }
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")); } }