diff --git a/core/src/main/java/io/kestra/plugin/core/http/Download.java b/core/src/main/java/io/kestra/plugin/core/http/Download.java index 308efa9996..6c1495101d 100644 --- a/core/src/main/java/io/kestra/plugin/core/http/Download.java +++ b/core/src/main/java/io/kestra/plugin/core/http/Download.java @@ -21,6 +21,8 @@ import java.io.File; import java.io.FileOutputStream; import java.net.URI; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; @@ -128,6 +130,9 @@ public Output run(RunContext runContext) throws Exception { String contentDisposition = builder.headers.get("Content-Disposition").getFirst(); filename = filenameFromHeader(runContext, contentDisposition); } + if (filename != null) { + filename = URLEncoder.encode(filename, StandardCharsets.UTF_8); + } builder.uri(runContext.storage().putFile(tempFile, filename)); @@ -145,7 +150,7 @@ private String filenameFromHeader(RunContext runContext, String contentDispositi String filename = null; for (String part : parts) { if (part.startsWith("filename")) { - filename = part.substring(part.lastIndexOf('=') + 2, part.length() - 1); + filename = part.substring(part.lastIndexOf('=') + 1); } if (part.startsWith("filename*")) { // following https://datatracker.ietf.org/doc/html/rfc5987 the filename* should be '(lang)' diff --git a/core/src/test/java/io/kestra/plugin/core/http/DownloadTest.java b/core/src/test/java/io/kestra/plugin/core/http/DownloadTest.java index 5442df59b1..d69d91f0ac 100644 --- a/core/src/test/java/io/kestra/plugin/core/http/DownloadTest.java +++ b/core/src/test/java/io/kestra/plugin/core/http/DownloadTest.java @@ -1,6 +1,7 @@ package io.kestra.plugin.core.http; import com.google.common.collect.ImmutableMap; +import io.kestra.core.junit.annotations.KestraTest; import io.kestra.core.runners.RunContext; import io.kestra.core.runners.RunContextFactory; import io.kestra.core.storages.StorageInterface; @@ -12,19 +13,16 @@ import io.micronaut.http.annotation.Get; import io.micronaut.http.client.exceptions.HttpClientResponseException; import io.micronaut.runtime.server.EmbeddedServer; -import io.kestra.core.junit.annotations.KestraTest; import jakarta.inject.Inject; import org.apache.commons.io.IOUtils; import org.junit.jupiter.api.Test; import java.io.IOException; import java.net.URI; -import java.net.URL; import java.nio.charset.StandardCharsets; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.endsWith; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.*; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -135,7 +133,7 @@ void contentDisposition() throws Exception { Download.Output output = task.run(runContext); - assertThat(output.getUri().toString(), endsWith("filename.jpg")); + assertThat(output.getUri().toString(), containsString("filename.jpg")); } @Controller() diff --git a/storage-local/src/main/java/io/kestra/storage/local/LocalStorage.java b/storage-local/src/main/java/io/kestra/storage/local/LocalStorage.java index 9669338c47..90946dd599 100644 --- a/storage-local/src/main/java/io/kestra/storage/local/LocalStorage.java +++ b/storage-local/src/main/java/io/kestra/storage/local/LocalStorage.java @@ -161,7 +161,7 @@ public URI put(String tenantId, URI uri, StorageObject storageObject) throws IOE } } - return URI.create("kestra://" + uri.getPath()); + return URI.create("kestra://" + uri.getRawPath()); } @Override