Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(): uploadfiles fix #4484

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -125,13 +126,15 @@ public InputStream getFileContent(final Path path) throws IOException {
* {@inheritDoc}
**/
@Override
public NamespaceFile putFile(final Path path, final InputStream content, final Conflicts onAlreadyExist) throws IOException {
public NamespaceFile putFile(final Path path, final InputStream content, final Conflicts onAlreadyExist) throws IOException, URISyntaxException {
Path namespaceFilesPrefix = NamespaceFile.of(namespace, path).storagePath();
final boolean exists = storage.exists(tenant, namespaceFilesPrefix.toUri());
// Remove Windows letter
URI cleanUri = new URI(namespaceFilesPrefix.toUri().toString().replaceFirst("^file:///[a-zA-Z]:", ""));
final boolean exists = storage.exists(tenant, cleanUri);

return switch (onAlreadyExist) {
case OVERWRITE -> {
URI uri = storage.put(tenant, namespaceFilesPrefix.toUri(), content);
URI uri = storage.put(tenant, cleanUri, content);
NamespaceFile namespaceFile = new NamespaceFile(relativize(uri), uri, namespace);
if (exists) {
logger.debug(String.format(
Expand Down
9 changes: 5 additions & 4 deletions core/src/main/java/io/kestra/core/storages/Namespace.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.List;
import java.util.function.Predicate;
Expand Down Expand Up @@ -83,17 +84,17 @@ default List<NamespaceFile> findAllFilesMatching(List<String> includes, List<Str
*/
InputStream getFileContent(Path path) throws IOException;

default NamespaceFile putFile(Path path, InputStream content) throws IOException {
default NamespaceFile putFile(Path path, InputStream content) throws IOException, URISyntaxException {
return putFile(path, content, Conflicts.OVERWRITE);
}

NamespaceFile putFile(Path path, InputStream content, Conflicts onAlreadyExist) throws IOException;
NamespaceFile putFile(Path path, InputStream content, Conflicts onAlreadyExist) throws IOException, URISyntaxException;

default NamespaceFile putFile(NamespaceFile file, InputStream content) throws IOException {
default NamespaceFile putFile(NamespaceFile file, InputStream content) throws IOException, URISyntaxException {
return putFile(file, content, Conflicts.OVERWRITE);
}

default NamespaceFile putFile(NamespaceFile file, InputStream content, Conflicts onAlreadyExist) throws IOException {
default NamespaceFile putFile(NamespaceFile file, InputStream content, Conflicts onAlreadyExist) throws IOException, URISyntaxException {
return putFile(Path.of(file.path()), content, onAlreadyExist);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.net.URI;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand Down Expand Up @@ -123,13 +124,18 @@ public UploadFiles.Output run(RunContext runContext) throws Exception {
for (Object file : filesList) {
Optional<URI> uri = FileUtils.getURI(file.toString());
// Immediately handle strings that are full URI
if (uri.isPresent()) {
if (runContext.storage().isFileExist(uri.get())) {
try {

if (uri.isPresent() && runContext.storage().isFileExist(uri.get())) {
Path targetFilePath = Path.of(renderedDestination, FileUtils.getFileName(uri.get()));
storageNamespace.putFile(targetFilePath, runContext.storage().getFile(uri.get()), conflict);
} else {
regexs.add(file.toString());
}
// else ignore
} else {
}
// If the string is not a valid URI, try to use it as a regex
catch (InvalidPathException | NullPointerException e) {
runContext.logger().debug("File {} is not a valid URI, using it as a regex", file);
regexs.add(file.toString());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
Expand All @@ -35,7 +36,7 @@ public void setUp() throws IOException {
}

@Test
void shouldGetAllNamespaceFiles() throws IOException {
void shouldGetAllNamespaceFiles() throws IOException, URISyntaxException {
// Given
final String namespaceId = "io.kestra." + IdUtils.create();
final InternalNamespace namespace = new InternalNamespace(logger, null, namespaceId, storageInterface);
Expand All @@ -54,7 +55,7 @@ void shouldGetAllNamespaceFiles() throws IOException {
}

@Test
void shouldPutFileGivenNoTenant() throws IOException {
void shouldPutFileGivenNoTenant() throws IOException, URISyntaxException {
// Given
final String namespaceId = "io.kestra." + IdUtils.create();
final InternalNamespace namespace = new InternalNamespace(logger, null, namespaceId, storageInterface);
Expand All @@ -71,7 +72,7 @@ void shouldPutFileGivenNoTenant() throws IOException {
}

@Test
void shouldSucceedPutFileGivenExistingFileForConflictOverwrite() throws IOException {
void shouldSucceedPutFileGivenExistingFileForConflictOverwrite() throws IOException, URISyntaxException {
// Given
final String namespaceId = "io.kestra." + IdUtils.create();
final InternalNamespace namespace = new InternalNamespace(logger, null, namespaceId, storageInterface);
Expand All @@ -90,7 +91,7 @@ void shouldSucceedPutFileGivenExistingFileForConflictOverwrite() throws IOExcept
}

@Test
void shouldFailPutFileGivenExistingFileForError() throws IOException {
void shouldFailPutFileGivenExistingFileForError() throws IOException, URISyntaxException {
// Given
final String namespaceId = "io.kestra." + IdUtils.create();
final InternalNamespace namespace = new InternalNamespace(logger, null, namespaceId, storageInterface);
Expand All @@ -107,7 +108,7 @@ void shouldFailPutFileGivenExistingFileForError() throws IOException {
}

@Test
void shouldIgnorePutFileGivenExistingFileForSkip() throws IOException {
void shouldIgnorePutFileGivenExistingFileForSkip() throws IOException, URISyntaxException {
// Given
final String namespaceId = "io.kestra." + IdUtils.create();
final InternalNamespace namespace = new InternalNamespace(logger, null, namespaceId, storageInterface);
Expand All @@ -126,7 +127,7 @@ void shouldIgnorePutFileGivenExistingFileForSkip() throws IOException {
}

@Test
void shouldFindAllMatchingGivenNoTenant() throws IOException {
void shouldFindAllMatchingGivenNoTenant() throws IOException, URISyntaxException {
// Given
final String namespaceId = "io.kestra." + IdUtils.create();
final InternalNamespace namespace = new InternalNamespace(logger, null, namespaceId, storageInterface);
Expand All @@ -153,7 +154,7 @@ void shouldFindAllMatchingGivenNoTenant() throws IOException {
}

@Test
void shouldFindAllGivenTenant() throws IOException {
void shouldFindAllGivenTenant() throws IOException, URISyntaxException {
// Given
final String namespaceId = "io.kestra." + IdUtils.create();
final InternalNamespace namespaceTenant1 = new InternalNamespace(logger, "tenant1", namespaceId, storageInterface);
Expand Down