Ejemplo n.º 1
0
        public FileResult GetResultContents(Guid jobId)
        {
            if (jobId.Equals(Guid.Empty))
            {
                return(null);
            }

            var job = (GenericJob)_context.Jobs.FirstOrDefault(e => jobId.Equals(e.Id));

            if (job == null || job.ResultContent == null)
            {
                return(null);
            }

            FileResult result = null;

            try
            {
                result = new FileResult(job.ResultContent, "text/plain", "SampleResponse.txt");
                RoboBrailleProcessor.UpdateDownloadCounterInDb(job.Id, _context);
            } catch
            {
                //ignored
            }
            return(result);
        }
Ejemplo n.º 2
0
        public FileResult GetResultContents(Guid jobId)
        {
            if (jobId.Equals(Guid.Empty))
            {
                return(null);
            }

            using (var context = new RoboBrailleDataContext())
            {
                var job = context.Jobs.FirstOrDefault(e => jobId.Equals(e.Id));
                if (job == null || job.ResultContent == null)
                {
                    return(null);
                }
                RoboBrailleProcessor.UpdateDownloadCounterInDb(job.Id, _context);
                FileResult result = null;
                try
                {
                    result = new FileResult(job.ResultContent, job.ResultMimeType, job.FileName + job.ResultFileExtension);
                }
                catch (Exception)
                {
                    // ignored
                }
                return(result);
            }
        }
        private string DecodeBrailleString(BrailleJob job)
        {
            Encoding enc = RoboBrailleProcessor.GetEncoding(job.FileContent, job.BrailleLanguage);
            string   res = enc.GetString(job.FileContent);

            res = res.Replace('\t', ' ');
            if (job.BrailleFormat.Equals(BrailleFormat.sixdot))
            {
                res = res.ToLowerInvariant();
            }
            return(res);
        }
Ejemplo n.º 4
0
        public async Task <Guid> SubmitWorkItem(SignLanguageJob job)
        {
            try
            {
                _context.Jobs.Add(job);
                _context.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                string errorMessages = string.Join("; ", ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage));
                throw new DbEntityValidationException(errorMessages);
            }

            var task = Task.Factory.StartNew(t =>
            {
                try
                {
                    /*
                     * TODO process the job here
                     * if at any point it fails change the success flag to false (success=false)
                     */

                    //get text from txt file
                    string textToTranslate = Encoding.UTF8.GetString(job.FileContent);
                    //submit to translation engine/service/library
                    var result = MockTranslate(textToTranslate, job.SourceTextLanguage, job.TargetSignLanguage, job.SignLanguageForm);
                    //required input: source text, source langauge, destination language
                    //get result and save to database as video file

                    job.ResultContent       = result;
                    job.DownloadCounter     = 0;
                    job.ResultFileExtension = "mp4";
                    job.ResultMimeType      = "video/mp4";
                    job.Status                = JobStatus.Done;
                    job.FinishTime            = DateTime.Now;
                    _context.Entry(job).State = EntityState.Modified;
                    _context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.Message);
                    RoboBrailleProcessor.SetJobFaulted(job, _context);
                    throw ex;
                }
            }, job);

            return(job.Id);
        }
