Skip to content

Commit

Permalink
Ajuste para exibir imagens com parametros na url
Browse files Browse the repository at this point in the history
  • Loading branch information
Moacir da Roza Flores committed Dec 21, 2024
1 parent 2517c42 commit d67a0bf
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 26 deletions.
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,20 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.14.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>

<!-- Netbeans modules -->
<dependency>
<groupId>org.netbeans.api</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,19 @@ public static URL downloadImage(URL url) {

byte[] bytes = http.send(request, ofByteArray())
.body();

if (bytes == null || bytes.length < 1000) {
return TempDir.getCantLoadImage().toUri().toURL();
}

Path file = Path.of(TEMP_DIR.toString(), Path.of(url.getFile()).getFileName().toString());
if (file != null) {
int contRepeatImage = 0;
while (Files.exists(file) && !fileExistsByHash(file, bytes)) {
file = Path.of(TEMP_DIR.toString(), (contRepeatImage++) + "_" + Path.of(url.getFile()).getFileName().toString());
}
String fileSrc = removeParameters(file.toAbsolutePath().toFile().getAbsolutePath());
file = Path.of(fileSrc);
Files.write(file, bytes, CREATE, TRUNCATE_EXISTING);
returnUrl = file.toUri().toURL();
}
Expand Down Expand Up @@ -253,4 +255,18 @@ public static boolean fileExistsByHash(Path path, byte[] bytes) {
}
return false;
}

