public ActionResult NewSubtitle(SubtitleViewModel model) { var validSubtitleType = new string[] // Array of valid filetypes { "text/plain", "application/octet-stream" }; if (model.SrtUpload == null || model.SrtUpload.ContentLength == 0) // Validation to make sure that a file has been selected for upload { ModelState.AddModelError("SrtUpload", "This field is required"); } else if (!validSubtitleType.Contains(model.SrtUpload.ContentType)) // Validation for valid filetypes { ModelState.AddModelError("SrtUpload", "Please choose a valid .srt file"); } if (ModelState.IsValid) { string userId = User.Identity.GetUserId(); List<ApplicationUser> users = new List<ApplicationUser>();; var subtitle = new Subtitle // Create a new instance of a subtitle with correct data { Name = model.Name, LanguageId = model.LanguageId, MediaId = model.MediaId, Users = users, Comments = new List<Comment>(), DateSubmitted = DateTime.Now, SubtitleRating = new SubtitleRating(), SubtitleUpvote = new SubtitleUpvote(), SubtitleDownvote = new SubtitleDownvote() }; int subtId = m_repo.AddSubtitle(subtitle); // Add the subtitle to db and get the id m_repo.AddUserToSubtitle(subtId, userId); // Add the logged in user to list of contributors if (model.SrtUpload != null || model.SrtUpload.ContentLength > 0) { StreamReader reader = new StreamReader(model.SrtUpload.InputStream); // Access the HttpPostedFileBase StreamReader List<SubtitleLine> lines = new List<SubtitleLine>(); // Create a list to contain the lines until they are saved to db while (!reader.EndOfStream) // Loops until it hits the end of the stream { SubtitleLine sl = new SubtitleLine(); // Create new line string tmpString = reader.ReadLine(); // Read the first line and make sure it isn't empty if (String.IsNullOrEmpty(tmpString)) // Skip this loop if it is { continue; } //LineNumber: int tmpInt; // Get the line number int.TryParse(tmpString, out tmpInt); sl.LineNumber = tmpInt; //Time: tmpString = reader.ReadLine(); // Get the time of the subtitle line sl.Time = tmpString; //LineOne: tmpString = reader.ReadLine(); // Get the first display text sl.LineOne = tmpString; //LineTwo: tmpString = reader.ReadLine(); // Get the second display text sl.LineTwo = tmpString; sl.SubtitleId = subtId; // Relate the line to the new subtitle lines.Add(sl); // Add the new line to the list if (String.IsNullOrEmpty(tmpString)) // Check if the second line was empty, { // if it was the next line would be the line number continue; } reader.ReadLine(); // Read the empty line between lines } m_repo.AddSubtitleLine(lines); // Send the list to db } return RedirectToAction("ViewSubtitle", new { mediaID = subtitle.MediaId, languageId = subtitle.LanguageId }); // Go to the newly created subtitle } return View(model); }
public void UpdateSubtitleLine(SubtitleLine s) { throw new NotImplementedException(); }
public ActionResult EditSubtitleLines(SubtitleLine s) { if (ModelState.IsValid) { SubtitleLine result = new SubtitleLine() { SubtitleId = s.SubtitleId, LineNumber = s.LineNumber, Id = s.Id, LineOne = s.LineOne, LineTwo = s.LineTwo, Time = s.Time }; m_repo.UpdateSubtitleLine(result); // Send the edited line to db m_repo.AddUserToSubtitle(result.SubtitleId, User.Identity.GetUserId()); // Add the user to list of contributors return RedirectToAction("EditSubtitleLines", new { subtitleId = s.SubtitleId });// Return to edit page with updated data } return View("Error"); }
public void UpdateSubtitleLine(SubtitleLine s) { var line = (from l in m_db.SubtitleLines where l.Id == s.Id select l).SingleOrDefault(); line.LineNumber = s.LineNumber; line.LineOne = s.LineOne; line.LineTwo = s.LineTwo; line.SubtitleId = s.SubtitleId; line.Time = s.Time; m_db.SaveChanges(); }