Ejemplo n.º 5
0
        public async Task <Guid> SubmitWorkItem(GenericJob job)
        {
            try
            {
                using (var context = new RoboBrailleDataContext())
                {
                    context.Jobs.Add(job);
                    context.SaveChanges();
                }
            }
            catch (DbEntityValidationException ex)
            {
                string errorMessages = string.Join("; ", ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage));
                throw new DbEntityValidationException(errorMessages);
            }

            var task = Task.Factory.StartNew(t =>
            {
                try
                {
                    //TODO process the job here
                    Task.Delay(10000);

                    job.ResultContent       = Encoding.UTF8.GetBytes("This is a sample job that ran for 10 seconds.");
                    job.ResultFileExtension = "txt";
                    job.ResultMimeType      = "text/plain";
                    job.DownloadCounter     = 0;
                    job.Status                = JobStatus.Done;
                    job.FinishTime            = DateTime.Now;
                    _context.Entry(job).State = EntityState.Modified;
                    _context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.Message);
                    RoboBrailleProcessor.SetJobFaulted(job, _context);
                    throw ex;
                }
            }, job);

            //info: add "await task;" here to make it run synced
            return(job.Id);
        }
        public async Task <Guid> SubmitWorkItem(HTMLToTextJob job)
        {
            try
            {
                _context.Jobs.Add(job);
                _context.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                string errorMessages = string.Join("; ", ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage));
                throw new DbEntityValidationException(errorMessages);
            }

            var task = Task.Factory.StartNew(t =>
            {
                try
                {
                    string mime               = "text/plain";
                    string fileExtension      = ".txt";
                    string res                = HTMLToTextProcessor.StripHTML(Encoding.UTF8.GetString(job.FileContent));
                    job.ResultContent         = Encoding.UTF8.GetBytes(res);
                    job.DownloadCounter       = 0;
                    job.ResultFileExtension   = fileExtension;
                    job.ResultMimeType        = mime;
                    job.Status                = JobStatus.Done;
                    job.FinishTime            = DateTime.Now;
                    _context.Entry(job).State = EntityState.Modified;
                    _context.SaveChanges();
                }
                catch (Exception ex)
                {
                    RoboBrailleProcessor.SetJobFaulted(job, _context);
                    throw ex;
                }
            }, job);

            return(job.Id);
        }
        public async System.Threading.Tasks.Task <Guid> SubmitWorkItem(MSOfficeJob job)
        {
            try
            {
                _context.Jobs.Add(job);
                _context.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                string errorMessages = string.Join("; ", ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage));
                throw new DbEntityValidationException(errorMessages);
            }

            var task = Task.Factory.StartNew(t =>
            {
                string sourceFilePath = Path.Combine(Path.GetTempPath(), job.Id + "." + job.FileExtension);
                bool success          = true;
                try
                {
                    /*
                     * What file do you have: doc, docx, rtf, ppt, pptx, html, other
                     * What file do you want: pdf, txt, html, rtf
                     * How to process?
                     */
                    File.WriteAllBytes(sourceFilePath, job.FileContent);
                    RtfHTMLProcessor rhp = new RtfHTMLProcessor();
                    switch (job.MSOfficeOutput)
                    {
                    case MSOfficeOutput.pdf:
                        var tempFile = Path.Combine(Path.GetTempPath(), job.Id.ToString() + ".pdf");
                        try
                        {
                            string[] args = new string[] { sourceFilePath, tempFile };
                            int status    = OfficeToPDF.OfficeToPdfFacade.ConvertOfficeToPDF(args);
                            if (status == 0)
                            {
                                job.ResultContent = File.ReadAllBytes(tempFile);
                            }
                            else
                            {
                                success = false;
                            }
                        }
                        catch (Exception e)
                        {
                            success = false;
                        }
                        finally
                        {
                            if (File.Exists(tempFile))
                            {
                                File.Delete(tempFile);
                            }
                        }
                        break;

                    case MSOfficeOutput.txt:
                        if (job.MimeType.Equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document", StringComparison.InvariantCultureIgnoreCase))
                        {
                            var res = OfficeWordProcessor.ConvertWordDocument(sourceFilePath, MSOfficeOutput.txt);
                            if (res != null)
                            {
                                job.ResultContent = res;
                            }
                            else
                            {
                                success = false;
                            }
                        }
                        else if ((job.MimeType.Equals("application/msword", StringComparison.InvariantCultureIgnoreCase) || job.MimeType.Equals("application/rtf", StringComparison.InvariantCultureIgnoreCase)) && job.FileExtension.Equals("rtf"))
                        {
                            job.ResultContent = Encoding.UTF8.GetBytes(rhp.ConvertRtfToText(Encoding.UTF8.GetString(job.FileContent)));
                        }
                        else if (job.MimeType.Equals("application/vnd.openxmlformats-officedocument.presentationml.presentation", StringComparison.InvariantCultureIgnoreCase))
                        {
                            byte[] result = null;
                            if (!OfficePresentationProcessor.ContainsVideo(sourceFilePath))
                            {
                                result = OfficePresentationProcessor.ProcessDocument(sourceFilePath, job);
                            }
                            else
                            {
                                result = (new OfficePresentationProcessor().ProcessPPTXWithVideo(sourceFilePath, job));
                            }
                            if (result != null)
                            {
                                job.ResultContent = result;
                            }
                            else
                            {
                                success = false;
                            }
                        }
                        else
                        {
                            success = false;
                        }
                        break;

                    case MSOfficeOutput.html:
                        byte[] html = null;
                        switch (job.MimeType.ToLowerInvariant())
                        {
                        case "application/rtf":
                        case "application/msword":
                            if (job.FileExtension.EndsWith("rtf"))
                            {
                                html = Encoding.UTF8.GetBytes(rhp.ConvertRtfToHtml(Encoding.UTF8.GetString(job.FileContent)));
                            }
                            break;

                        case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
                            html = OfficeWordProcessor.ConvertWordDocument(sourceFilePath, MSOfficeOutput.html);
                            break;

                        case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
                            html = OfficePresentationProcessor.ProcessDocument(sourceFilePath, job);
                            break;

                        default:
                            success = false;
                            break;
                        }
                        if (html != null)
                        {
                            job.ResultContent = html;
                        }
                        else
                        {
                            success = false;
                        }
                        break;

                    case MSOfficeOutput.rtf:
                        byte[] rtf = null;
                        switch (job.MimeType.ToLowerInvariant())
                        {
                        case "text/html":
                        case "application/xhtml+xml":
                            //rtf = rhp.ConvertHtmlToRtf(Encoding.UTF8.GetString(job.FileContent));
                            break;

                        case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
                            rtf = OfficePresentationProcessor.ProcessDocument(sourceFilePath, job);
                            break;

                        case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
                            rtf = OfficeWordProcessor.ConvertWordDocument(sourceFilePath, MSOfficeOutput.rtf);
                            break;

                        default:
                            success = false;
                            break;
                        }
                        if (rtf != null)
                        {
                            job.ResultContent = rtf;
                        }
                        else
                        {
                            success = false;
                        }
                        break;

                    default:
                        success = false;
                        break;
                    }
                    if (success)
                    {
                        string mime          = "text/plain";
                        string fileExtension = ".txt";
                        switch (job.MSOfficeOutput)
                        {
                        case MSOfficeOutput.pdf:
                            mime          = "application/pdf";
                            fileExtension = ".pdf";
                            break;

                        case MSOfficeOutput.html:
                            mime          = "text/html";
                            fileExtension = ".html";
                            break;

                        case MSOfficeOutput.rtf:
                            mime          = "application/rtf";
                            fileExtension = ".rtf";
                            break;

                        case MSOfficeOutput.txt:
                            if (job.SubtitleFormat == null && job.SubtitleLangauge == null)
                            {
                                mime          = "text/plain";
                                fileExtension = ".txt";
                            }
                            else
                            {
                                mime          = "application/zip";
                                fileExtension = ".zip";
                            }
                            break;

                        default:
                            break;
                        }
                        job.DownloadCounter     = 0;
                        job.ResultFileExtension = fileExtension;
                        job.ResultMimeType      = mime;
                        job.Status     = JobStatus.Done;
                        job.FinishTime = DateTime.Now;

                        _context.Entry(job).State = EntityState.Modified;
                        _context.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    //sometimes transaction exception coming from video conversion part https://msdn.microsoft.com/en-gb/data/dn456833
                    success = false;
                    RoboBrailleProcessor.SetJobFaulted(job, _context);
                    Trace.WriteLine(ex.Message);
                    throw ex;
                }
                finally
                {
                    if (File.Exists(sourceFilePath))
                    {
                        File.Delete(sourceFilePath);
                    }
                }

                if (!success)
                {
                    RoboBrailleProcessor.SetJobFaulted(job, _context);
                    throw new Exception("Job was unssuccessful!");
                }
            }, job);

            return(job.Id);
        }
        /// <summary>
        /// Consider two output types:
        /// .txt if pptx doesn't contain videos
        /// .zip if it contains videos (a .txt file + the video files)
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public async Task <Dictionary <int, string> > ProcessVideoParts(string source, MSOfficeJob job, string tempDirectory)
        {
            Dictionary <int, string> TextContent = new Dictionary <int, string>();
            int index = 1;

            using (PresentationDocument ppt = PresentationDocument.Open(source, false))
            {
                // Get the relationship ID of the first slide.
                PresentationPart   part     = ppt.PresentationPart;
                OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements;
                foreach (SlideId slideID in slideIds)
                {
                    //get the right content according to type. for now only text is getting through
                    string relId = slideID.RelationshipId;

                    // Get the slide part from the relationship ID.
                    SlidePart slide = (SlidePart)part.GetPartById(relId);

                    //extract the videos and put placeholders
                    IEnumerable <A.VideoFromFile> videos = slide.Slide.Descendants <A.VideoFromFile>();
                    foreach (A.VideoFromFile vid in videos)
                    {
                        string videoRelId         = vid.Link;
                        ReferenceRelationship rel = slide.GetReferenceRelationship(videoRelId);
                        Uri    uri        = rel.Uri;
                        var    filename   = uri.ToString().Split('/').Last();
                        var    s          = ppt.Package.GetPart(uri).GetStream();
                        byte[] videoBytes = null;
                        using (BinaryReader br = new BinaryReader(s))
                        {
                            videoBytes = br.ReadBytes((int)s.Length);
                        }
                        //write video to result directory
                        File.WriteAllBytes(Path.Combine(tempDirectory, filename), videoBytes);

                        //send to amara
                        string langauge = "en-us";
                        if (ppt.PackageProperties.Language != null)
                        {
                            langauge = ppt.PackageProperties.Language;
                        }

                        AmaraSubtitleJob vj = new AmaraSubtitleJob()
                        {
                            SubmitTime       = DateTime.Now,
                            FinishTime       = DateTime.Now,
                            Status           = JobStatus.Started,
                            MimeType         = "video/" + filename.Substring(filename.LastIndexOf('.') + 1), //define this properly
                            User             = job.User,
                            UserId           = job.UserId,
                            DownloadCounter  = 0,
                            FileContent      = videoBytes,
                            InputFileHash    = RoboBrailleProcessor.GetMD5Hash(videoBytes),
                            SubtitleFormat   = job.SubtitleFormat,
                            SubtitleLangauge = job.SubtitleLangauge,
                            FileName         = job.Id.ToString() + "-" + filename.Substring(0, filename.LastIndexOf('.')),
                            FileExtension    = filename.Substring(filename.LastIndexOf('.') + 1)
                        };

                        //retrieve the message from amara
                        byte[] filebytes     = null;
                        Guid   subtitleJobId = vcr.SubmitWorkItem(vj).Result;
                        while (vcr.GetWorkStatus(subtitleJobId) == 2)
                        {
                            await Task.Delay(2000);
                        }
                        filebytes = vcr.GetResultContents(subtitleJobId).getFileContents();
                        File.WriteAllBytes(Path.Combine(tempDirectory, filename.Substring(0, filename.LastIndexOf('.')) + vj.ResultFileExtension), filebytes);

                        slide.Slide.CommonSlideData.ShapeTree.AppendChild(
                            new ShapeProperties(
                                new TextBody(
                                    new A.Paragraph(
                                        new A.Run(
                                            new A.Text("Video file name = " + filename + " subtitle attached to video has id: " + vj.Id.ToString())
                                            )))));
                    }


                    // Build a StringBuilder object.
                    StringBuilder paragraphText = new StringBuilder();

                    // Get the inner text of the slide:
                    IEnumerable <A.Text> texts = slide.Slide.Descendants <A.Text>();
                    foreach (A.Text text in texts)
                    {
                        if (text.Text.Length > 1)
                        {
                            paragraphText.Append(text.Text + " ");
                        }
                        else
                        {
                            paragraphText.Append(text.Text);
                        }
                    }
                    string slideText = paragraphText.ToString();
                    TextContent.Add(index, slideText);
                    index++;
                }
                //until now there are: video files + subtitle files
                //at the end there will be: a txt file + video files + subtitle files (all zipped up)
            }
            return(TextContent);
        }
Ejemplo n.º 9
0
        public async System.Threading.Tasks.Task <Guid> SubmitWorkItem(DaisyJob job)
        {
            try
            {
                _context.Jobs.Add(job);
                _context.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                string errorMessages = string.Join("; ", ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage));
                throw new DbEntityValidationException(errorMessages);
            }

            var task = Task.Factory.StartNew(t =>
            {
                bool success = true;
                try
                {
                    byte[] res = null;
                    res        = _daisyCall.Call(job.FileContent, DaisyOutput.Epub3WMO.Equals(job.DaisyOutput), job.Id.ToString());
                    if (res != null && res.Length > 0)
                    {
                        job.ResultContent = res;
                    }
                    else
                    {
                        success = false;
                    }

                    string mime          = "application/zip";
                    string fileExtension = ".zip";
                    string fileName      = job.FileName;
                    if (DaisyOutput.Epub3WMO.Equals(job.DaisyOutput))
                    {
                        mime          = "application/epub+zip";
                        fileExtension = ".epub";
                    }
                    if (!success)
                    {
                        RoboBrailleProcessor.SetJobFaulted(job, _context);
                    }
                    else
                    {
                        job.ResultFileExtension = fileExtension;
                        job.ResultMimeType      = mime;
                        job.DownloadCounter     = 0;
                        job.Status                = JobStatus.Done;
                        job.FinishTime            = DateTime.Now;
                        _context.Entry(job).State = EntityState.Modified;
                        _context.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    RoboBrailleProcessor.SetJobFaulted(job, _context);
                    throw ex;
                }
            }, job);

            return(job.Id);
        }
Ejemplo n.º 10
0
        public async Task <Guid> SubmitWorkItem(AudioJob job)
        {
            if (job == null)
            {
                throw new Exception("audio job is null");
            }

            //VoiceGender gender = VoiceGender.NotSet;
            //CultureInfo ci = CultureInfo.CurrentCulture;
            //EncodingFormat eformat = EncodingFormat.Pcm;
            //string voiceName = String.Empty;
            //VoicePropriety voicePropriety = job.VoicePropriety.First();
            //    switch (job.AudioLanguage)
            //    {
            //        case Language.daDK:
            //            ci = CultureInfo.GetCultureInfo("da-DK");
            //            if (voicePropriety.Equals(VoicePropriety.Male))
            //            {
            //                gender = VoiceGender.Male;
            //                voiceName = "Carsten";
            //            }
            //            else
            //            {
            //                gender = VoiceGender.Female;
            //                if (job.VoicePropriety.Contains(VoicePropriety.Anne))
            //                    voiceName = "Anne";
            //                else
            //                    voiceName = "Sara";
            //            }
            //            break;
            //    case Language.ltLT:
            //        ci = CultureInfo.GetCultureInfo("lt-LT");
            //        if (voicePropriety.Equals(VoicePropriety.Male))
            //        {
            //            gender = VoiceGender.Male;
            //            if (job.VoicePropriety.Contains(VoicePropriety.Older))
            //                voiceName = "Vladas";
            //            else voiceName = "Edvardas";
            //        }
            //        else
            //        {
            //            gender = VoiceGender.Female;
            //            if (job.VoicePropriety.Contains(VoicePropriety.Older))
            //                voiceName = "Regina";
            //            else voiceName = "Aiste";
            //        }
            //        break;
            //    //case Language.arEG:
            //    //    ci = CultureInfo.GetCultureInfo("ar-EG");
            //    //    if (voicePropriety.Equals(VoicePropriety.Male))
            //    //    {
            //    //        gender = VoiceGender.Male;
            //    //        voiceName = "Sakhr Voice One";
            //    //    }
            //    //    else
            //    //    {
            //    //        gender = VoiceGender.Female;
            //    //        voiceName = "Sakhr Voice Two"; //Three, Four, Five, Six
            //    //    }
            //    //    break;
            //    case Language.huHU: ci = CultureInfo.GetCultureInfo("hu-HU");
            //        if (voicePropriety.Equals(VoicePropriety.Male))
            //        {
            //            gender = VoiceGender.Male;
            //            voiceName = "Gabor";
            //        }
            //        else gender = VoiceGender.Female;
            //            voiceName = "Eszter";
            //        break;
            //    case Language.isIS: ci = CultureInfo.GetCultureInfo("is-IS");
            //        if (voicePropriety.Equals(VoicePropriety.Male))
            //        {
            //            gender = VoiceGender.Male;
            //            voiceName = "IVONA 2 Karl";
            //        }
            //        else
            //        {
            //            gender = VoiceGender.Female;
            //            voiceName = "IVONA 2 Dóra";
            //        }
            //        break;
            //    case Language.nlNL: ci = CultureInfo.GetCultureInfo("nl-NL");
            //        if (voicePropriety.Equals(VoicePropriety.Male))
            //        {
            //            gender = VoiceGender.Male;
            //            voiceName = "Arthur";
            //        }
            //        else
            //        {
            //            gender = VoiceGender.Female;
            //            voiceName = "Janneke";
            //        };
            //        break;
            //    case Language.enUS: ci = CultureInfo.GetCultureInfo("en-US"); gender = VoiceGender.Female; voiceName = "IVONA 2 Jennifer"; break;
            //    case Language.enGB: ci = CultureInfo.GetCultureInfo("en-GB"); gender = VoiceGender.Female; voiceName = "Kate"; break;
            //    case Language.frFR: ci = CultureInfo.GetCultureInfo("fr-FR"); gender = VoiceGender.Female; voiceName = "ScanSoft Virginie_Full_22kHz"; break;
            //    case Language.deDE: ci = CultureInfo.GetCultureInfo("de-DE"); gender = VoiceGender.Male; voiceName = "Stefan"; break;
            //    case Language.esES: ci = CultureInfo.GetCultureInfo("es-ES"); gender = VoiceGender.Male; voiceName = "Jorge"; break;
            //    case Language.esCO: ci = CultureInfo.GetCultureInfo("es-CO"); gender = VoiceGender.Female; voiceName = "Ximena"; break;
            //    case Language.bgBG: ci = CultureInfo.GetCultureInfo("bg-BG"); gender = VoiceGender.Female; voiceName = "Gergana"; break;
            //    case Language.itIT: ci = CultureInfo.GetCultureInfo("it-IT"); gender = VoiceGender.Female; voiceName = "Paola"; break;
            //    case Language.nbNO: ci = CultureInfo.GetCultureInfo("nb-NO"); break;
            //    case Language.roRO: ci = CultureInfo.GetCultureInfo("ro-RO"); gender = VoiceGender.Female; voiceName = "IVONA 2 Carmen"; break;
            //    case Language.svSE: ci = CultureInfo.GetCultureInfo("sv-SE"); break;
            //    case Language.plPL: ci = CultureInfo.GetCultureInfo("pl-PL"); gender = VoiceGender.Male; voiceName = "Krzysztof"; break;
            //    case Language.ptBR: ci = CultureInfo.GetCultureInfo("pt-BR"); break;
            //    case Language.enAU: ci = CultureInfo.GetCultureInfo("en-AU"); break;
            //    case Language.frCA: ci = CultureInfo.GetCultureInfo("fr-CA"); break;
            //    case Language.ptPT: ci = CultureInfo.GetCultureInfo("pt-PT"); gender = VoiceGender.Female; voiceName = "Amalia"; break;
            //    case Language.klGL: ci = CultureInfo.GetCultureInfo("kl-GL"); gender = VoiceGender.Female; voiceName = "Martha"; break;
            //    case Language.elGR: ci = CultureInfo.GetCultureInfo("el-GR"); gender = VoiceGender.Female; voiceName = "Maria"; break;
            //    case Language.slSI: ci = CultureInfo.GetCultureInfo("sl-SI"); gender = VoiceGender.Male; voiceName = "Matej Govorec"; break;
            //    case Language.jaJP: ci = CultureInfo.GetCultureInfo("ja-JP"); break;
            //    case Language.koKR: ci = CultureInfo.GetCultureInfo("ko-KR"); break;
            //    case Language.zhCN: ci = CultureInfo.GetCultureInfo("zh-CN"); break;
            //    case Language.zhHK: ci = CultureInfo.GetCultureInfo("zh-HK"); break;
            //    case Language.zhTW: ci = CultureInfo.GetCultureInfo("zh-TW"); break;
            //    case Language.fiFI: ci = CultureInfo.GetCultureInfo("fi-FI"); break;
            //    case Language.esMX: ci = CultureInfo.GetCultureInfo("es-MX"); break;
            //    case Language.caES: ci = CultureInfo.GetCultureInfo("ca-ES"); break;
            //    case Language.ruRU: ci = CultureInfo.GetCultureInfo("ru-RU"); gender = VoiceGender.Female; voiceName = "IVONA 2 Tatyana"; break;
            //    default: ci = CultureInfo.GetCultureInfo("en-US"); gender = VoiceGender.Female; voiceName = "IVONA 2 Jennifer"; break;
            //}
            try
            {
                _context.Jobs.Add(job);
                _context.SaveChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                throw ex;
            }

            var task = Task.Factory.StartNew(t =>
            {
                AudioJob auJob = (AudioJob)t;
                try
                {
                    //if (isVoiceInstalled(ci, gender, voiceName) && !"Gergana".Equals(voiceName))
                    //{
                    //    string tempfile = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
                    //    int rate = (int)Enum.Parse(typeof(AudioSpeed), Convert.ToString(auJob.SpeedOptions));

                    //    switch (auJob.FormatOptions)
                    //    {
                    //        case AudioFormat.Mp3:
                    //            tempfile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".mp3");
                    //            break;
                    //        case AudioFormat.Wav:
                    //            tempfile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".wav");
                    //            break;
                    //        case AudioFormat.Wma:
                    //            tempfile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".wma");
                    //            break;
                    //        case AudioFormat.Aac:
                    //            tempfile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".aac");
                    //            break;
                    //    }

                    //    var speech = new SpeechSynthesizer();
                    //    speech.Rate = rate;
                    //    speech.SelectVoice(voiceName);
                    //    if (speech.Voice.Equals(null))
                    //    {
                    //        speech.SelectVoiceByHints(gender, VoiceAge.Adult, 1, ci);
                    //        if (speech.Voice.Equals(null))
                    //        {
                    //            //return a message saying the voice is not installed on the system
                    //            RoboBrailleProcessor.SetJobFaulted(auJob);
                    //            return;
                    //        }
                    //    }
                    //    var safi = new SpeechAudioFormatInfo(eformat, 44100, 16, 2, 44100 * 4, 4, null);
                    //    speech.SetOutputToWaveFile(tempfile, safi);
                    //    Encoding enc = RoboBrailleProcessor.GetEncoding(auJob.FileContent);
                    //    if (enc.Equals(Encoding.ASCII))
                    //        enc = RoboBrailleProcessor.GetEncodingByCountryCode(auJob.AudioLanguage);
                    //    speech.Speak(enc.GetString(auJob.FileContent));
                    //    speech.SetOutputToNull();
                    //    auJob.ResultContent = File.ReadAllBytes(tempfile);
                    //    if (File.Exists(tempfile))
                    //        File.Delete(tempfile);
                    //}
                    //else
                    //{
                    //send job to rabbitmq cluster
                    byte[] result = _auSender.SendAudioJobToQueue(auJob);

                    //get file from WEBSERVER2\Temp file system. this is where RBA16 placed the result
                    string outputPath = Encoding.UTF8.GetString(result);

                    //TODO uncomment this line when publishing to the SERVER
                    //note: it  may be C:\RoboBrailleWebApi\Temp instead of just temp.
                    outputPath = outputPath.Replace(@"C:\RoboBrailleWebApi", @"\\WEBSERVER2");
                    //outputPath = outputPath.Replace(@"C:", @"\\WEBSERVER2");
                    if (File.Exists(outputPath))
                    {
                        result = File.ReadAllBytes(outputPath);
                        File.Delete(outputPath);
                    }
                    else
                    {
                        result = null;
                    }

                    if (result == null)
                    {
                        RoboBrailleProcessor.SetJobFaulted(auJob, _context);
                        throw new Exception("Job result is null!");
                    }
                    else
                    {
                        auJob.ResultContent = result;
                    }
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.Message);
                    RoboBrailleProcessor.SetJobFaulted(auJob, _context);
                    throw ex;
                }

                string mime            = "audio/wav";
                string fileExtension   = ".wav";
                AudioFormat fmtOptions = auJob.FormatOptions;

                switch (fmtOptions)
                {
                case AudioFormat.Mp3:
                    mime          = "audio/mpeg3";
                    fileExtension = ".mp3";
                    break;

                case AudioFormat.Wav:
                    mime          = "audio/wav";
                    fileExtension = ".wav";
                    break;

                case AudioFormat.Aac:
                    mime          = "audio/aac";
                    fileExtension = ".aac";
                    break;

                default:
                    mime          = "audio/wav";
                    fileExtension = ".wav";
                    break;
                }
                try
                {
                    auJob.DownloadCounter     = 0;
                    auJob.ResultFileExtension = fileExtension;
                    auJob.ResultMimeType      = mime;
                    auJob.FinishTime          = DateTime.Now;
                    auJob.Status = JobStatus.Done;

                    _context.Jobs.Attach(auJob);
                    _context.Entry(auJob).State = EntityState.Modified;
                    _context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.Message);
                    throw ex;
                }
            }, job);

            return(job.Id);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Formats the braille text input based on the braille job parameters
        /// </summary>
        /// <param name="source">the input text</param>
        /// <param name="job">the BrailleJob class</param>
        /// <returns>the formatted text</returns>
        public static string FormatBraille(string source, BrailleJob job)
        {
            int cols = job.CharactersPerLine;
            int rows = job.LinesPerPage;
            //bool isPef = OutputFormat.Pef.Equals(job.OutputFormat);
            //bool isUni = OutputFormat.Unicode.Equals(job.OutputFormat);
            string result = source;

            try
            {
                Encoding enc = RoboBrailleProcessor.GetEncodingByCountryCode(job.BrailleLanguage);
                switch (job.OutputFormat)
                {
                case OutputFormat.Pef:
                {
                    result = RoboBrailleProcessor.ConvertBrailleToUnicode(enc.GetBytes(result));

                    if (cols > 10 && rows > 2 && !String.IsNullOrWhiteSpace(result))
                    {
                        result = RoboBrailleProcessor.CreatePagination(result, cols);
                        if (PageNumbering.none.Equals(job.PageNumbering))
                        {
                            result = RoboBrailleProcessor.CreatePefFromUnicode(result, cols, rows, 0, true);
                        }
                        else
                        {
                            result = RoboBrailleProcessor.CreatePefFromUnicodeWithPageNumber(result, cols, rows, 0, true, PageNumbering.right.Equals(job.PageNumbering));
                        }
                    }
                    break;
                }

                case OutputFormat.Unicode:
                {
                    result = RoboBrailleProcessor.ConvertBrailleToUnicode(enc.GetBytes(result));

                    if (cols > 10 && rows > 2 && !String.IsNullOrWhiteSpace(result))
                    {
                        result = RoboBrailleProcessor.CreatePagination(result, cols);
                        result = RoboBrailleProcessor.CreateNewLine(result, rows);
                    }
                    break;
                }

                case OutputFormat.NACB:
                {
                    result = RoboBrailleProcessor.ConvertBrailleToUnicode(enc.GetBytes(result));
                    result = enc.GetString(RoboBrailleProcessor.ConvertUnicodeToNACB(result)).ToLowerInvariant();

                    if (cols > 10 && rows > 2 && !String.IsNullOrWhiteSpace(result))
                    {
                        result = RoboBrailleProcessor.CreatePagination(result, cols);
                        result = RoboBrailleProcessor.CreateNewLine(result, rows);
                    }
                    break;
                }

                default:
                {
                    if (cols > 10 && rows > 2 && !String.IsNullOrWhiteSpace(result))
                    {
                        result = RoboBrailleProcessor.CreatePagination(result, cols);
                        result = RoboBrailleProcessor.CreateNewLine(result, rows);
                    }
                    break;
                }
                }
                return(result);
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.Message);
                return(source);
            }
        }
