Esempio n. 1
0
 public void AddSubtitle(Subtitle sub)
 {
     db.Subtitles.Add(sub);
     db.SaveChanges();
 }
Esempio n. 2
0
 public void DeleteSubtitle(Subtitle sub)
 {
     db.Subtitles.Remove(sub);
     db.SaveChanges();
 }
Esempio n. 3
0
        public ActionResult Upload(Subtitle item, HttpPostedFileBase file)
        {
            var filename = Path.GetFileName(file.FileName);
            var extension = Path.GetExtension(filename);

            //Checks if the file extension is correct and redirects user to error page if required.
            if ((extension != ".srt" && extension != ".txt"))
            {
                return View("UploadError");
            }

            Media med = db.GetMediaByName(item.Title);
            Media newMedia = new Media();

            // If the media doesnt exist it
            // will be created automatically
            if (med == null)
            {
                newMedia.Title = item.Title;
                db.AddMedia(newMedia);
                med = newMedia;
            }

            if (ModelState.IsValid)
            {
                item.MediaID = med.ID;
                item.Author = User.Identity.Name;
                db.AddSubtitle(item);

                //Code that checks if uploaded file has content
                //before the file is saved to the server.
                if ((file != null) && (file.ContentLength > 0))
                {
                    string fn = System.IO.Path.GetFileName(file.FileName);
                    string SaveLocation = Server.MapPath("~/App_Data/" + fn);
                    file.SaveAs(SaveLocation);
                }

                SubtitleLine srtLine = new SubtitleLine();

                //Turn file to string
                string srtString = new StreamReader(file.InputStream, Encoding.Default, true).ReadToEnd();

                //regex for srt files from http://www.codeproject.com/Articles/32834/Subtitle-Synchronization-with-C
                string pattern =  @"(?<sequence>\d+)\r\n(?<start>\d{2}\:\d{2}\:\d{2},\d{3}) --\> " +
                                  @"(?<end>\d{2}\:\d{2}\:\d{2},\d{3})\r\n(?<text>[\s\S]*?\r\n\r\n)";

                //parse string and send to database
                int counter = 1;
                bool srtTrue = false;

                foreach (string result in Regex.Split(srtString, pattern))
                {
                    //first instance in the str format is always empty
                    if (counter == 1)
                    {
                        srtLine.Index = 0;
                        srtLine.TimeFrom = null;
                        srtLine.TimeTo = null;
                        srtLine.Text = null;
                    }

                    //second instance is "subtitle number this"
                    if (counter == 2)
                    {
                        srtLine.Index = Convert.ToInt32(result);
                    }

                    //tird instance is TC in
                    if (counter == 3)
                    {
                        srtLine.TimeFrom = result;
                    }

                    //fourth is TC out
                    if (counter == 4)
                    {
                        srtLine.TimeTo = result;
                    }

                    //fifth is the actual onscreen text
                    if (counter == 5)
                    {
                        srtLine.Text = result;
                        srtLine.SubtitleID = item.ID;
                        counter = 0;
                    }

                    counter++;

                    // checks to see if all columns in srtLine have been populated
                    // before adding a line to the database.
                    if (   srtLine.Index != 0
                        && srtLine.TimeFrom != null
                        && srtLine.TimeTo != null
                        && srtLine.Text != null
                        && srtLine.SubtitleID != 0)
                    {
                        db.AddSubtitleLine(srtLine);
                        srtTrue = true;
                    }

                }

                //If columns have been populated the format is fine and file created.
                //Else entires that have been created are deleted and user directed to error page.
                if (srtTrue)
                {
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    db.DeleteSubtitle(item);
                    db.DeleteMedia(med);
                    return View("UploadError");
                }
            }

            // Gets language dropdown
            ViewBag.Languages = db.GetLanguageListItems();
            return View(item);
        }