public ActionResult Upload(UploadVideoViewModel model) { if (model.VideoFile == null) { ModelState.AddModelError("", "Video file is required"); return(View(model)); } if (model.Video.Title == null) { ModelState.AddModelError("", "Title is required"); return(View(model)); } var id = _videoRepo.Upload(model); if (id != 0) { return(RedirectToAction("View", "Video", new { id = id })); } else { ModelState.AddModelError("", "Something went wrong with the Upload"); return(View(model)); } }
public async Task <IActionResult> Post(UploadVideoViewModel model) { int result = 0; if (ModelState.IsValid) { model.Categories = model.Categories[0] .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) .ToArray(); var pathInfo = this.fileQueries.GenerateVideoPath( this.ExtractFileExtension(model.Video.FileName)); var dto = new VideoDTO() { Title = model.Title, Description = model.Description, Path = pathInfo.Path, FileName = pathInfo.FileName, AuthorUserId = this.userManager.GetUserId(HttpContext.User), Categories = model.Categories }; result = await this.videoWriteService.Save(dto, model.Video); } return(result > 0 ? this.Ok() : this.StatusCode(500)); }
public async Task <ActionResult> Create([Bind(Include = "Title,Description,Keywords,Thumbnail,Video")] UploadVideoViewModel val) { if (ModelState.IsValid) { //ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(User.Identity.GetUserId()); ApplicationUser user = db.Users.Find(User.Identity.GetUserId()); if (user.Id != null) { Channel channel = user.UserChannel; int channelId = channel.Channel_id; if (channelId > 0) { HttpPostedFileBase thumbnail = val.Thumbnail; HttpPostedFileBase video = val.Video; if (thumbnail.ContentType == "image/jpg" || thumbnail.ContentType == "image/jpeg" || thumbnail.ContentType == "image/png") { if (video.ContentType == "video/mp4" || video.ContentType == "video/mpeg") { string thumbnailPath = string.Format(tFileLocation, channelId); //create path if doesn't already exist Directory.CreateDirectory(Server.MapPath(thumbnailPath)); thumbnailPath += thumbnail.FileName; System.Diagnostics.Debug.WriteLine(Server.MapPath(thumbnailPath)); string videoPath = string.Format(vFileLocation, channelId) + video.FileName; string rootVideoPath = System.Web.HttpContext.Current.Server.MapPath(videoPath); string rootThumbnailPath = System.Web.HttpContext.Current.Server.MapPath(thumbnailPath); //populate the video model with data Video vid = new Video(); vid.Title = val.Title; vid.Description = val.Description; vid.Keywords = val.Keywords; vid.FilePath = videoPath; vid.ThumbnailPath = thumbnailPath; vid.CreatorChannel = channel; db.Videos.Add(vid); //the below seems to be uneeded as entity framework seems to be doing this for me //Channel chan = db.Channels.First(c => c.Channel_id == channel.Channel_id); //chan.VideoCollection.Add(vid); await db.SaveChangesAsync(); video.SaveAs(rootVideoPath); thumbnail.SaveAs(rootThumbnailPath); return(RedirectToAction("Index")); } } } } return(RedirectToAction("Create", "Channels")); } return(RedirectToAction("Login", "Account")); }
public async Task <IActionResult> Upload() { UploadVideoViewModel upload = new UploadVideoViewModel(_context) { Video = new Video() }; var user = await GetCurrentUserAsync(); return(View(upload)); }
public int Upload(UploadVideoViewModel model) { string videoUploadFolder = "/UserMedia/Videos"; var fileGuid = Guid.NewGuid().ToString(); var fileExt = Path.GetExtension(model.VideoFile.FileName); var fileName = fileGuid + fileExt; string VideoDbPath = videoUploadFolder + "/" + fileName; string path = Path.Combine(HttpContext.Current.Server.MapPath(videoUploadFolder), fileName); model.VideoFile.SaveAs(path); string thumbnailDbPath = CreateThumbnail(fileGuid, path); if (model.Video.Description == null) { model.Video.Description = "No Description"; } using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString)) using (SqlCommand command = new SqlCommand("spUploadVideo", conn)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@UserID", (long)HttpContext.Current.Session["UserID"]); command.Parameters.AddWithValue("@Title", model.Video.Title); command.Parameters.AddWithValue("@Description", model.Video.Description); command.Parameters.Add("@VideoID", SqlDbType.Int).Direction = ParameterDirection.Output; command.Parameters.AddWithValue("@Source", VideoDbPath); command.Parameters.AddWithValue("@Thumbnail", thumbnailDbPath); conn.Open(); command.ExecuteNonQuery(); if (int.TryParse(command.Parameters["@VideoID"].Value.ToString(), out int videoId)) { return(videoId); } } return(0); }
public async Task <IActionResult> Upload(UploadVideoViewModel upload) { ModelState.Remove("Video.User"); ModelState.Remove("Video.VideoFilePath"); if (ModelState.IsValid) { var user = await GetCurrentUserAsync(); upload.Video.VideoTypeId = upload.Video.VideoTypeId; IFormFile videoFile = upload.UserVideo; //create unique identifying using Guid // The use Property of Path and GetExtension method to get the FileName extension // combine these two so videos uploaded cannot have duplocate naming string guid = Guid.NewGuid().ToString(); string newFileName = guid + Path.GetExtension(videoFile.FileName); var videoPath = _environment.WebRootPath + $@"\video\{newFileName}"; // create custom thumbnail, installed FFMPEG video utility library // set the path to the environment root folder string thumbName = guid + ".jpg"; string thumbPath = _environment.WebRootPath + $@"\video\thumbs\{thumbName}"; using (var fileStream = new FileStream(videoPath, FileMode.Create)) { await videoFile.CopyToAsync(fileStream); upload.Video.VideoFilePath = "..\\..\\video\\" + newFileName; } // arguments to pass to FFMPEG Process // -i = telling ffmpeg program which video file to use as an input during it's Process, key value pairs // -vframes = 1 frame to use to make an image, key value pair // -ss how many seconds into video to capture image, key value pair // -filter: v = use video filter that is built in to scale size proportionally // scale = 280 is the width and -1 tells it to keep it's height proportional to width, key value pair string ffmpegArgs = "-i " + videoPath + " -vframes 1 -ss 00:00:05 -filter:v scale=\"280:-1\" " + thumbPath; // start ffmpeg.exe file by creating a new instance which is called a Process Process thumb = new Process(); // use instance of StartInfo class from Process class thumb.StartInfo.FileName = _environment.WebRootPath + "\\ffmpeg.exe"; // declare what arguments it will use thumb.StartInfo.Arguments = ffmpegArgs; // make sure that Process does not continue if Application stops running thumb.StartInfo.UseShellExecute = false; // start process, make it wait until process is complete , then dispose of the process instance to cleam up memory try { thumb.Start(); thumb.WaitForExit(); thumb.Dispose(); } catch (Exception err) { // make sure error is displayed in debug output console, may not display if using Console.Writeline Debug.WriteLine("thumb error message : " + err.Message); } upload.Video.User = user; upload.Video.Thumbnail = "..\\..\\video\\thumbs\\" + thumbName; _context.Add(upload.Video); await _context.SaveChangesAsync(); return(RedirectToAction("Profile", "Home")); } return(RedirectToAction("Profile", "Home")); }