public static String removeParameters(String src) {
if (src == null || src.isBlank()) {
return src;
}
int index = src.indexOf("?");
if (index == -1) {
index = src.indexOf("%3F");
}
if (index != -1) {
return src.replace(src.substring(index, src.length()), "");
}
return src;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private URL buildURL(String url) {
resultUrl = ImageHelper.convertSVGToPNG(resultUrl);
}
} else {
File localFile = ImageHelper.getLocalImage(url);
File localFile = ImageHelper.getLocalImage(ImageHelper.removeParameters(url));
if (localFile != null) {
resultUrl = Utilities.toURI(localFile).toURL();
if (ImageHelper.isSVG(resultUrl)) {
Expand Down
123 changes: 100 additions & 23 deletions src/test/java/io/github/moacirrf/netbeans/markdown/html/ImageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,134 @@
*/
package io.github.moacirrf.netbeans.markdown.html;

import static io.github.moacirrf.netbeans.markdown.TempDir.getTempDir;
import io.github.moacirrf.netbeans.markdown.TempDir;
import static java.io.File.separator;
import java.io.IOException;
import static java.lang.System.getProperty;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.mockito.MockedStatic;
import static org.mockito.Mockito.mockStatic;
import org.openide.util.Exceptions;

/**
*
* @author Moacir da Roza Flores <moacirrf@gmail.com>
*/
public class ImageTest {

public static final String TEMP_DIR_PLUGIN = getProperty("java.io.tmpdir") + "/nb_markdown_teste";

private HtmlBuilder htmlBuilder = HtmlBuilder.getInstance();

public static Path getTempDirTeste() {
Path path = Paths.get(TEMP_DIR_PLUGIN);
if (!Files.exists(path)) {
try {
Files.createDirectory(path);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
return path;
}

@Test
public void testResizableImage() {
var srcExpected = "file:" + getTempDir() + separator + "tux.png";
var html = "<html>\n"
+ " <body>\n"
+ " <img src=\"%s\" width=\"300\" height=\"300\" class=\"removeMarginPaddingTop\" />\n"
+ " </body>\n"
+ "</html>";
try (MockedStatic<TempDir> mockedStatic = mockStatic(TempDir.class)) {
mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(getTempDirTeste());

var expected = String.format(html, srcExpected);
var srcExpected = "file:" + TempDir.getTempDir() + separator + "tux.png";
var html = "<html>\n"
+ " <body>\n"
+ " <img src=\"%s\" width=\"300\" height=\"300\" class=\"removeMarginPaddingTop\" />\n"
+ " </body>\n"
+ "</html>";

var given = "<img src=\"https://mdg.imgix.net/assets/images/tux.png\" width=\"300\" height=\"300\" />";
var expected = String.format(html, srcExpected);

var result = htmlBuilder.build(given);
var given = "<img src=\"https://mdg.imgix.net/assets/images/tux.png\" width=\"300\" height=\"300\" />";

assertNotNull(result);
assertEquals(expected, result);
var result = htmlBuilder.build(given);

assertNotNull(result);
assertEquals(expected, result);
}
}

@Test
public void testImage() {
var srcExpected = "file:" + getTempDir() + separator + "tux.png";
try (MockedStatic<TempDir> mockedStatic = mockStatic(TempDir.class)) {
mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(getTempDirTeste());

var srcExpected = "file:" + TempDir.getTempDir() + separator + "tux.png";

String html = "<html>\n"
+ " <body>\n"
+ " <p id=\"0-134\" class=\"removeMarginPaddingTop\"><span id=\"0-24\"><a href=\"https://mdg.imgix.net/assets/images/tux.png\" id=\"0-133\" class=\"removeColorLinkWithImage\"><img src=\"%s\" alt=\"Tux, the Linux mascot\" title=\"Title of image\" id=\"1-87\" /></a></span></p>\n"
+ " </body>\n"
+ "</html>";

var expected = String.format(html, srcExpected);

var given = "[![Tux, the Linux mascot](https://mdg.imgix.net/assets/images/tux.png \"Title of image\")](https://mdg.imgix.net/assets/images/tux.png)\n";

var result = htmlBuilder.build(given);

assertNotNull(result);
assertEquals(expected, result);
}
}

@Test
public void testResizableImageWithParameters() {
try (MockedStatic<TempDir> mockedStatic = mockStatic(TempDir.class)) {
mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(getTempDirTeste());

var srcExpected = "file:" + TempDir.getTempDir() + separator + "tux.png";
var html = "<html>\n"
+ " <body>\n"
+ " <img src=\"%s\" width=\"300\" height=\"300\" class=\"removeMarginPaddingTop\" />\n"
+ " </body>\n"
+ "</html>";

var expected = String.format(html, srcExpected);

var given = "<img src=\"https://mdg.imgix.net/assets/images/tux.png?raw=true&teste=23\" width=\"300\" height=\"300\" />";

var result = htmlBuilder.build(given);

assertNotNull(result);
assertEquals(expected, result);
}
}

@Test
public void testImageWithParameters() {
try (MockedStatic<TempDir> mockedStatic = mockStatic(TempDir.class)) {
mockedStatic.when(() -> TempDir.getTempDir()).thenReturn(getTempDirTeste());

var srcExpected = "file:" + TempDir.getTempDir() + separator + "tux.png";

String html = "<html>\n"
+ " <body>\n"
+ " <p id=\"0-134\" class=\"removeMarginPaddingTop\"><span id=\"0-24\"><a href=\"https://mdg.imgix.net/assets/images/tux.png\" id=\"0-133\" class=\"removeColorLinkWithImage\"><img src=\"%s\" alt=\"Tux, the Linux mascot\" title=\"Title of image\" id=\"1-87\" /></a></span></p>\n"
+ " </body>\n"
+ "</html>";
String html = "<html>\n"
+ " <body>\n"
+ " <p id=\"0-143\" class=\"removeMarginPaddingTop\"><span id=\"0-24\"><a href=\"https://mdg.imgix.net/assets/images/tux.png\" id=\"0-142\" class=\"removeColorLinkWithImage\"><img src=\"%s\" alt=\"Tux, the Linux mascot\" title=\"Title of image\" id=\"1-96\" /></a></span></p>\n"
+ " </body>\n"
+ "</html>";

var expected = String.format(html, srcExpected);
var expected = String.format(html, srcExpected);

var given = "[![Tux, the Linux mascot](https://mdg.imgix.net/assets/images/tux.png \"Title of image\")](https://mdg.imgix.net/assets/images/tux.png)\n";
var given = "[![Tux, the Linux mascot](https://mdg.imgix.net/assets/images/tux.png?raw=true \"Title of image\")](https://mdg.imgix.net/assets/images/tux.png)\n";

var result = htmlBuilder.build(given);
var result = htmlBuilder.build(given);

assertNotNull(result);
assertEquals(expected, result);
assertNotNull(result);
assertEquals(expected, result);
}
}

}

0 comments on commit d67a0bf

Please sign in to comment.