@Test public void shouldReturnUpdatedContentWhenEmbeddedImage() throws Exception { HTMLUploadImageProcessor imageProcessor = new HTMLUploadImageProcessor(portalContainer, uploadService, repositoryService, linkManager); String content = "<p>content with image: <img src=\"/portal/image?uploadId=123456\" /></p>"; Node node = mock(Node.class); File imageFile = uploadFolder.newFile("image.png"); UploadResource uploadImage = new UploadResource("123456", "image.png"); uploadImage.setStoreLocation(imageFile.getPath()); when(uploadService.getUploadResource(eq("123456"))).thenReturn(uploadImage); when(node.hasNode(eq("image.png"))).thenReturn(false); when(node.addNode(anyString(), anyString())).thenReturn(node); when(portalContainer.getName()).thenReturn("portal"); when(portalContainer.getRestContextName()).thenReturn("rest"); when(repositoryService.getCurrentRepository()).thenReturn(repository); when(repository.getConfiguration()).thenReturn(repositoryEntry); when(repositoryEntry.getName()).thenReturn("repository"); when(node.getSession()).thenReturn(session); Workspace workspace = mock(Workspace.class); when(session.getWorkspace()).thenReturn(workspace); when(workspace.getName()).thenReturn("collaboration"); when(node.getPath()).thenReturn("/path/to/image.png"); String processedContent = imageProcessor.processImages(content, node, null); assertTrue(processedContent.matches("<p>content with image: <img src=\"/portal/rest/images/repository/collaboration/[a-z0-9]+\" /></p>")); }
public String processImages(String content, Node parentNode, String imagesSubFolderPath) throws Exception { if(StringUtils.isBlank(content)) { return content; } Set<String> processedUploads = new HashSet<>(); Map<String, String> urlToReplaces = new HashMap<>(); Matcher matcher = UPLOAD_ID_PATTERN.matcher(content); if (!matcher.find()) { return content; } if(parentNode == null) { throw new IllegalArgumentException("Container node for uploaded processed images in HTML content must not be null"); } Node imagesFolderNode = parentNode; if(StringUtils.isNotEmpty(imagesSubFolderPath)) { for (String folder : imagesSubFolderPath.split("/")) { if (StringUtils.isBlank(folder)) { continue; } if (imagesFolderNode.hasNode(folder)) { imagesFolderNode = imagesFolderNode.getNode(folder); if (imagesFolderNode.isNodeType(NodetypeConstant.EXO_SYMLINK)) { imagesFolderNode = linkManager.getTarget(imagesFolderNode); } } else if (imagesFolderNode.isNodeType(NodetypeConstant.EXO_SYMLINK)) { imagesFolderNode = linkManager.getTarget(imagesFolderNode).getNode(folder); } else { imagesFolderNode = imagesFolderNode.addNode(folder, "nt:unstructured"); } } } String processedContent = content; do { String uploadId = matcher.group(matcher.groupCount() - 1); if (!processedUploads.contains(uploadId)) { UploadResource uploadedResource = uploadService.getUploadResource(uploadId); if (uploadedResource == null) { continue; } String fileName = uploadedResource.getFileName(); int i = 1; String originalFileName = fileName; while (imagesFolderNode.hasNode(fileName)) { if (originalFileName.contains(".")) { int indexOfPoint = originalFileName.indexOf("."); fileName = originalFileName.substring(0, indexOfPoint) + "(" + i + ")" + originalFileName.substring(indexOfPoint); } else { fileName = originalFileName + "(" + i + ")"; } i++; } fileName = Text.escapeIllegalJcrChars(fileName); fileName = Utils.cleanName(fileName); Node imageNode = imagesFolderNode.addNode(fileName, "nt:file"); Node resourceNode = imageNode.addNode("jcr:content", "nt:resource"); resourceNode.setProperty("jcr:mimeType", uploadedResource.getMimeType()); resourceNode.setProperty("jcr:lastModified", Calendar.getInstance()); String fileDiskLocation = uploadedResource.getStoreLocation(); try(InputStream inputStream = new FileInputStream(fileDiskLocation)) { resourceNode.setProperty("jcr:data", inputStream); resourceNode.getSession().save(); parentNode.getSession().save(); } uploadService.removeUploadResource(uploadId); int uploadIdIndex = matcher.start(); String urlToReplace = getURLToReplace(processedContent, uploadId, uploadIdIndex); if (!UPLOAD_URL_PATTERN.matcher(urlToReplace).matches()) { LOG.warn("Unrecognized URL to replace in activity body {}", urlToReplace); continue; } String fileURI = getJcrURI(imageNode); if (StringUtils.isNotBlank(urlToReplace)) { urlToReplaces.put(urlToReplace, fileURI); processedUploads.add(uploadId); } } } while (matcher.find()); processedContent = replaceUrl(content, urlToReplaces); return processedContent; }
HTMLUploadImageProcessor { public String processImages(String content, Node parentNode, String imagesSubFolderPath) throws Exception { if(StringUtils.isBlank(content)) { return content; } Set<String> processedUploads = new HashSet<>(); Map<String, String> urlToReplaces = new HashMap<>(); Matcher matcher = UPLOAD_ID_PATTERN.matcher(content); if (!matcher.find()) { return content; } if(parentNode == null) { throw new IllegalArgumentException("Container node for uploaded processed images in HTML content must not be null"); } Node imagesFolderNode = parentNode; if(StringUtils.isNotEmpty(imagesSubFolderPath)) { for (String folder : imagesSubFolderPath.split("/")) { if (StringUtils.isBlank(folder)) { continue; } if (imagesFolderNode.hasNode(folder)) { imagesFolderNode = imagesFolderNode.getNode(folder); if (imagesFolderNode.isNodeType(NodetypeConstant.EXO_SYMLINK)) { imagesFolderNode = linkManager.getTarget(imagesFolderNode); } } else if (imagesFolderNode.isNodeType(NodetypeConstant.EXO_SYMLINK)) { imagesFolderNode = linkManager.getTarget(imagesFolderNode).getNode(folder); } else { imagesFolderNode = imagesFolderNode.addNode(folder, "nt:unstructured"); } } } String processedContent = content; do { String uploadId = matcher.group(matcher.groupCount() - 1); if (!processedUploads.contains(uploadId)) { UploadResource uploadedResource = uploadService.getUploadResource(uploadId); if (uploadedResource == null) { continue; } String fileName = uploadedResource.getFileName(); int i = 1; String originalFileName = fileName; while (imagesFolderNode.hasNode(fileName)) { if (originalFileName.contains(".")) { int indexOfPoint = originalFileName.indexOf("."); fileName = originalFileName.substring(0, indexOfPoint) + "(" + i + ")" + originalFileName.substring(indexOfPoint); } else { fileName = originalFileName + "(" + i + ")"; } i++; } fileName = Text.escapeIllegalJcrChars(fileName); fileName = Utils.cleanName(fileName); Node imageNode = imagesFolderNode.addNode(fileName, "nt:file"); Node resourceNode = imageNode.addNode("jcr:content", "nt:resource"); resourceNode.setProperty("jcr:mimeType", uploadedResource.getMimeType()); resourceNode.setProperty("jcr:lastModified", Calendar.getInstance()); String fileDiskLocation = uploadedResource.getStoreLocation(); try(InputStream inputStream = new FileInputStream(fileDiskLocation)) { resourceNode.setProperty("jcr:data", inputStream); resourceNode.getSession().save(); parentNode.getSession().save(); } uploadService.removeUploadResource(uploadId); int uploadIdIndex = matcher.start(); String urlToReplace = getURLToReplace(processedContent, uploadId, uploadIdIndex); if (!UPLOAD_URL_PATTERN.matcher(urlToReplace).matches()) { LOG.warn("Unrecognized URL to replace in activity body {}", urlToReplace); continue; } String fileURI = getJcrURI(imageNode); if (StringUtils.isNotBlank(urlToReplace)) { urlToReplaces.put(urlToReplace, fileURI); processedUploads.add(uploadId); } } } while (matcher.find()); processedContent = replaceUrl(content, urlToReplaces); return processedContent; } }
HTMLUploadImageProcessor { public String processImages(String content, Node parentNode, String imagesSubFolderPath) throws Exception { if(StringUtils.isBlank(content)) { return content; } Set<String> processedUploads = new HashSet<>(); Map<String, String> urlToReplaces = new HashMap<>(); Matcher matcher = UPLOAD_ID_PATTERN.matcher(content); if (!matcher.find()) { return content; } if(parentNode == null) { throw new IllegalArgumentException("Container node for uploaded processed images in HTML content must not be null"); } Node imagesFolderNode = parentNode; if(StringUtils.isNotEmpty(imagesSubFolderPath)) { for (String folder : imagesSubFolderPath.split("/")) { if (StringUtils.isBlank(folder)) { continue; } if (imagesFolderNode.hasNode(folder)) { imagesFolderNode = imagesFolderNode.getNode(folder); if (imagesFolderNode.isNodeType(NodetypeConstant.EXO_SYMLINK)) { imagesFolderNode = linkManager.getTarget(imagesFolderNode); } } else if (imagesFolderNode.isNodeType(NodetypeConstant.EXO_SYMLINK)) { imagesFolderNode = linkManager.getTarget(imagesFolderNode).getNode(folder); } else { imagesFolderNode = imagesFolderNode.addNode(folder, "nt:unstructured"); } } } String processedContent = content; do { String uploadId = matcher.group(matcher.groupCount() - 1); if (!processedUploads.contains(uploadId)) { UploadResource uploadedResource = uploadService.getUploadResource(uploadId); if (uploadedResource == null) { continue; } String fileName = uploadedResource.getFileName(); int i = 1; String originalFileName = fileName; while (imagesFolderNode.hasNode(fileName)) { if (originalFileName.contains(".")) { int indexOfPoint = originalFileName.indexOf("."); fileName = originalFileName.substring(0, indexOfPoint) + "(" + i + ")" + originalFileName.substring(indexOfPoint); } else { fileName = originalFileName + "(" + i + ")"; } i++; } fileName = Text.escapeIllegalJcrChars(fileName); fileName = Utils.cleanName(fileName); Node imageNode = imagesFolderNode.addNode(fileName, "nt:file"); Node resourceNode = imageNode.addNode("jcr:content", "nt:resource"); resourceNode.setProperty("jcr:mimeType", uploadedResource.getMimeType()); resourceNode.setProperty("jcr:lastModified", Calendar.getInstance()); String fileDiskLocation = uploadedResource.getStoreLocation(); try(InputStream inputStream = new FileInputStream(fileDiskLocation)) { resourceNode.setProperty("jcr:data", inputStream); resourceNode.getSession().save(); parentNode.getSession().save(); } uploadService.removeUploadResource(uploadId); int uploadIdIndex = matcher.start(); String urlToReplace = getURLToReplace(processedContent, uploadId, uploadIdIndex); if (!UPLOAD_URL_PATTERN.matcher(urlToReplace).matches()) { LOG.warn("Unrecognized URL to replace in activity body {}", urlToReplace); continue; } String fileURI = getJcrURI(imageNode); if (StringUtils.isNotBlank(urlToReplace)) { urlToReplaces.put(urlToReplace, fileURI); processedUploads.add(uploadId); } } } while (matcher.find()); processedContent = replaceUrl(content, urlToReplaces); return processedContent; } HTMLUploadImageProcessor(PortalContainer portalContainer, UploadService uploadService,
                                  RepositoryService repositoryService, LinkManager linkManager); }
