public byte[] TestEbookAPI(string format, string testDocumentsName) { var apiFileContent = inputFiles.Where(x => x.Key.EndsWith(testDocumentsName)).Select(x => x.Value).First(); var ftpFilePath = inputFiles.Where(x => x.Key.EndsWith(testDocumentsName)).Select(x => x.Key).First(); var fileExtension = testDocumentsName.Substring(testDocumentsName.LastIndexOf(".") + 1); EbookFormat ef = (EbookFormat)Enum.Parse(typeof(EbookFormat), format.ToLowerInvariant()); switch (format) { case "EPUB": ef = EbookFormat.epub; break; case "MOBI": ef = EbookFormat.mobi; break; case "RTF": ef = EbookFormat.rtf; break; case "TXT": ef = EbookFormat.txt; break; default: break; } var ebookj = new EBookJob() { Id = Guid.NewGuid(), FileContent = apiFileContent, UserId = Guid.Parse("d2b97532-e8c5-e411-8270-f0def103cfd0"), FileExtension = fileExtension, FileName = "testEbook", MimeType = "plain/text", Status = JobStatus.Started, SubmitTime = DateTime.Now, DownloadCounter = 0, InputFileHash = RoboBrailleProcessor.GetMD5Hash(apiFileContent), EbookFormat = ef }; var apiTask = Task.Run(() => WebAPICall(ebookj)); byte[] apiRes = apiTask.Result; return(apiRes); }
public static IBookLoader GetBookLoader(string filename) { if (!string.IsNullOrEmpty(filename)) { EbookFormat ebookFormat = EbookFormat.Epub; if (filename.EndsWith(".txt")) { ebookFormat = EbookFormat.Txt; } if (filename.EndsWith(".html") || filename.EndsWith(".htm") || filename.EndsWith(".xhtml") || filename.EndsWith(".xhtm")) { ebookFormat = EbookFormat.Html; } return(GetBookLoader(ebookFormat)); } throw new UnknownFileFormatException(filename); }
public static IBookLoader GetBookLoader(EbookFormat format) { return(IocManager.Container.ResolveKeyed <IBookLoader>(format)); }
public void TestEbook() { //setup resultPath = InputSourceRepository.GetTestResultDirectory() + @"\Ebook-" + format; Directory.CreateDirectory(resultPath); InputSourceRepository isr = new InputSourceRepository(); string testDocumentsPath = ""; Job ebookj = null; if (format.Equals("EPUB3WMO")) { testDocumentsPath = InputSourceRepository.GetTestDirectory() + "A1.Daisy.docx"; File.Copy(testDocumentsPath + "", daisyTest); ebookj = new DaisyJob() { Id = Guid.NewGuid(), FileContent = File.ReadAllBytes(daisyTest), UserId = Guid.Parse("d2b97532-e8c5-e411-8270-f0def103cfd0"), FileExtension = ".txt", FileName = "testDaisyEPUB3WMO", MimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document", Status = JobStatus.Started, SubmitTime = DateTime.Now, DownloadCounter = 0, InputFileHash = new byte[8], DaisyOutput = DaisyOutput.Epub3WMO }; } else { testDocumentsPath = InputSourceRepository.GetTestDirectory() + "A6testPDF.pdf"; File.Copy(testDocumentsPath + "", ebookTest); EbookFormat ef = EbookFormat.mobi; if (format.Equals("EPUB")) { ef = EbookFormat.epub; } else { ef = EbookFormat.mobi; } ebookj = new EBookJob() { Id = Guid.NewGuid(), FileContent = File.ReadAllBytes(ebookTest), UserId = Guid.Parse("d2b97532-e8c5-e411-8270-f0def103cfd0"), FileExtension = ".txt", FileName = "testEbook", MimeType = "plaint/text", Status = JobStatus.Started, SubmitTime = DateTime.Now, DownloadCounter = 0, InputFileHash = new byte[8], EbookFormat = ef }; } string destFile = Guid.NewGuid().ToString() + ".txt"; SensusRequest sr = new SensusRequest() { Process = "E-book", SubProcess = format, Option = "", //normal = none large xlarge huge Language = "", Gender = "", Age = "", Prefix = "", RequesterID = "sensus-test", FTPServer = "2.109.50.19", FTPUser = "******", FTPPassword = "******", SourcePath = ebookTest, DestinationFile = destFile }; //act var apiTask = Task.Run(() => WebAPICall(ebookj)); //var ftpTask = Task.Run(() => FTPCall(sr)); //Task.WaitAll(new Task[] { apiTask, ftpTask }); byte[] apiRes = apiTask.Result; //byte[] ftpRes = ftpTask.Result; //assert NUnit.Framework.Assert.IsNotNull(apiRes); //NUnit.Framework.Assert.IsNotNull(ftpRes); //string expected = RoboBrailleProcessor.GetEncodingByCountryCode((Language)Enum.Parse(typeof(Language), language, true)).GetString(ftpRes).Trim(); //string result = Encoding.UTF8.GetString(apiRes).Trim(); //byte assertion fails because the files are not the same encoding File.WriteAllBytes(resultPath + @"\api.epub", apiRes); //File.WriteAllBytes(resultPath + @"\ftp.epub", ftpRes); //NUnit.Framework.Assert.AreEqual(ftpRes, apiRes); //NUnit.Framework.Assert.AreEqual(expected, result); }
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); }