public void CreateMusic(Music music)
 {
     db.Musics.Add(music);
     db.SaveChanges();
 }
示例#2
0
        public ActionResult Create(Music music)
        {
            try
            {

                if (Session["UserAccountID"] == null)
                    return RedirectToAction("Validate", "Login");
                User user = (User)Session["User"];
                ViewData["LoginInfo"] = Utility.BuildUserAccountString(user.Username, Convert.ToString(Session["UserAccountName"]));
                if (user.IsAdmin)
                    ViewData["txtIsAdmin"] = "true";
                else
                    ViewData["txtIsAdmin"] = "false";

                if (ModelState.IsValid)
                {
                    // Set NULLs to Empty Strings
                    music = FillNulls(music);
                    music.AccountID = Convert.ToInt32(Session["UserAccountID"]);
                    Guid fileguid = Guid.NewGuid();

                    if (Request.Form["lstFile"] != null && !String.IsNullOrEmpty(Request.Form["lstFile"].ToString().Trim()))
                    {
                        music.OriginalFilename = Request.Form["lstFile"].ToString().Trim();
                        if (music.OriginalFilename != "0")
                        {
                            string extension = music.OriginalFilename.Substring(music.OriginalFilename.LastIndexOf('.'));
                            music.StoredFilename = fileguid + extension;
                        }
                        else
                        {
                            music.OriginalFilename = String.Empty;
                        }
                    }
                    else
                    {
                        music.OriginalFilename = String.Empty;
                        music.StoredFilename = String.Empty;
                    }

                    string validation = ValidateInput(music);
                    if (!String.IsNullOrEmpty(validation))
                    {
                        ViewData["ValidationMessage"] = validation;
                        ViewData["FileList"] = new SelectList(BuildFileList(Request.Form["lstFile"].ToString().Trim()), "Value", "Text", music.OriginalFilename);
                        ViewData["MusicURL"] = selectedfile;
                        return View(music);
                    }
                    else
                    {
                        try
                        {
                            // Move the music
                            string oldmusic = Server.MapPath(@"~/UploadedFiles");
                            if (!oldmusic.EndsWith(@"\"))
                                oldmusic += @"\";
                            oldmusic += Convert.ToString(Session["UserAccountID"]) + @"\Music\" + music.OriginalFilename;

                            string newmusic = Server.MapPath(@"~/Media");
                            if (!newmusic.EndsWith(@"\"))
                                newmusic += @"\";
                            newmusic += Convert.ToString(Session["UserAccountID"]) + @"\Music\" + music.StoredFilename;

                            System.IO.File.Copy(oldmusic, newmusic);
                            System.IO.File.Delete(oldmusic);
                        }
                        catch
                        {
                            ViewData["ValidationMessage"] = "Failed to copy file.";
                            ViewData["FileList"] = new SelectList(BuildFileList(Request.Form["lstFile"].ToString().Trim()), "Value", "Text", music.OriginalFilename);
                            ViewData["MusicURL"] = selectedfile;
                            return View(music);
                        }

                        repository.CreateMusic(music);

                        CommonMethods.CreateActivityLog((User)Session["User"], "Music", "Add",
                                "Added music '" + music.MusicName + "' - ID: " + music.MusicID.ToString());

                        return RedirectToAction("Index");
                    }
                }

                return View(music);
            }
            catch (Exception ex)
            {
                Helpers.SetupApplicationError("Music", "Create POST", ex.Message);
                return RedirectToAction("Index", "ApplicationError");
            }
        }
 public void UpdateMusic(Music music)
 {
     db.Entry(music).State = EntityState.Modified;
     db.SaveChanges();
 }
示例#4
0
        private string ValidateInput(Music music)
        {
            if (music.AccountID == 0)
                return "Account ID is not valid.";

            if (music.OriginalFilename == "0" || String.IsNullOrEmpty(music.OriginalFilename))
                return "You must select a valid music file.";

            if (String.IsNullOrEmpty(music.MusicName))
                return "Music Name is required.";

            if (String.IsNullOrEmpty(music.OriginalFilename))
                return "Original Filename is required.";

            if (String.IsNullOrEmpty(music.StoredFilename))
                return "Stored Filename is required.";

            return String.Empty;
        }
示例#5
0
        private Music FillNulls(Music music)
        {
            if (music.Tags == null) music.Tags = String.Empty;

            return music;
        }
示例#6
0
        private Music CreateNewMusic()
        {
            Music music = new Music();
            music.MusicID = 0;
            music.AccountID = 0;
            music.OriginalFilename = String.Empty;
            music.StoredFilename = String.Empty;
            music.MusicName = String.Empty;
            music.Tags = String.Empty;
            music.IsActive = true;

            return music;
        }
示例#7
0
        public ActionResult Upload(Music music)
        {
            try
            {

                if (Session["UserAccountID"] == null)
                    return RedirectToAction("Validate", "Login");
                User user = (User)Session["User"];
                ViewData["LoginInfo"] = Utility.BuildUserAccountString(user.Username, Convert.ToString(Session["UserAccountName"]));
                if (user.IsAdmin)
                    ViewData["txtIsAdmin"] = "true";
                else
                    ViewData["txtIsAdmin"] = "false";

                string validation = String.Empty;
                if (ModelState.IsValid)
                {
                    // Only one file is allowed per upload
                    HttpPostedFileBase file = Request.Files[0];
                    if (file != null && file.ContentLength > 0 && !String.IsNullOrEmpty(file.FileName))
                    {
                        if (file.ContentLength > 104000000)
                        {
                            ViewData["ValidationMessage"] = "Uploaded files must be 100MB or less.";
                        }
                        else
                        {
                            string fname = file.FileName.ToLower();
                            if (!fname.EndsWith(".mp3") && !fname.EndsWith(".wma"))
                            {
                                ViewData["ValidationMessage"] = "Only .mp3 and .wma music files can be uploaded.";
                            }
                            else
                            {
                                string filetype = "Music";
                                string filename = Path.GetFileName(file.FileName);
                                string serverpath = Server.MapPath("~/UploadedFiles");
                                if (!serverpath.EndsWith(@"\"))
                                    serverpath += @"\";
                                string path = serverpath + user.AccountID.ToString() + @"\" + filetype + @"\" + filename;
                                if (!System.IO.File.Exists(path))
                                    System.IO.File.Delete(path);
                                file.SaveAs(path);
                            }
                        }
                    }
                    else
                    {
                        ViewData["ValidationMessage"] = "You must select a file.";
                        return View(music);
                    }

                    // Set NULLs to Empty Strings
                    music = FillNulls(music);
                    music.AccountID = Convert.ToInt32(Session["UserAccountID"]);
                    Guid fileguid = Guid.NewGuid();

                    music.OriginalFilename = Path.GetFileName(file.FileName);
                    string extension = music.OriginalFilename.Substring(music.OriginalFilename.LastIndexOf('.'));
                    music.StoredFilename = fileguid + extension;

                    validation = ValidateInput(music);
                    if (!String.IsNullOrEmpty(validation))
                    {
                        ViewData["ValidationMessage"] = validation;
                        return View(music);
                    }
                    else
                    {
                        try
                        {
                            // Move the music
                            string oldmusic = Server.MapPath(@"~/UploadedFiles");
                            if (!oldmusic.EndsWith(@"\"))
                                oldmusic += @"\";
                            oldmusic += Convert.ToString(Session["UserAccountID"]) + @"\Music\" + music.OriginalFilename;

                            string newmusic = Server.MapPath(@"~/Media");
                            if (!newmusic.EndsWith(@"\"))
                                newmusic += @"\";
                            newmusic += Convert.ToString(Session["UserAccountID"]) + @"\Music\" + music.StoredFilename;

                            System.IO.File.Copy(oldmusic, newmusic);
                            System.IO.File.Delete(oldmusic);
                        }
                        catch
                        {
                            ViewData["ValidationMessage"] = "Failed to copy file.";
                            return View(music);
                        }

                        repository.CreateMusic(music);

                        CommonMethods.CreateActivityLog((User)Session["User"], "Music", "Upload",
                                "Added music '" + music.MusicName + "' - ID: " + music.MusicID.ToString());

                        return RedirectToAction("Index");
                    }
                }

                return View(music);
            }
            catch (Exception ex)
            {
                Helpers.SetupApplicationError("Music", "Upload POST", ex.Message);
                return RedirectToAction("Index", "ApplicationError");
            }
        }
示例#8
0
        public ActionResult Edit(Music music)
        {
            try
            {
                if (Session["UserAccountID"] == null)
                    return RedirectToAction("Validate", "Login");
                User user = (User)Session["User"];
                ViewData["LoginInfo"] = Utility.BuildUserAccountString(user.Username, Convert.ToString(Session["UserAccountName"]));
                if (user.IsAdmin)
                    ViewData["txtIsAdmin"] = "true";
                else
                    ViewData["txtIsAdmin"] = "false";

                if (ModelState.IsValid)
                {
                    // Set NULLs to Empty Strings
                    music = FillNulls(music);

                    string validation = ValidateInput(music);
                    if (!String.IsNullOrEmpty(validation))
                    {
                        ViewData["MusicURL"] = @"~/Media/" + Convert.ToString(Session["UserAccountID"]) + @"/Music/" + music.StoredFilename;
                        ViewData["ValidationMessage"] = validation;
                        return View(music);
                    }

                    repository.UpdateMusic(music);

                    CommonMethods.CreateActivityLog((User)Session["User"], "Music", "Edit",
                            "Edited music '" + music.MusicName + "' - ID: " + music.MusicID.ToString());

                    return RedirectToAction("Index");
                }

                return View(music);
            }
            catch (Exception ex)
            {
                Helpers.SetupApplicationError("Music", "Edit POST", ex.Message);
                return RedirectToAction("Index", "ApplicationError");
            }
        }