示例#1
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);
        }
示例#2
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);
        }
示例#4
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);
        }
示例#5
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 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);
        }
示例#8
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);
        }
示例#9
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);
            }
        }
示例#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);
        }
        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);
        }
        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);
        }
示例#13
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);
        }