예제 #1
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"));
            }
        }