HTMLUploadImageProcessor { public String processImages(String content, Node parentNode, String imagesSubFolderPath) throws Exception { if(StringUtils.isBlank(content)) { return content; } Set<String> processedUploads = new HashSet<>(); Map<String, String> urlToReplaces = new HashMap<>(); Matcher matcher = UPLOAD_ID_PATTERN.matcher(content); if (!matcher.find()) { return content; } if(parentNode == null) { throw new IllegalArgumentException("Container node for uploaded processed images in HTML content must not be null"); } Node imagesFolderNode = parentNode; if(StringUtils.isNotEmpty(imagesSubFolderPath)) { for (String folder : imagesSubFolderPath.split("/")) { if (StringUtils.isBlank(folder)) { continue; } if (imagesFolderNode.hasNode(folder)) { imagesFolderNode = imagesFolderNode.getNode(folder); if (imagesFolderNode.isNodeType(NodetypeConstant.EXO_SYMLINK)) { imagesFolderNode = linkManager.getTarget(imagesFolderNode); } } else if (imagesFolderNode.isNodeType(NodetypeConstant.EXO_SYMLINK)) { imagesFolderNode = linkManager.getTarget(imagesFolderNode).getNode(folder); } else { imagesFolderNode = imagesFolderNode.addNode(folder, "nt:unstructured"); } } } String processedContent = content; do { String uploadId = matcher.group(matcher.groupCount() - 1); if (!processedUploads.contains(uploadId)) { UploadResource uploadedResource = uploadService.getUploadResource(uploadId); if (uploadedResource == null) { continue; } String fileName = uploadedResource.getFileName(); int i = 1; String originalFileName = fileName; while (imagesFolderNode.hasNode(fileName)) { if (originalFileName.contains(".")) { int indexOfPoint = originalFileName.indexOf("."); fileName = originalFileName.substring(0, indexOfPoint) + "(" + i + ")" + originalFileName.substring(indexOfPoint); } else { fileName = originalFileName + "(" + i + ")"; } i++; } fileName = Text.escapeIllegalJcrChars(fileName); fileName = Utils.cleanName(fileName); Node imageNode = imagesFolderNode.addNode(fileName, "nt:file"); Node resourceNode = imageNode.addNode("jcr:content", "nt:resource"); resourceNode.setProperty("jcr:mimeType", uploadedResource.getMimeType()); resourceNode.setProperty("jcr:lastModified", Calendar.getInstance()); String fileDiskLocation = uploadedResource.getStoreLocation(); try(InputStream inputStream = new FileInputStream(fileDiskLocation)) { resourceNode.setProperty("jcr:data", inputStream); resourceNode.getSession().save(); parentNode.getSession().save(); } uploadService.removeUploadResource(uploadId); int uploadIdIndex = matcher.start(); String urlToReplace = getURLToReplace(processedContent, uploadId, uploadIdIndex); if (!UPLOAD_URL_PATTERN.matcher(urlToReplace).matches()) { LOG.warn("Unrecognized URL to replace in activity body {}", urlToReplace); continue; } String fileURI = getJcrURI(imageNode); if (StringUtils.isNotBlank(urlToReplace)) { urlToReplaces.put(urlToReplace, fileURI); processedUploads.add(uploadId); } } } while (matcher.find()); processedContent = replaceUrl(content, urlToReplaces); return processedContent; } HTMLUploadImageProcessor(PortalContainer portalContainer, UploadService uploadService,
                                  RepositoryService repositoryService, LinkManager linkManager); String processImages(String content, Node parentNode, String imagesSubFolderPath); String getRepositoryName(); }
