Skip to content

Commit

Permalink
[release-2.19] refactor: optimize request headers when generating thu…
Browse files Browse the repository at this point in the history
…mbnails from URI (#6633)

This is an automated cherry-pick of #6628

/assign JohnNiang

```release-note
None
```
  • Loading branch information
halo-dev-bot authored Sep 10, 2024
1 parent a5ff816 commit 9ffb1bb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
Expand Down Expand Up @@ -172,7 +173,11 @@ Path downloadFileInternal(URL url) throws IOException {
File tempFile = File.createTempFile("halo-image-thumb-", ".tmp");
long totalBytesDownloaded = 0;
var tempFilePath = tempFile.toPath();
try (InputStream inputStream = url.openStream();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)"
+ " Chrome/92.0.4515.131 Safari/537.36");
try (InputStream inputStream = connection.getInputStream();
FileOutputStream outputStream = new FileOutputStream(tempFile)) {

byte[] buffer = new byte[4096];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Result reconcile(Request request) {
var annotations = attachment.getMetadata().getAnnotations();
if (annotations != null) {
attachmentService.getPermalink(attachment)
.map(URI::toString)
.map(URI::toASCIIString)
.switchIfEmpty(Mono.fromSupplier(() -> {
// Only for back-compatibility
return annotations.get(Constant.EXTERNAL_LINK_ANNO_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import run.halo.app.core.extension.attachment.Constant;
import run.halo.app.core.extension.attachment.LocalThumbnail;
import run.halo.app.extension.ExtensionClient;
import run.halo.app.extension.ExtensionUtil;
import run.halo.app.extension.ListOptions;
import run.halo.app.extension.PageRequestImpl;
import run.halo.app.extension.controller.Controller;
Expand All @@ -47,6 +48,9 @@ public class LocalThumbnailsReconciler implements Reconciler<Reconciler.Request>
public Result reconcile(Request request) {
client.fetch(LocalThumbnail.class, request.name())
.ifPresent(thumbnail -> {
if (ExtensionUtil.isDeleted(thumbnail)) {
return;
}
if (shouldGenerate(thumbnail)) {
requestGenerateThumbnail(thumbnail);
nullSafeAnnotations(thumbnail).remove(REQUEST_TO_GENERATE_ANNO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -57,7 +59,9 @@ void testDownloadImage_Success() throws Exception {
String mockImageData = "fakeImageData";
InputStream mockInputStream = new ByteArrayInputStream(mockImageData.getBytes());

doAnswer(invocation -> mockInputStream).when(spyImageUrl).openStream();
var urlConnection = mock(HttpURLConnection.class);
doAnswer(invocation -> urlConnection).when(spyImageUrl).openConnection();
doReturn(mockInputStream).when(urlConnection).getInputStream();

var path = imageDownloader.downloadFileInternal(spyImageUrl);
assertThat(path).isNotNull();
Expand All @@ -81,8 +85,9 @@ void downloadImage_FileSizeLimitExceeded() throws Exception {
var fileSizeByte = ThumbnailGenerator.MAX_FILE_SIZE + 10;
byte[] largeImageData = new byte[fileSizeByte];
InputStream mockInputStream = new ByteArrayInputStream(largeImageData);

doReturn(mockInputStream).when(spyImageUrl).openStream();
var urlConnection = mock(HttpURLConnection.class);
doAnswer(invocation -> urlConnection).when(spyImageUrl).openConnection();
doReturn(mockInputStream).when(urlConnection).getInputStream();
assertThatThrownBy(() -> imageDownloader.downloadFileInternal(spyImageUrl))
.isInstanceOf(IOException.class)
.hasMessageContaining("File size exceeds the limit");
Expand Down

0 comments on commit 9ffb1bb

Please sign in to comment.