mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-06 15:20:54 +03:00
LocalFile abstraction in SFTPFileTransfer
Upload method using LocalFile in FileTransfer. Use only LocalFile in SFTPFileTransfer Upload.
This commit is contained in:
committed by
Shikhar Bhushan
parent
137dc5ed42
commit
9761f44cd4
@@ -15,20 +15,22 @@
|
|||||||
*/
|
*/
|
||||||
package net.schmizz.sshj.sftp;
|
package net.schmizz.sshj.sftp;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileFilter;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.EnumSet;
|
||||||
|
|
||||||
import net.schmizz.sshj.common.StreamCopier;
|
import net.schmizz.sshj.common.StreamCopier;
|
||||||
import net.schmizz.sshj.sftp.Response.StatusCode;
|
import net.schmizz.sshj.sftp.Response.StatusCode;
|
||||||
import net.schmizz.sshj.xfer.AbstractFileTransfer;
|
import net.schmizz.sshj.xfer.AbstractFileTransfer;
|
||||||
|
import net.schmizz.sshj.xfer.FileSystemFile;
|
||||||
import net.schmizz.sshj.xfer.FileTransfer;
|
import net.schmizz.sshj.xfer.FileTransfer;
|
||||||
import net.schmizz.sshj.xfer.FileTransferUtil;
|
import net.schmizz.sshj.xfer.FileTransferUtil;
|
||||||
|
import net.schmizz.sshj.xfer.LocalFile;
|
||||||
import net.schmizz.sshj.xfer.TransferListener;
|
import net.schmizz.sshj.xfer.TransferListener;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileFilter;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.EnumSet;
|
|
||||||
|
|
||||||
public class SFTPFileTransfer
|
public class SFTPFileTransfer
|
||||||
extends AbstractFileTransfer
|
extends AbstractFileTransfer
|
||||||
implements FileTransfer {
|
implements FileTransfer {
|
||||||
@@ -61,8 +63,14 @@ public class SFTPFileTransfer
|
|||||||
@Override
|
@Override
|
||||||
public void upload(String source, String dest)
|
public void upload(String source, String dest)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
new Uploader().upload(new File(source), dest);
|
new Uploader().upload(new FileSystemFile(source), dest);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void upload(LocalFile localFile, String remotePath)
|
||||||
|
throws IOException {
|
||||||
|
new Uploader().upload(localFile, remotePath);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void download(String source, String dest)
|
public void download(String source, String dest)
|
||||||
@@ -163,7 +171,7 @@ public class SFTPFileTransfer
|
|||||||
|
|
||||||
private final TransferListener listener = getTransferListener();
|
private final TransferListener listener = getTransferListener();
|
||||||
|
|
||||||
private void upload(File local, String remote)
|
private void upload(LocalFile local, String remote)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
final String adjustedPath;
|
final String adjustedPath;
|
||||||
if (local.isDirectory()) {
|
if (local.isDirectory()) {
|
||||||
@@ -179,22 +187,22 @@ public class SFTPFileTransfer
|
|||||||
engine.setAttributes(adjustedPath, getAttributes(local));
|
engine.setAttributes(adjustedPath, getAttributes(local));
|
||||||
}
|
}
|
||||||
|
|
||||||
private String uploadDir(File local, String remote)
|
private String uploadDir(LocalFile local, String remote)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
final String adjusted = prepareDir(local, remote);
|
final String adjusted = prepareDir(local, remote);
|
||||||
for (File f : local.listFiles(getUploadFilter()))
|
for (LocalFile f : local.getChildren(getUploadFilter()))
|
||||||
upload(f, adjusted);
|
upload(f, adjusted);
|
||||||
return adjusted;
|
return adjusted;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String uploadFile(File local, String remote)
|
private String uploadFile(LocalFile local, String remote)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
final String adjusted = prepareFile(local, remote);
|
final String adjusted = prepareFile(local, remote);
|
||||||
final RemoteFile rf = engine.open(adjusted, EnumSet.of(OpenMode.WRITE,
|
final RemoteFile rf = engine.open(adjusted, EnumSet.of(OpenMode.WRITE,
|
||||||
OpenMode.CREAT,
|
OpenMode.CREAT,
|
||||||
OpenMode.TRUNC));
|
OpenMode.TRUNC));
|
||||||
try {
|
try {
|
||||||
final FileInputStream fis = new FileInputStream(local);
|
final InputStream fis = local.stream();
|
||||||
try {
|
try {
|
||||||
final int bufSize = engine.getSubsystem().getRemoteMaxPacketSize() - rf.getOutgoingPacketOverhead();
|
final int bufSize = engine.getSubsystem().getRemoteMaxPacketSize() - rf.getOutgoingPacketOverhead();
|
||||||
StreamCopier.copy(fis, rf.getOutputStream(), bufSize, false, listener);
|
StreamCopier.copy(fis, rf.getOutputStream(), bufSize, false, listener);
|
||||||
@@ -207,7 +215,7 @@ public class SFTPFileTransfer
|
|||||||
return adjusted;
|
return adjusted;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String prepareDir(File local, String remote)
|
private String prepareDir(LocalFile local, String remote)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
final FileAttributes attrs;
|
final FileAttributes attrs;
|
||||||
try {
|
try {
|
||||||
@@ -233,7 +241,7 @@ public class SFTPFileTransfer
|
|||||||
throw new IOException(attrs.getMode().getType() + " file already exists at " + remote);
|
throw new IOException(attrs.getMode().getType() + " file already exists at " + remote);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String prepareFile(File local, String remote)
|
private String prepareFile(LocalFile local, String remote)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
final FileAttributes attrs;
|
final FileAttributes attrs;
|
||||||
try {
|
try {
|
||||||
@@ -255,7 +263,7 @@ public class SFTPFileTransfer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private FileAttributes getAttributes(File local)
|
private FileAttributes getAttributes(LocalFile local)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
final FileAttributes.Builder builder = new FileAttributes.Builder()
|
final FileAttributes.Builder builder = new FileAttributes.Builder()
|
||||||
.withPermissions(getModeGetter().getPermissions(local));
|
.withPermissions(getModeGetter().getPermissions(local));
|
||||||
|
|||||||
@@ -57,6 +57,12 @@ public class FileSystemFile implements LocalFile {
|
|||||||
return getChildren(file);
|
return getChildren(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterable<LocalFile> getChildren(FileFilter filter) throws IOException {
|
||||||
|
setFileFilter(filter);
|
||||||
|
return getChildren(file);
|
||||||
|
}
|
||||||
|
|
||||||
private Iterable<LocalFile> getChildren(File f) throws IOException {
|
private Iterable<LocalFile> getChildren(File f) throws IOException {
|
||||||
Collection<LocalFile> files = new ArrayList<LocalFile>();
|
Collection<LocalFile> files = new ArrayList<LocalFile>();
|
||||||
File[] childFiles = fileFilter == null ? f.listFiles() : f.listFiles(fileFilter);
|
File[] childFiles = fileFilter == null ? f.listFiles() : f.listFiles(fileFilter);
|
||||||
|
|||||||
@@ -22,6 +22,9 @@ public interface FileTransfer {
|
|||||||
void upload(String localPath, String remotePath)
|
void upload(String localPath, String remotePath)
|
||||||
throws IOException;
|
throws IOException;
|
||||||
|
|
||||||
|
void upload(LocalFile localFile, String remotePath)
|
||||||
|
throws IOException;
|
||||||
|
|
||||||
void download(String remotePath, String localPath)
|
void download(String remotePath, String localPath)
|
||||||
throws IOException;
|
throws IOException;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package net.schmizz.sshj.xfer;
|
package net.schmizz.sshj.xfer;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.FileFilter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@@ -72,4 +73,10 @@ public class InMemoryFile implements LocalFile {
|
|||||||
public Iterable<LocalFile> getChildren() {
|
public Iterable<LocalFile> getChildren() {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterable<LocalFile> getChildren(FileFilter filter)
|
||||||
|
throws IOException {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package net.schmizz.sshj.xfer;
|
package net.schmizz.sshj.xfer;
|
||||||
|
|
||||||
|
import java.io.FileFilter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
@@ -16,4 +17,5 @@ public interface LocalFile {
|
|||||||
InputStream stream() throws IOException;
|
InputStream stream() throws IOException;
|
||||||
|
|
||||||
Iterable<LocalFile> getChildren() throws IOException;
|
Iterable<LocalFile> getChildren() throws IOException;
|
||||||
|
Iterable<LocalFile> getChildren(FileFilter filter) throws IOException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ public class SCPFileTransfer
|
|||||||
newSCPUploadClient().copy(new FileSystemFile(localPath), remotePath);
|
newSCPUploadClient().copy(new FileSystemFile(localPath), remotePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void upload(LocalFile localFile, String remotePath)
|
public void upload(LocalFile localFile, String remotePath)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
newSCPUploadClient().copy(localFile, remotePath);
|
newSCPUploadClient().copy(localFile, remotePath);
|
||||||
|
|||||||
Reference in New Issue
Block a user