mirror of https://github.com/halo-dev/halo
[release-2.19] refactor: optimize request headers when generating thumbnails from URI (#6633)
This is an automated cherry-pick of #6628 /assign JohnNiang ```release-note None ```release-2.19 v2.19.3
parent
a5ff816af5
commit
9ffb1bbc10
|
@ -6,6 +6,7 @@ import java.io.File;
|
|||
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;
|
||||
|
@ -172,7 +173,11 @@ public class ThumbnailGenerator {
|
|||
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];
|
||||
|
|
|
@ -58,7 +58,7 @@ public class AttachmentReconciler implements Reconciler<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);
|
||||
|
|
|
@ -27,6 +27,7 @@ import run.halo.app.core.extension.attachment.Attachment;
|
|||
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;
|
||||
|
@ -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);
|
||||
|
|
|
@ -4,11 +4,13 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
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;
|
||||
|
@ -57,7 +59,9 @@ class ThumbnailGeneratorTest {
|
|||
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();
|
||||
|
@ -81,8 +85,9 @@ class ThumbnailGeneratorTest {
|
|||
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");
|
||||
|
|
Loading…
Reference in New Issue