HTMLUploadImageProcessor { public String processImages(String content, Node parentNode, String imagesSubFolderPath) throws Exception { if(StringUtils.isBlank(content)) { return content; } Set<String> processedUploads = new HashSet<>(); Map<String, String> urlToReplaces = new HashMap<>(); Matcher matcher = UPLOAD_ID_PATTERN.matcher(content); if (!matcher.find()) { return content; } if(parentNode == null) { throw new IllegalArgumentException("Container node for uploaded processed images in HTML content must not be null"); } Node imagesFolderNode = parentNode; if(StringUtils.isNotEmpty(imagesSubFolderPath)) { for (String folder : imagesSubFolderPath.split("/")) { if (StringUtils.isBlank(folder)) { continue; } if (imagesFolderNode.hasNode(folder)) { imagesFolderNode = imagesFolderNode.getNode(folder); if (imagesFolderNode.isNodeType(NodetypeConstant.EXO_SYMLINK)) { imagesFolderNode = linkManager.getTarget(imagesFolderNode); } } else if (imagesFolderNode.isNodeType(NodetypeConstant.EXO_SYMLINK)) { imagesFolderNode = linkManager.getTarget(imagesFolderNode).getNode(folder); } else { imagesFolderNode = imagesFolderNode.addNode(folder, "nt:unstructured"); } } } String processedContent = content; do { String uploadId = matcher.group(matcher.groupCount() - 1); if (!processedUploads.contains(uploadId)) { UploadResource uploadedResource = uploadService.getUploadResource(uploadId); if (uploadedResource == null) { continue; } String fileName = uploadedResource.getFileName(); int i = 1; String originalFileName = fileName; while (imagesFolderNode.hasNode(fileName)) { if (originalFileName.contains(".")) { int indexOfPoint = originalFileName.indexOf("."); fileName = originalFileName.substring(0, indexOfPoint) + "(" + i + ")" + originalFileName.substring(indexOfPoint); } else { fileName = originalFileName + "(" + i + ")"; } i++; } fileName = Text.escapeIllegalJcrChars(fileName); fileName = Utils.cleanName(fileName); Node imageNode = imagesFolderNode.addNode(fileName, "nt:file"); Node resourceNode = imageNode.addNode("jcr:content", "nt:resource"); resourceNode.setProperty("jcr:mimeType", uploadedResource.getMimeType()); resourceNode.setProperty("jcr:lastModified", Calendar.getInstance()); String fileDiskLocation = uploadedResource.getStoreLocation(); try(InputStream inputStream = new FileInputStream(fileDiskLocation)) { resourceNode.setProperty("jcr:data", inputStream); resourceNode.getSession().save(); parentNode.getSession().save(); } uploadService.removeUploadResource(uploadId); int uploadIdIndex = matcher.start(); String urlToReplace = getURLToReplace(processedContent, uploadId, uploadIdIndex); if (!UPLOAD_URL_PATTERN.matcher(urlToReplace).matches()) { LOG.warn("Unrecognized URL to replace in activity body {}", urlToReplace); continue; } String fileURI = getJcrURI(imageNode); if (StringUtils.isNotBlank(urlToReplace)) { urlToReplaces.put(urlToReplace, fileURI); processedUploads.add(uploadId); } } } while (matcher.find()); processedContent = replaceUrl(content, urlToReplaces); return processedContent; } HTMLUploadImageProcessor(PortalContainer portalContainer, UploadService uploadService,
                                  RepositoryService repositoryService, LinkManager linkManager); String processImages(String content, Node parentNode, String imagesSubFolderPath); String getRepositoryName(); static final String IP_REGEX; static final String URL_OR_URI_REGEX; }