Ejemplo n.º 12
0
        public async Task <Guid> SubmitWorkItem(HTMLtoPDFJob job)
        {
            iTextSharp.text.Rectangle pageSize = PageSize.A4;

            if (job == null)
            {
                throw new Exception("html to pdf job is null");
            }

            switch (job.paperSize)
            {
            case PaperSize.a1:
                pageSize = PageSize.A1;
                break;

            case PaperSize.a2:
                pageSize = PageSize.A2;
                break;

            case PaperSize.a3:
                pageSize = PageSize.A3;
                break;

            case PaperSize.a4:
                pageSize = PageSize.A4;
                break;

            case PaperSize.a5:
                pageSize = PageSize.A5;
                break;

            case PaperSize.a6:
                pageSize = PageSize.A6;
                break;

            case PaperSize.a7:
                pageSize = PageSize.A7;
                break;

            case PaperSize.a8:
                pageSize = PageSize.A8;
                break;

            case PaperSize.a9:
                pageSize = PageSize.A9;
                break;

            case PaperSize.letter:
                pageSize = PageSize.LETTER;
                break;

            default:
                pageSize = PageSize.A4;
                break;
            }

            try
            {
                _context.Jobs.Add(job);
                _context.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                string errorMessages = string.Join("; ", ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage));
                throw new DbEntityValidationException(errorMessages);
            }

            var task = Task.Factory.StartNew(t =>
            {
                HTMLtoPDFJob ebJob    = (HTMLtoPDFJob)t;
                string htmlStream     = "";
                string outputFileName = Guid.NewGuid().ToString("N") + ".pdf";
                string tempfile       = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

                try
                {
                    File.WriteAllBytes(tempfile + ".html", job.FileContent);
                    htmlStream = new StreamReader(tempfile + ".html").ReadToEnd();

                    if (!string.IsNullOrEmpty(htmlStream))
                    {
                        var fsOut         = new MemoryStream();
                        var stringReader  = new StringReader(htmlStream);
                        Document document = new Document(pageSize, 5, 5, 5, 5);

                        //Document setup
                        var pdfWriter = PdfAWriter.GetInstance(document, fsOut);
                        pdfWriter.SetTagged();
                        pdfWriter.SetPdfVersion(PdfWriter.PDF_VERSION_1_7);
                        pdfWriter.CreateXmpMetadata();
                        pdfWriter.AddViewerPreference(PdfName.DISPLAYDOCTITLE, PdfBoolean.PDFTRUE);
                        pdfWriter.Info.Put(new PdfName("Producer"), new PdfString("©" + DateTime.Now.Year.ToString() + " Robobraille.org"));

                        //PDF/A-1A Conformance
                        //pdfWriter = PdfAWriter.GetInstance(document, fsOut, PdfAConformanceLevel.PDF_A_1A);

                        document.Open();
                        document.AddCreationDate();


                        //Custom tag processor
                        var tagProcessors = (DefaultTagProcessorFactory)Tags.GetHtmlTagProcessorFactory();
                        tagProcessors.AddProcessor(HTML.Tag.HTML, new HTMLTagProcessor(document));
                        tagProcessors.AddProcessor(HTML.Tag.TITLE, new HTMLTagProcessor(document));
                        tagProcessors.AddProcessor(HTML.Tag.TABLE, new TableTagProcessor());
                        tagProcessors.AddProcessor(HTML.Tag.TH, new THTagProcessor());

                        string colorProfilePath = null;
                        string defaultCSSPath   = null;
                        try
                        {
                            colorProfilePath = Path.Combine(HttpRuntime.AppDomainAppPath, "App_Data/colors/sRGB.profile");
                            defaultCSSPath   = Path.Combine(HttpRuntime.AppDomainAppPath, "App_Data/css/default.css");
                        }
                        catch (Exception e)
                        {
                            colorProfilePath = Path.Combine(ConfigurationManager.AppSettings.Get("ProjectDirectory"), "App_Data/colors/sRGB.profile");
                            defaultCSSPath   = Path.Combine(ConfigurationManager.AppSettings.Get("ProjectDirectory"), "App_Data/css/default.css");
                        }
                        //Setup color profile
                        var cssResolver = new StyleAttrCSSResolver();
                        if (colorProfilePath != null && defaultCSSPath != null)
                        {
                            ICC_Profile icc = ICC_Profile.GetInstance(new FileStream(colorProfilePath, FileMode.Open, FileAccess.Read));
                            pdfWriter.SetOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
                            var xmlWorkerHelper = XMLWorkerHelper.GetInstance();
                            cssResolver.AddCssFile(defaultCSSPath, true);// CSS with default style for all converted docs
                        }

                        //Register system fonts
                        var xmlWorkerFontProvider = new XMLWorkerFontProvider();
                        Environment.SpecialFolder specialFolder = Environment.SpecialFolder.Fonts;
                        string path = Environment.GetFolderPath(specialFolder);
                        DirectoryInfo directoryInfo = new DirectoryInfo(path);
                        FileInfo[] fontFiles        = directoryInfo.GetFiles();
                        foreach (var fontFile in fontFiles)
                        {
                            if (fontFile.Extension == ".ttf")
                            {
                                xmlWorkerFontProvider.Register(fontFile.FullName);
                            }
                        }

                        //HTML & CSS parsing
                        var cssAppliers = new CssAppliersImpl(xmlWorkerFontProvider);
                        var htmlContext = new HtmlPipelineContext(cssAppliers);
                        htmlContext.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(tagProcessors);
                        PdfWriterPipeline pdfWriterPipeline     = new PdfWriterPipeline(document, pdfWriter);
                        HtmlPipeline htmlPipeline               = new HtmlPipeline(htmlContext, pdfWriterPipeline);
                        CssResolverPipeline cssResolverPipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
                        XMLWorker xmlWorker = new XMLWorker(cssResolverPipeline, true);
                        XMLParser xmlParser = new XMLParser(xmlWorker);
                        xmlParser.Parse(stringReader);
                        document.Close();

                        //Data as byte array
                        byte[] fileData = fsOut.ToArray();

                        try
                        {
                            string mime               = "application/pdf";
                            string fileExtension      = ".pdf";
                            ebJob.DownloadCounter     = 0;
                            ebJob.ResultFileExtension = fileExtension;
                            ebJob.ResultMimeType      = mime;
                            ebJob.ResultContent       = fileData;
                            ebJob.Status              = JobStatus.Done;
                            ebJob.FinishTime          = DateTime.Now;
                            _context.Jobs.Attach(ebJob);
                            _context.Entry(job).State = EntityState.Modified;
                            _context.SaveChanges();
                        }
                        catch (Exception ex)
                        {
                            RoboBrailleProcessor.SetJobFaulted(job, _context);
                            throw ex;
                        }
                    }
                    else
                    {
                        RoboBrailleProcessor.SetJobFaulted(job, _context);
                        //Error No HTML file to convert
                        throw new Exception("Error No HTML file to convert");
                    }
                }
                catch (Exception e)
                {
                    RoboBrailleProcessor.SetJobFaulted(job, _context);
                    //ignore
                    throw e;
                }
            }, job);

            return(job.Id);
        }
        public Task <Guid> SubmitWorkItem(BrailleJob job)
        {
            if (job == null)
            {
                return(null);
            }

            try
            {
                _context.Jobs.Add(job);
                _context.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                string errorMessages = string.Join("; ", ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage));
                throw new DbEntityValidationException(errorMessages);
            }

            var task = Task.Factory.StartNew(t =>
            {
                try
                {
                    string strBraille = null;
                    if (!string.IsNullOrWhiteSpace(job.TranslationTable))
                    {
                        strBraille = this.LouisBrailleConvert(job);
                    }
                    else
                    {
                        switch (job.BrailleLanguage)
                        {
                        case Language.daDK:
                        //case Language.enUS:
                        case Language.enGB:
                        case Language.nnNO:
                        case Language.isIS:
                        case Language.svSE:
                        //strBraille = this.SensusBrailleConvert(job);
                        //break;
                        default:
                            strBraille = this.LouisBrailleConvert(job);
                            break;
                        }
                    }

                    if (strBraille != null)
                    {
                        strBraille   = RoboBrailleProcessor.FormatBraille(strBraille, job);
                        Encoding enc = RoboBrailleProcessor.GetEncodingByCountryCode(job.BrailleLanguage);
                        switch (job.OutputFormat)
                        {
                        case OutputFormat.Pef:
                            {
                                job.ResultContent = Encoding.UTF8.GetBytes(strBraille);
                                break;
                            }

                        case OutputFormat.Unicode:
                            {
                                job.ResultContent = Encoding.Unicode.GetBytes(strBraille);
                                break;
                            }

                        case OutputFormat.NACB:
                            {
                                job.ResultContent = enc.GetBytes(strBraille);
                                break;
                            }

                        default:
                            {
                                job.ResultContent = enc.GetBytes(strBraille);
                                break;
                            }
                        }
                    }
                    else
                    {
                        RoboBrailleProcessor.SetJobFaulted(job, _context);
                        throw new Exception("Braille conversion result is null!");
                    }
                    string fileExtension = ".txt";
                    string mime          = "text/plain";
                    string fileName      = job.FileName;
                    var outputFormat     = (OutputFormat)job.OutputFormat;

                    switch (outputFormat)
                    {
                    case OutputFormat.Pef:
                        mime          = "application/x-pef";
                        fileExtension = ".pef";
                        break;

                    default:
                        fileExtension = ".txt";
                        mime          = "text/plain";
                        break;
                    }
                    job.DownloadCounter     = 0;
                    job.ResultFileExtension = fileExtension;
                    job.ResultMimeType      = mime;
                    job.Status                = JobStatus.Done;
                    job.FinishTime            = DateTime.Now;
                    _context.Entry(job).State = EntityState.Modified;
                    _context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.Message);
                    RoboBrailleProcessor.SetJobFaulted(job, _context);
                    throw ex;
                }
            }, job);

            return(Task.FromResult(job.Id));
        }
        public async Task <Guid> SubmitWorkItem(AccessibleConversionJob accessibleJob)
        {
            try
            {
                _context.Jobs.Add(accessibleJob);
                _context.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                string errorMessages = string.Join("; ", ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage));
                throw new DbEntityValidationException(errorMessages);
            }

            // send the job to OCR server
            var task = Task.Factory.StartNew(j =>
            {
                var job = (AccessibleConversionJob)j;
                RSSoapServiceSoapClient wsClient;
                string flowName;

                string wsUri = WebConfigurationManager.AppSettings["AbbyyOCRServer"];
                string wName = WebConfigurationManager.AppSettings["RBWorkflowName"];

                try
                {
                    wsClient = new RSSoapServiceSoapClient();
                    // enumerate all workflows
                    var workflows = wsClient.GetWorkflows(wsUri);
                    flowName      = workflows.FirstOrDefault(e => e.Equals(wName));
                }
                catch (Exception e)
                {
                    Trace.WriteLine(e);
                    RoboBrailleProcessor.SetJobFaulted(job, _context);
                    throw e;
                }

                if (string.IsNullOrWhiteSpace(flowName))
                {
                    RoboBrailleProcessor.SetJobFaulted(job, _context);
                    throw new Exception("The conversion workflow does not exist!");
                }

                var fileContainer = new FileContainer {
                    FileContents = job.FileContent
                };

                var ticket = wsClient.CreateTicket(wsUri, flowName);
                var infile = new InputFile {
                    FileData = fileContainer
                };

                var fileformat = (OutputFileFormatEnum)Enum.Parse(typeof(OutputFileFormatEnum), Convert.ToString(job.TargetDocumentFormat));

                #region init ocr settings
                OutputFormatSettings ofs;

                switch (fileformat)
                {
                case OutputFileFormatEnum.OFF_PDF:
                    ofs = new PDFExportSettings
                    {
                        PDFExportMode         = PDFExportModeEnum.PEM_TextOnly,
                        PictureResolution     = -1,
                        Quality               = 70,
                        UseOriginalPaperSize  = true,
                        WriteTaggedPdf        = true,
                        IsEncryptionRequested = false,
                        FileFormat            = OutputFileFormatEnum.OFF_PDF,
                    };
                    break;

                case OutputFileFormatEnum.OFF_PDFA:
                    ofs = new PDFAExportSettings
                    {
                        Write1ACompliant     = true,
                        UseOriginalPaperSize = true,
                        Quality           = 70,
                        PictureResolution = -1,
                        PDFExportMode     = PDFExportModeEnum.PEM_ImageOnText,
                        FileFormat        = OutputFileFormatEnum.OFF_PDFA
                    };
                    break;

                case OutputFileFormatEnum.OFF_RTF:
                    ofs = new RTFExportSettings
                    {
                        ForceFixedPageSize = false,
                        HighlightErrorsWithBackgroundColor = false,
                        PaperHeight      = 16834,
                        PaperWidth       = 11909,
                        RTFSynthesisMode = RTFSynthesisModeEnum.RSM_EditableCopy,
                        WritePictures    = true,
                        FileFormat       = OutputFileFormatEnum.OFF_RTF
                    };
                    break;

                case OutputFileFormatEnum.OFF_Text:
                    ofs = new TextExportSettings
                    {
                        ExportParagraphsAsOneLine = true,
                        EncodingType = TextEncodingTypeEnum.TET_Simple,
                        KeepOriginalHeadersFooters = true,
                        FileFormat = OutputFileFormatEnum.OFF_Text
                    };
                    break;

                case OutputFileFormatEnum.OFF_UTF8:
                    ofs = new TextExportSettings
                    {
                        ExportParagraphsAsOneLine = true,
                        EncodingType = TextEncodingTypeEnum.TET_UTF8,
                        KeepOriginalHeadersFooters = true,
                        FileFormat = OutputFileFormatEnum.OFF_Text
                    };
                    break;

                case OutputFileFormatEnum.OFF_UTF16:
                    ofs = new TextExportSettings
                    {
                        ExportParagraphsAsOneLine = true,
                        EncodingType = TextEncodingTypeEnum.TET_UTF16,
                        KeepOriginalHeadersFooters = true,
                        FileFormat = OutputFileFormatEnum.OFF_Text
                    };
                    break;

                case OutputFileFormatEnum.OFF_MSWord:
                    ofs = new MSWordExportSettings
                    {
                        ForceFixedPageSize = false,
                        HighlightErrorsWithBackgroundColor = false,
                        RTFSynthesisMode = RTFSynthesisModeEnum.RSM_EditableCopy,
                        WritePictures    = true,
                        PaperHeight      = 16834,
                        PaperWidth       = 11909,
                        FileFormat       = OutputFileFormatEnum.OFF_MSWord
                    };
                    break;

                case OutputFileFormatEnum.OFF_HTML:
                    ofs = new HTMLExportSettings
                    {
                        EncodingType      = TextEncodingTypeEnum.TET_UTF8,
                        HTMLSynthesisMode = HTMLSynthesisModeEnum.HSM_PageLayout,
                        AllowCss          = true,
                        CodePage          = CodePageEnum.CP_Latin,
                        WritePictures     = true,
                        FileFormat        = OutputFileFormatEnum.OFF_HTML
                    };
                    break;

                case OutputFileFormatEnum.OFF_CSV:
                    ofs = new CSVExportSettings
                    {
                        CodePage                = CodePageEnum.CP_Latin,
                        EncodingType            = TextEncodingTypeEnum.TET_UTF8,
                        IgnoreTextOutsideTables = false,
                        TabSeparator            = ",",
                        UsePageBreaks           = false,
                        FileFormat              = OutputFileFormatEnum.OFF_CSV
                    };
                    break;

                case OutputFileFormatEnum.OFF_DOCX:
                    ofs = new DOCXExportSettings
                    {
                        ForceFixedPageSize = false,
                        HighlightErrorsWithBackgroundColor = false,
                        PaperHeight      = 16834,
                        PaperWidth       = 11909,
                        RTFSynthesisMode = RTFSynthesisModeEnum.RSM_EditableCopy,
                        WritePictures    = true,
                        FileFormat       = OutputFileFormatEnum.OFF_DOCX
                    };
                    break;

                case OutputFileFormatEnum.OFF_EPUB:
                    ofs = new EpubExportSettings
                    {
                        PictureResolution = -1,
                        Quality           = 70,
                        FileFormat        = OutputFileFormatEnum.OFF_EPUB
                    };
                    break;

                case OutputFileFormatEnum.OFF_MSExcel:
                    ofs = new XLExportSettings
                    {
                        ConvertNumericValuesToNumbers = true,
                        IgnoreTextOutsideTables       = false,
                        FileFormat = OutputFileFormatEnum.OFF_MSExcel
                    };
                    break;

                case OutputFileFormatEnum.OFF_XLSX:
                    ofs = new XLSXExportSettings
                    {
                        ConvertNumericValuesToNumbers = true,
                        IgnoreTextOutsideTables       = false,
                        FileFormat = OutputFileFormatEnum.OFF_XLSX
                    };
                    break;

                case OutputFileFormatEnum.OFF_XML:
                    ofs = new XMLExportSettings
                    {
                        PagesPerFile = 512,
                        WriteCharactersFormatting   = false,
                        WriteCharAttributes         = false,
                        WriteExtendedCharAttributes = false,
                        WriteNonDeskewedCoordinates = false,
                        FileFormat = OutputFileFormatEnum.OFF_XML
                    };
                    break;

                case OutputFileFormatEnum.OFF_TIFF:
                    ofs = new TiffExportSettings
                    {
                        ColorMode   = ImageColorModeEnum.ICM_AsIs,
                        Compression = ImageCompressionTypeEnum.ICT_Jpeg,
                        Resolution  = -1,
                        FileFormat  = OutputFileFormatEnum.OFF_TIFF
                    };
                    break;

                case OutputFileFormatEnum.OFF_JPG:
                    ofs = new JpegExportSettings
                    {
                        ColorMode  = ImageColorModeEnum.ICM_AsIs,
                        Quality    = 70,
                        Resolution = -1,
                        FileFormat = OutputFileFormatEnum.OFF_JPG
                    };
                    break;

                case OutputFileFormatEnum.OFF_InternalFormat:
                    ofs = new InternalFormatSettings
                    {
                        FileFormat = OutputFileFormatEnum.OFF_InternalFormat
                    };
                    break;

                default:
                    ofs = new TextExportSettings
                    {
                        EncodingType = TextEncodingTypeEnum.TET_UTF8,
                        FileFormat   = OutputFileFormatEnum.OFF_Text
                    };
                    break;
                }
                #endregion

                var formatset  = new[] { ofs };
                var inputFiles = new[] { infile };

                ticket.ExportParams.Formats = formatset;
                ticket.InputFiles           = inputFiles;
                ticket.Priority             = job.Priority;

                XmlResult result;
                try
                {
                    result = wsClient.ProcessTicket(wsUri, flowName, ticket);
                }
                catch (Exception ex)
                {
                    RoboBrailleProcessor.SetJobFaulted(job, _context);
                    throw ex;
                }

                if (result.IsFailed)
                {
                    RoboBrailleProcessor.SetJobFaulted(job, _context);
                    throw new Exception("Conversion job failed!");
                }

                byte[] contents         = null;
                List <byte[]> contents2 = new List <byte[]>();
                foreach (var ifile in result.InputFiles)
                {
                    foreach (var odoc in ifile.OutputDocuments)
                    {
                        foreach (var ofile in odoc.Files)
                        {
                            contents = ofile.FileContents;
                            if (ofs.FileFormat == OutputFileFormatEnum.OFF_InternalFormat)
                            {
                                contents2.Add(ofile.FileContents);
                            }
                        }
                    }
                }
                if (ofs.FileFormat == OutputFileFormatEnum.OFF_InternalFormat)
                {
                    int i           = 0;
                    string filePath = ConfigurationManager.AppSettings.Get("FileDirectory") + @"Temp\" + job.Id;
                    Directory.CreateDirectory(filePath);
                    foreach (var f in contents2)
                    {
                        File.WriteAllBytes(filePath + @"\ocrData" + i, f);
                        i++;
                    }
                    ZipFile.CreateFromDirectory(filePath, filePath + ".zip");
                    contents = File.ReadAllBytes(filePath + ".zip");
                    Directory.Delete(filePath);
                    File.Delete(filePath + ".zip");
                }

                if (contents == null)
                {
                    RoboBrailleProcessor.SetJobFaulted(job, _context);
                    throw new Exception("Job result is null!");
                }
                string mime;
                string fileExtension = ".txt";
                switch (fileformat)
                {
                case OutputFileFormatEnum.OFF_PDF:
                    mime          = "application/pdf";
                    fileExtension = ".pdf";
                    break;

                case OutputFileFormatEnum.OFF_PDFA:
                    mime          = "application/pdf";
                    fileExtension = ".pdf";
                    break;

                case OutputFileFormatEnum.OFF_RTF:
                    mime          = "text/rtf";
                    fileExtension = ".rtf";
                    break;

                case OutputFileFormatEnum.OFF_Text:
                    mime          = "text/plain";
                    fileExtension = ".txt";
                    break;

                case OutputFileFormatEnum.OFF_MSWord:
                    mime          = "application/msword";
                    fileExtension = ".doc";
                    break;

                case OutputFileFormatEnum.OFF_HTML:
                    mime          = "text/html";
                    fileExtension = ".html";
                    break;

                case OutputFileFormatEnum.OFF_CSV:
                    mime          = "text/csv";
                    fileExtension = ".csv";
                    break;

                case OutputFileFormatEnum.OFF_DOCX:
                    mime          = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                    fileExtension = ".docx";
                    break;

                case OutputFileFormatEnum.OFF_EPUB:
                    mime          = "application/epub+zip";
                    fileExtension = ".epub";
                    break;

                case OutputFileFormatEnum.OFF_MSExcel:
                    mime          = "application/vnd.ms-excel";
                    fileExtension = ".xls";
                    break;

                case OutputFileFormatEnum.OFF_XLSX:
                    mime          = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    fileExtension = ".xlsx";
                    break;

                case OutputFileFormatEnum.OFF_XML:
                    mime          = "application/xml";
                    fileExtension = ".xml";
                    break;

                case OutputFileFormatEnum.OFF_JPG:
                    mime          = "image/jpeg";
                    fileExtension = ".jpg";
                    break;

                case OutputFileFormatEnum.OFF_TIFF:
                    mime          = "image/tiff";
                    fileExtension = ".tiff";
                    break;

                case OutputFileFormatEnum.OFF_InternalFormat:
                    mime          = "application/zip";
                    fileExtension = ".zip";
                    break;

                default:
                    mime          = "text/plain";
                    fileExtension = ".txt";
                    break;
                }

                try
                {
                    job.DownloadCounter     = 0;
                    job.ResultFileExtension = fileExtension;
                    job.ResultMimeType      = mime;
                    job.ResultContent       = contents;
                    job.Status     = JobStatus.Done;
                    job.FinishTime = DateTime.Now;
                    _context.Jobs.Attach(job);
                    _context.Entry(job).State = EntityState.Modified;
                    _context.SaveChanges();
                }
                catch (Exception e)
                {
                    RoboBrailleProcessor.SetJobFaulted(job, _context);
                    throw e;
                }
            }, accessibleJob);

            return(accessibleJob.Id);
        }
