Skip to content

Commit

Permalink
fix(core): encode filename (#5593)
Browse files Browse the repository at this point in the history
close #5589
  • Loading branch information
Skraye committed Oct 22, 2024
1 parent 2a578fe commit a49b406
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
7 changes: 6 additions & 1 deletion core/src/main/java/io/kestra/plugin/core/http/Download.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));

Expand All @@ -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 <ENCODING>'(lang)'<filename>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a49b406

Please sign in to comment.