BQModuleRepository implements IModuleRepository<String, BQModule> { String cacheFileFromArchive(BQModule module, String currLine) { if (!module.isArchive()) { return currLine; } Pattern pattern = Pattern.compile("<img.+?src\\s*?=\\s*['\"](.+?)[\"']>", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(currLine); if (!matcher.find() && matcher.groupCount() < 2) { return currLine; } StringBuilder result = new StringBuilder(currLine); for (int i = 1; i < matcher.groupCount() + 1; i++) { String path = matcher.group(i); if (path == null || path.isEmpty()) { continue; } try (InputStream imageStream = getImageReader(module, path)) { byte[] bytes = fsUtils.getBytes(imageStream); if (bytes == null) { continue; } String data = Base64.encodeToString(bytes, Base64.NO_WRAP); String ext = FilenameUtils.getExtension(path); result.replace(matcher.start(i), matcher.end(i), String.format(Locale.US, "data:image/%s;base64,%s", ext, data)); } catch (Exception ex) { StaticLogger.error(this, "", ex); return currLine; } } return result.toString(); } BQModuleRepository(FsUtilsWrapper fsUtils); @Override Bitmap getBitmap(BQModule module, String path); @Override BQModule loadModule(String path); @Override Chapter loadChapter(BQModule module, String bookID, int chapter); @Override @NonNull String getBookContent(BQModule module, String bookID);  }
@Test public void convertImagePathsToBase64WithAltInEnd() throws Exception { String result1 = repository.cacheFileFromArchive(module, "test string with <img src=\"bible.png\" alt=\"bible\"> and other symbols"); assertNotNull(result1); assertEquals(27921, result1.length()); }
@Test public void convertImagePathsToBase64WithAltInStart() throws Exception { String result2 = repository.cacheFileFromArchive(module, "test string with <img alt=\"bible\" src=\"bible.png\"> and other symbols"); assertNotNull(result2); assertEquals(27921, result2.length()); }
@Test public void convertImagePathsToBase64WithoutAlt() throws Exception { String result3 = repository.cacheFileFromArchive(module, "test string with <img src='bible.png'> and other symbols"); assertNotNull(result3); assertEquals(27909, result3.length()); }
@Test public void convertImagePathsToBase64WithoutAltLowerCase() throws Exception { String result3 = repository.cacheFileFromArchive(module, "<p><IMG SRC= \"theatre_seats.jpg\"> ".toLowerCase()); assertNotNull(result3); assertEquals(27879, result3.length()); }
@Test public void convertImagePathsToBase64withNonArchiveModules() throws Exception { when(module.isArchive()).thenReturn(false); String testLine = "test string with <img src=\"bible.png\" alt=\"bible\"> and other symbols"; String result = repository.cacheFileFromArchive(module, testLine); assertNotNull(result); assertEquals(testLine, result); }