Ejemplo n.º 15
0
        private bool SubtitleDownloadAndSave(AmaraSubtitleJob viJob)
        {
            string mime          = "text/plain";
            string fileExtension = ".txt";

            byte[] result = vjp.DownloadSubtitle(viJob);
            if (result == null)
            {
                RoboBrailleProcessor.SetJobFaulted(viJob, _context);
                return(false);
            }
            else
            {
                viJob.ResultContent = result;
            }
            var fmtOptions = viJob.SubtitleFormat;

            switch (fmtOptions)
            {
            case "srt":
                mime          = "text/srt";
                fileExtension = ".srt";
                break;

            case "txt":
                mime          = "text/plain";
                fileExtension = ".txt";
                break;

            case "dfxp":
                mime          = "application/ttml+xml";
                fileExtension = ".dfxp";
                break;

            case "sbv":
                mime          = "text/sbv";
                fileExtension = ".sbv";
                break;

            case "ssa":
                mime          = "text/ssa";
                fileExtension = ".ssa";
                break;

            case "vtt":
                mime          = "text/vtt";
                fileExtension = ".vtt";
                break;

            default:
                mime          = "text/srt";
                fileExtension = ".srt";
                break;
            }
            try
            {
                viJob.DownloadCounter     = 0;
                viJob.ResultFileExtension = fileExtension;
                viJob.ResultMimeType      = mime;
                viJob.FinishTime          = DateTime.Now;
                viJob.Status = JobStatus.Done;
                _context.Jobs.Attach(viJob);
                _context.Entry(viJob).State = EntityState.Modified;
                _context.SaveChanges();
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
        public async System.Threading.Tasks.Task <Guid> SubmitWorkItem(OcrConversionJob job)
        {
            try
            {
                _context.Jobs.Add(job);
                _context.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                string errorMessages = string.Join("; ", ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage));
                throw new DbEntityValidationException(errorMessages);
            }
            var task = Task.Factory.StartNew(t =>
            {
                try
                {
                    string mime          = "text/plain";
                    string fileExtension = ".txt";
                    if (job.HasTable)
                    {
                        job           = PostToCerth(job).Result;
                        mime          = "text/html";
                        fileExtension = ".html";
                    }
                    else
                    {
                        if (!supportedLangs.ContainsKey(job.OcrLanguage))
                        {
                            throw new Exception("The input language is not supported by the ocr conversion tool");
                        }
                        using (var engine = new TesseractEngine(ConfigurationManager.AppSettings["TessDataPath"], supportedLangs[job.OcrLanguage], EngineMode.Default))
                        {
                            MemoryStream stream = new MemoryStream();
                            stream.Write(job.FileContent, 0, job.FileContent.Length);
                            // have to load Pix via a bitmap since Pix doesn't support loading a stream.
                            using (var image = new System.Drawing.Bitmap(stream))
                            {
                                using (var pix = PixConverter.ToPix(image))
                                {
                                    using (var page = engine.Process(pix))
                                    {
                                        job.ResultContent = Encoding.UTF8.GetBytes(page.GetText());
                                    }
                                }
                            }
                        }
                        mime          = "text/plain";
                        fileExtension = ".txt";
                    }
                    job.DownloadCounter     = 0;
                    job.ResultFileExtension = fileExtension;
                    job.ResultMimeType      = mime;
                    job.Status                = JobStatus.Done;
                    job.FinishTime            = DateTime.Now;
                    _context.Entry(job).State = EntityState.Modified;
                    _context.SaveChanges();
                }
                catch (Exception ex)
                {
                    RoboBrailleProcessor.SetJobFaulted(job, _context);
                    throw ex;
                }
            }, job);

            return(job.Id);
        }
Ejemplo n.º 17
0
        public async System.Threading.Tasks.Task <Guid> SubmitWorkItem(AmaraSubtitleJob job)
        {
            try
            {
                if (job.FileContent != null)
                {
                    job.VideoUrl = vjp.CreateVideoUrl(job);
                }
                job.DownloadCounter = 0;
                job.Status          = JobStatus.Started;
                try
                {
                    _context.Jobs.Add(job);
                    _context.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    string errorMessages = string.Join("; ", ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage));
                    throw new DbEntityValidationException(errorMessages);
                }
            }
            catch (Exception ex)
            {
                RoboBrailleProcessor.SetJobFaulted(job, _context);
                throw ex;
            }

            var task = Task.Factory.StartNew(t =>
            {
                AmaraSubtitleJob viJob = (AmaraSubtitleJob)t;
                try
                {
                    byte[] result = null;
                    if (!String.IsNullOrWhiteSpace(viJob.VideoUrl))
                    {
                        SubtitleInfo si      = vjp.PostVideo(viJob);
                        viJob.AmaraVideoId   = si.VideoId;
                        string mime          = "text/plain";
                        string fileExtension = ".txt";
                        switch (si.Status)
                        {
                        case VideoSubtitleStatus.SubtitleRequested:
                            result = Encoding.UTF8.GetBytes("Video exists but subtitle does not. Contact amara.org to request a subtitle!" + Environment.NewLine +
                                                            "Amara video id: " + viJob.AmaraVideoId + Environment.NewLine +
                                                            "RoboBraille job id: " + viJob.Id
                                                            );
                            viJob.ResultContent = result;
                            try
                            {
                                viJob.DownloadCounter     = 0;
                                viJob.ResultFileExtension = fileExtension;
                                viJob.ResultMimeType      = mime;
                                viJob.FinishTime          = DateTime.Now;
                                viJob.Status = JobStatus.Queued;
                                _context.Jobs.Attach(viJob);
                                _context.Entry(viJob).State = EntityState.Modified;
                                _context.SaveChanges();
                            }
                            catch (Exception ex)
                            {
                                Trace.WriteLine(ex.Message);
                                RoboBrailleProcessor.SetJobFaulted(job, _context);
                                throw ex;
                            }
                            break;

                        case VideoSubtitleStatus.Error:
                            viJob.ResultContent = Encoding.UTF8.GetBytes("Subtitle could not be created. Error!");
                            RoboBrailleProcessor.SetJobFaulted(viJob, _context);
                            break;

                        case VideoSubtitleStatus.Exists:
                            SubtitleDownloadAndSave(viJob);
                            break;

                        case VideoSubtitleStatus.Submitted:
                            result = Encoding.UTF8.GetBytes("Video with id " + si.VideoId + " has been submitted for manual subtitling. Make sure to check when it's ready!");
                            viJob.ResultContent = result;
                            try
                            {
                                viJob.DownloadCounter     = 0;
                                viJob.ResultFileExtension = fileExtension;
                                viJob.ResultMimeType      = mime;
                                viJob.FinishTime          = DateTime.Now;
                                viJob.Status = JobStatus.Processing;
                                _context.Jobs.Attach(viJob);
                                _context.Entry(viJob).State = EntityState.Modified;
                                _context.SaveChanges();
                            }
                            catch (Exception ex)
                            {
                                Trace.WriteLine(ex.Message);
                                RoboBrailleProcessor.SetJobFaulted(job, _context);
                                throw ex;
                            }
                            break;

                        case VideoSubtitleStatus.Complete:
                            viJob.ResultContent = Encoding.UTF8.GetBytes("Subtitle is complete.");
                            break;     //not handled yet

                        case VideoSubtitleStatus.NotComplete:
                            result = Encoding.UTF8.GetBytes("Video with id " + si.VideoId + " already exists but the subtitle is not complete. Make sure to check when it's complete!");
                            viJob.ResultContent = result;
                            try
                            {
                                viJob.DownloadCounter     = 0;
                                viJob.ResultFileExtension = fileExtension;
                                viJob.ResultMimeType      = mime;
                                viJob.FinishTime          = DateTime.Now;
                                viJob.Status = JobStatus.Processing;
                                _context.Jobs.Attach(viJob);
                                _context.Entry(viJob).State = EntityState.Modified;
                                _context.SaveChanges();
                            }
                            catch (Exception ex)
                            {
                                Trace.WriteLine(ex.Message);
                                RoboBrailleProcessor.SetJobFaulted(job, _context);
                                throw ex;
                            }
                            break;

                        default: break;
                        }
                    }
                    else
                    {
                        throw new Exception("No video url");
                    }
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.Message);
                    RoboBrailleProcessor.SetJobFaulted(job, _context);
                    throw ex;
                }
            }, job);

            return(job.Id);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Reads the input from the POST requests and creates the appropriate job instance
        /// </summary>
        /// <param name="type"></param>
        /// <param name="readStream"></param>
        /// <param name="content"></param>
        /// <param name="formatterLogger"></param>
        /// <returns></returns>
        public override async Task <object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
        {
            if (content == null)
            {
                return(null);
            }

            var tempStorage         = Path.GetTempPath();
            var tempStorageProvider = new MultipartFormDataStreamProvider(tempStorage);
            var msp = await content.ReadAsMultipartAsync(tempStorageProvider);

            Job job = null;

            if (type == typeof(AccessibleConversionJob))
            {
                job = new AccessibleConversionJob
                {
                    TargetDocumentFormat = (OutputFileFormatEnum)Enum.Parse(typeof(OutputFileFormatEnum), msp.FormData["targetdocumentformat"])//Convert.ToInt32(msp.FormData["targetformat"]),
                };

                if (msp.FormData.AllKeys.Contains("priority"))
                {
                    ((AccessibleConversionJob)job).Priority = (PriorityEnum)Enum.Parse(typeof(PriorityEnum), msp.FormData["priority"]);
                }
                else
                {
                    ((AccessibleConversionJob)job).Priority = PriorityEnum.P_Normal;
                }
            }
            else if (type == typeof(AudioJob))
            {
                job = new AudioJob()
                {
                    AudioLanguage = (Language)Enum.Parse(typeof(Language), msp.FormData["audiolanguage"]),
                    SpeedOptions  = (AudioSpeed)Enum.Parse(typeof(AudioSpeed), msp.FormData["speedoptions"]),
                    FormatOptions = (AudioFormat)Enum.Parse(typeof(AudioFormat), msp.FormData["formatoptions"])
                };
                if (msp.FormData.AllKeys.Contains("voicepropriety"))
                {
                    string[] props = msp.FormData["voicepropriety"].Split(':');
                    List <VoicePropriety> propList = new List <VoicePropriety>();
                    foreach (string prop in props)
                    {
                        propList.Add((VoicePropriety)Enum.Parse(typeof(VoicePropriety), prop));
                    }
                    ((AudioJob)job).VoicePropriety = propList;
                }
                else
                {
                    ((AudioJob)job).VoicePropriety = new List <VoicePropriety>()
                    {
                        VoicePropriety.None
                    }
                };
            }
            else if (type == typeof(BrailleJob))
            {
                job = new BrailleJob
                {
                    BrailleFormat     = (BrailleFormat)Enum.Parse(typeof(BrailleFormat), msp.FormData["brailleformat"]),
                    BrailleLanguage   = (Language)Enum.Parse(typeof(Language), msp.FormData["language"]),
                    Contraction       = (BrailleContraction)Enum.Parse(typeof(BrailleContraction), msp.FormData["contraction"]),
                    OutputFormat      = (OutputFormat)Enum.Parse(typeof(OutputFormat), msp.FormData["outputformat"]),
                    ConversionPath    = (ConversionPath)int.Parse(msp.FormData["conversionpath"]),
                    CharactersPerLine = int.Parse(msp.FormData["charactersperline"]),
                    LinesPerPage      = int.Parse(msp.FormData["linesperpage"])
                };

                if (msp.FormData.AllKeys.Contains("pagenumbering"))
                {
                    ((BrailleJob)job).PageNumbering = (PageNumbering)int.Parse(msp.FormData["pagenumbering"]);
                }
            }
            else if (type == typeof(DaisyJob))
            {
                job = new DaisyJob
                {
                    DaisyOutput = (DaisyOutput)Enum.Parse(typeof(DaisyOutput), msp.FormData["daisyoutput"])
                };
            }
            else if (type == typeof(EBookJob))
            {
                job = new EBookJob
                {
                    EbookFormat = (EbookFormat)Enum.Parse(typeof(EbookFormat), msp.FormData["format"])
                };
                if (msp.FormData.AllKeys.Contains("basefontsize"))
                {
                    ((EBookJob)job).BaseFontSize = (EbookBaseFontSize)Enum.Parse(typeof(EbookBaseFontSize), msp.FormData["basefontsize"]);
                }
            }
            else if (type == typeof(HTMLtoPDFJob))
            {
                job = new HTMLtoPDFJob
                {
                    paperSize = (PaperSize)Enum.Parse(typeof(PaperSize), msp.FormData["size"])
                };
            }
            else if (type == typeof(MSOfficeJob))
            {
                job = new MSOfficeJob
                {
                    MSOfficeOutput = (MSOfficeOutput)Enum.Parse(typeof(MSOfficeOutput), msp.FormData["msofficeoutput"])
                };
                if (msp.FormData.AllKeys.Contains("subtitlelangauge") && msp.FormData.AllKeys.Contains("subtitleformat"))
                {
                    ((MSOfficeJob)job).SubtitleLangauge = msp.FormData["subtitlelangauge"];
                    ((MSOfficeJob)job).SubtitleFormat   = msp.FormData["subtitleformat"];
                }
            }
            else if (type == typeof(OcrConversionJob))
            {
                job = new OcrConversionJob
                {
                    OcrLanguage = (Language)Enum.Parse(typeof(Language), msp.FormData["language"]),
                    HasTable    = false
                };
                if (msp.FormData.AllKeys.Contains("hastable"))
                {
                    ((OcrConversionJob)job).HasTable = bool.Parse(msp.FormData["hastable"]);
                }
            }
            else if (type == typeof(HTMLToTextJob))
            {
                job = new HTMLToTextJob();
            }
            else if (type == typeof(AmaraSubtitleJob))
            {
                job = new AmaraSubtitleJob()
                {
                    SubtitleLangauge = msp.FormData["language"],
                    SubtitleFormat   = msp.FormData["format"]
                };
                if (msp.FormData.AllKeys.Contains("videourl"))
                {
                    ((AmaraSubtitleJob)job).VideoUrl = msp.FormData["videourl"];
                }
            }
            else if (type == typeof(DocumentStructureJob))
            {
                job = new DocumentStructureJob()
                {
                };
            }
            else if (type == typeof(TranslationJob))
            {
                job = new TranslationJob()
                {
                    SourceLanguage = msp.FormData["sourcelanguage"],
                    TargetLanguage = msp.FormData["targetlanguage"]
                };
            }
            else if (type == typeof(SignLanguageJob))
            {
                job = new SignLanguageJob()
                {
                    SourceTextLanguage = msp.FormData["sourcetextlanguage"],
                    TargetSignLanguage = msp.FormData["targetsignlanguage"]
                };
            }

            if (job == null)
            {
                return(null);
            }
            else
            {
                if (msp.FileData.Count > 0)
                {
                    job.FileContent = File.ReadAllBytes(msp.FileData[0].LocalFileName);
                    string fileName = msp.FileData[0].Headers.ContentDisposition.FileName.Replace("\"", "").ToString();
                    job.FileName      = fileName.Substring(0, fileName.LastIndexOf("."));
                    job.FileExtension = fileName.Substring(fileName.LastIndexOf(".") + 1);
                    job.MimeType      = msp.FileData[0].Headers.ContentType.MediaType;
                }
                else if (msp.FormData["lastjobid"] != null)
                {
                    //1) check if a guid is present and exists in the database. 2) get the byte content from the database and use it in the new job
                    Guid previousJobId = Guid.Parse(msp.FormData["lastjobid"]);
                    RoboBrailleDataContext _context = new RoboBrailleDataContext();
                    Job previousJob = RoboBrailleProcessor.CheckForJobInDatabase(previousJobId, _context);
                    if (previousJob != null)
                    {
                        job.FileContent   = previousJob.ResultContent;
                        job.FileName      = previousJob.FileName;
                        job.FileExtension = previousJob.ResultFileExtension;
                        job.MimeType      = previousJob.ResultMimeType;
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    //if not assume it is a URL
                    if (!String.IsNullOrWhiteSpace(msp.FormData["FileContent"]))
                    {
                        string sorh = msp.FormData["FileContent"];
                        Uri    uriResult;
                        bool   result = Uri.TryCreate(sorh, UriKind.Absolute, out uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
                        if (result)
                        {
                            var webRequest = WebRequest.Create(uriResult);

                            using (var response = webRequest.GetResponse())
                                using (var httpcontent = response.GetResponseStream())
                                    using (var reader = new StreamReader(httpcontent))
                                    {
                                        job.FileContent   = Encoding.UTF8.GetBytes(reader.ReadToEnd());
                                        job.FileName      = DateTime.Now.Ticks + "-RoboFile";
                                        job.FileExtension = ".html";
                                        job.MimeType      = "text/html";
                                    }
                        }
                        else //else it must be a file
                        {
                            job.FileContent   = Encoding.UTF8.GetBytes(sorh);
                            job.FileName      = DateTime.Now.Ticks + "-RoboFile";
                            job.FileExtension = ".txt";
                            job.MimeType      = "text/plain";
                        }
                    }
                    else
                    {
                        //it's a video job
                    }
                }
                if (type != typeof(AmaraSubtitleJob))
                {
                    job.InputFileHash = RoboBrailleProcessor.GetMD5Hash(job.FileContent);
                }
                job.Status     = JobStatus.Started;
                job.SubmitTime = DateTime.Now;
                return(job);
            }
        }
Ejemplo n.º 19
0
        public async Task <Guid> SubmitWorkItem(EBookJob job)
        {
            var outputFormat = "." + job.EbookFormat.ToString().ToLowerInvariant();

            try
            {
                _context.Jobs.Add(job);
                _context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            var task = Task.Factory.StartNew(t =>
            {
                EBookJob ebJob  = (EBookJob)t;
                string tempfile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
                File.WriteAllBytes(tempfile + "." + ebJob.FileExtension, ebJob.FileContent);

                string cmdArgs = " --book-producer=\"Sensus\" --change-justification=\"left\"";
                if (ebJob.EbookFormat.Equals(EbookFormat.epub))
                {
                    cmdArgs += " --preserve-cover-aspect-ratio";
                }
                if (ebJob.EbookFormat.Equals(EbookFormat.mobi))
                {
                    cmdArgs += " --enable-heuristics";
                }
                if (ebJob.EbookFormat.Equals(EbookFormat.html))
                {
                    cmdArgs = "";
                }
                switch (ebJob.BaseFontSize)
                {
                case EbookBaseFontSize.LARGE:
                    cmdArgs += " --base-font-size=\"16\"  --font-size-mapping=\"12,14,16,18,20,22,24,28\"";
                    break;

                case EbookBaseFontSize.XLARGE:
                    cmdArgs += " --base-font-size=\"24\"  --font-size-mapping=\"18,20,24,26,28,30,32,36\"";
                    break;

                case EbookBaseFontSize.HUGE:
                    cmdArgs += " --base-font-size=\"40\"  --font-size-mapping=\"32,36,40,42,48,56,60,72\"";
                    break;

                default:
                    break;
                }

                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.WorkingDirectory = calibre;
                startInfo.CreateNoWindow   = true;
                startInfo.UseShellExecute  = true;
                startInfo.FileName         = "ebook-convert.exe";
                startInfo.WindowStyle      = ProcessWindowStyle.Hidden;
                startInfo.Arguments        = tempfile + "." + ebJob.FileExtension + " " + tempfile + outputFormat + cmdArgs;

                try
                {
                    using (Process exeProcess = Process.Start(startInfo))
                    {
                        exeProcess.WaitForExit();
                    }
                }
                catch (Exception e)
                {
                    RoboBrailleProcessor.SetJobFaulted(ebJob, _context);
                    throw e;
                }
                finally
                {
                    try
                    {
                        EbookFormat fmtOptions = ebJob.EbookFormat;
                        string mime            = "application/epub+zip";
                        string fileExtension   = ".epub";
                        switch (fmtOptions)
                        {
                        case EbookFormat.epub:
                            mime          = "application/epub+zip";
                            fileExtension = ".epub";
                            break;

                        case EbookFormat.mobi:
                            mime          = "application/x-mobipocket-ebook";
                            fileExtension = ".prc";
                            break;

                        case EbookFormat.txt:
                            mime          = "plain/text";
                            fileExtension = ".txt";
                            break;

                        case EbookFormat.rtf:
                            mime          = "application/rtf";
                            fileExtension = ".rtf";
                            break;

                        case EbookFormat.html:
                            mime          = "text/html";
                            fileExtension = ".html";
                            break;

                        case EbookFormat.chm:
                            mime          = "application/vnd.ms-htmlhelp";
                            fileExtension = ".chm";
                            break;

                        case EbookFormat.docx:
                            mime          = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                            fileExtension = ".docx";
                            break;

                        default:
                            mime          = "application/epub+zip";
                            fileExtension = ".epub";
                            break;
                        }
                        if (File.Exists(tempfile + outputFormat))
                        {
                            ebJob.ResultContent       = File.ReadAllBytes(tempfile + outputFormat);
                            ebJob.DownloadCounter     = 0;
                            ebJob.ResultMimeType      = mime;
                            ebJob.ResultFileExtension = fileExtension;
                            ebJob.Status     = JobStatus.Done;
                            ebJob.FinishTime = DateTime.Now;
                            _context.Jobs.Attach(ebJob);
                            _context.Entry(ebJob).State = EntityState.Modified;
                            _context.SaveChanges();
                            File.Delete(tempfile + outputFormat);
                        }
                        else
                        {
                            RoboBrailleProcessor.SetJobFaulted(ebJob, _context);
                            throw new Exception("Result file does not exist!");
                        }
                        if (File.Exists(tempfile + "." + ebJob.FileExtension))
                        {
                            File.Delete(tempfile + "." + ebJob.FileExtension);
                        }
                    }
                    catch (Exception ex)
                    {
                        RoboBrailleProcessor.SetJobFaulted(ebJob, _context);
                        throw ex;
                    }
                }
            }, job);

            return(job.Id);
        }