コード例 #1
0
        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);
        }
コード例 #2
0
        public ActionResult NewSubtitle(int? id)
        {
            if (id.HasValue)
            {
                var media = m_repo.GetMedias().Where(s => s.Id == id.Value).SingleOrDefault();                      // Get the media the new subtitle belongs to
                SubtitleViewModel model = new SubtitleViewModel { MediaId = id.Value, MediaName = media.Name };     // Create view model with correct data
                var subtitleLanguages = m_repo.GetSubtitleLanguages().ToList();                                     // Get all available languages

                model.SubtitleLanguages = new List<SelectListItem>();                                               // add languages to list of SelectListItem
                foreach (var m in subtitleLanguages)
                {
                    model.SubtitleLanguages.Add(new SelectListItem { Value = m.Id.ToString(), Text = m.Language });
                }
                return View(model);
            }

            return View("Error");
        }