Skip to content

Commit

Permalink
Fix dir creation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rpoet-jh committed Nov 1, 2023
1 parent 06baa8c commit f9b4466
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import static org.eclipse.pass.deposit.transport.Transport.TRANSPORT_USERNAME;
import static org.eclipse.pass.deposit.transport.sftp.SftpTransport.SFTP_BASE_DIRECTORY;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -38,6 +40,7 @@
import org.apache.sshd.sftp.client.impl.DefaultSftpClientFactory;
import org.apache.sshd.sftp.common.SftpConstants;
import org.apache.sshd.sftp.common.SftpException;
import org.eclipse.pass.deposit.DepositServiceRuntimeException;
import org.eclipse.pass.deposit.assembler.PackageStream;
import org.eclipse.pass.deposit.transport.TransportResponse;
import org.eclipse.pass.deposit.transport.TransportSession;
Expand Down Expand Up @@ -90,43 +93,53 @@ private TransportResponse writeFile(ClientSession clientSession, PackageStream p
try (InputStream inputStream = packageStream.open()) {
String baseDir = getBaseDir();
createBaseDirIfNeeded(baseDir, sftpClient);
try (OutputStream outputStream = sftpClient.write(baseDir + "/" + fileName)) {
try (OutputStream outputStream = sftpClient.write(baseDir + File.separator + fileName)) {
IOUtils.copy(inputStream, outputStream);
return new SftpTransportResponse(true);
}
}
}
}

private String getBaseDir() {
String baseDir = transportProps.get(SFTP_BASE_DIRECTORY);
if (StringUtils.isBlank(baseDir)) {
throw new DepositServiceRuntimeException("Sftp requires \"default-directory\" in protocol-binding");
}
String cleanedDirName = baseDir.startsWith(File.separator) ? baseDir.substring(1) : baseDir;
return cleanedDirName.contains("%s")
? String.format(cleanedDirName, OffsetDateTime.now(ZoneId.of("UTC")).format(ISO_LOCAL_DATE))
: cleanedDirName;
}

private void createBaseDirIfNeeded(String path, SftpClient sftpClient) throws IOException {
if (StringUtils.isNotEmpty(path)) {
String[] dirs = path.split("/");
String splitRegex = Pattern.quote(System.getProperty("file.separator"));
String[] dirs = path.split(splitRegex);
StringBuilder fullPath = new StringBuilder();
for (String dir : dirs) {
if (!fullPath.isEmpty()) {
fullPath.append("/");
fullPath.append(File.separator);
}
fullPath.append(dir);
try {
sftpClient.stat(fullPath.toString());
} catch (SftpException e) {
if (e.getStatus() != SftpConstants.SSH_FX_NO_SUCH_FILE) {
throw new RuntimeException(e);
throw new RuntimeException("Error creating dir named " + fullPath, e);
}
sftpClient.mkdir(fullPath.toString());
createDir(fullPath.toString(), sftpClient);
}
}
}
}

private String getBaseDir() {
String baseDir = transportProps.get(SFTP_BASE_DIRECTORY);
if (StringUtils.isNotEmpty(baseDir)) {
return baseDir.contains("%s")
? String.format(baseDir, OffsetDateTime.now(ZoneId.of("UTC")).format(ISO_LOCAL_DATE))
: baseDir;
private void createDir(String dirName, SftpClient sftpClient) {
try {
sftpClient.mkdir(dirName);
} catch (IOException e) {
throw new RuntimeException("Error creating dir named " + dirName, e);
}
return "";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void testCreateFile() {
TRANSPORT_SERVER_PORT, String.valueOf(sshd.getPort()),
TRANSPORT_USERNAME, "dummyUser",
TRANSPORT_PASSWORD, "dummyPass",
SFTP_BASE_DIRECTORY, "upload/test/%s"
SFTP_BASE_DIRECTORY, "/upload/test/%s"
);
String testFileName = System.currentTimeMillis() + "package.tar.gz";
NullInputStream content = new NullInputStream(ONE_MIB);
Expand Down

0 comments on commit f9b4466

Please sign in to comment.