use of com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate in project AmazeFileManager by TeamAmaze.
the class HybridFile method forEachChildrenFile.
/**
* Helper method to list children of this file
*/
public void forEachChildrenFile(Context context, boolean isRoot, OnFileFound onFileFound) {
switch(mode) {
case SFTP:
try {
SshClientUtils.execute(new SFtpClientTemplate(path) {
@Override
public Void execute(SFTPClient client) throws IOException {
try {
for (RemoteResourceInfo info : client.ls(SshClientUtils.extractRemotePathFrom(path))) {
HybridFileParcelable f = new HybridFileParcelable(String.format("%s/%s", path, info.getName()));
f.setName(info.getName());
f.setMode(OpenMode.SFTP);
f.setDirectory(info.isDirectory());
f.setDate(info.getAttributes().getMtime() * 1000);
f.setSize(f.isDirectory() ? 0 : info.getAttributes().getSize());
f.setPermission(Integer.toString(FilePermission.toMask(info.getAttributes().getPermissions()), 8));
onFileFound.onFileFound(f);
}
} catch (IOException e) {
Log.w("DEBUG.listFiles", "IOException", e);
}
return null;
}
});
} catch (Exception e) {
e.printStackTrace();
}
break;
case SMB:
try {
SmbFile smbFile = new SmbFile(path);
for (SmbFile smbFile1 : smbFile.listFiles()) {
HybridFileParcelable baseFile = new HybridFileParcelable(smbFile1.getPath());
baseFile.setName(smbFile1.getName());
baseFile.setMode(OpenMode.SMB);
baseFile.setDirectory(smbFile1.isDirectory());
baseFile.setDate(smbFile1.lastModified());
baseFile.setSize(baseFile.isDirectory() ? 0 : smbFile1.length());
onFileFound.onFileFound(baseFile);
}
} catch (MalformedURLException | SmbException e) {
e.printStackTrace();
}
break;
case OTG:
OTGUtil.getDocumentFiles(path, context, onFileFound);
break;
case DROPBOX:
case BOX:
case GDRIVE:
case ONEDRIVE:
try {
CloudUtil.getCloudFiles(path, dataUtils.getAccount(mode), mode, onFileFound);
} catch (CloudPluginException e) {
e.printStackTrace();
}
break;
default:
RootHelper.getFiles(path, isRoot, true, null, onFileFound);
}
}
use of com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate in project AmazeFileManager by TeamAmaze.
the class HybridFile method delete.
public boolean delete(Context context, boolean rootmode) throws ShellNotRunningException {
if (isSftp()) {
SshClientUtils.execute(new SFtpClientTemplate(path) {
@Override
public Void execute(SFTPClient client) throws IOException {
if (isDirectory(AppConfig.getInstance()))
client.rmdir(SshClientUtils.extractRemotePathFrom(path));
else
client.rm(SshClientUtils.extractRemotePathFrom(path));
return null;
}
});
return true;
} else if (isSmb()) {
try {
new SmbFile(path).delete();
} catch (SmbException | MalformedURLException e) {
e.printStackTrace();
}
} else {
if (isRoot() && rootmode) {
setMode(OpenMode.ROOT);
RootUtils.delete(getPath());
} else {
FileUtil.deleteFile(new File(path), context);
}
}
return !exists();
}
use of com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate in project AmazeFileManager by TeamAmaze.
the class HybridFile method getUsableSpace.
/**
* Gets usable i.e. free space of a device
* @return
*/
public long getUsableSpace() {
long size = 0L;
switch(mode) {
case SMB:
try {
size = (new SmbFile(path).getDiskFreeSpace());
} catch (MalformedURLException e) {
size = 0L;
e.printStackTrace();
} catch (SmbException e) {
size = 0L;
e.printStackTrace();
}
break;
case FILE:
case ROOT:
size = new File(path).getUsableSpace();
break;
case DROPBOX:
case BOX:
case GDRIVE:
case ONEDRIVE:
SpaceAllocation spaceAllocation = dataUtils.getAccount(mode).getAllocation();
size = spaceAllocation.getTotal() - spaceAllocation.getUsed();
break;
case SFTP:
size = SshClientUtils.execute(new SFtpClientTemplate(path) {
@Override
public Long execute(@NonNull SFTPClient client) throws IOException {
try {
Statvfs.Response response = new Statvfs.Response(path, client.getSFTPEngine().request(Statvfs.request(client, SshClientUtils.extractRemotePathFrom(path))).retrieve());
return response.diskFreeSpace();
} catch (SFTPException e) {
Log.e(TAG, "Error querying server", e);
return 0L;
} catch (Buffer.BufferException e) {
Log.e(TAG, "Error parsing reply", e);
return 0L;
}
}
});
break;
case OTG:
// TODO: Get free space from OTG when {@link DocumentFile} API adds support
break;
}
return size;
}
use of com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate in project AmazeFileManager by TeamAmaze.
the class HybridFile method getInputStream.
/**
* Handles getting input stream for various {@link OpenMode}
* @deprecated use {@link #getInputStream(Context)} which allows handling content resolver
* @return
*/
public InputStream getInputStream() {
InputStream inputStream;
if (isSftp()) {
return SshClientUtils.execute(new SFtpClientTemplate(path) {
@Override
public InputStream execute(SFTPClient client) throws IOException {
final RemoteFile rf = client.open(SshClientUtils.extractRemotePathFrom(path));
return rf.new RemoteFileInputStream() {
@Override
public void close() throws IOException {
try {
super.close();
} finally {
rf.close();
}
}
};
}
});
} else if (isSmb()) {
try {
inputStream = new SmbFile(path).getInputStream();
} catch (IOException e) {
inputStream = null;
e.printStackTrace();
}
} else {
try {
inputStream = new FileInputStream(path);
} catch (FileNotFoundException e) {
inputStream = null;
e.printStackTrace();
}
}
return inputStream;
}
use of com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate in project AmazeFileManager by TeamAmaze.
the class HybridFile method listFiles.
/**
* Helper method to list children of this file
* @deprecated use forEachChildrenFile()
*/
public ArrayList<HybridFileParcelable> listFiles(Context context, boolean isRoot) {
ArrayList<HybridFileParcelable> arrayList = new ArrayList<>();
switch(mode) {
case SFTP:
try {
arrayList = SshClientUtils.execute(new SFtpClientTemplate(path) {
@Override
public ArrayList<HybridFileParcelable> execute(SFTPClient client) throws IOException {
ArrayList<HybridFileParcelable> retval = new ArrayList<HybridFileParcelable>();
try {
for (RemoteResourceInfo info : client.ls(SshClientUtils.extractRemotePathFrom(path))) {
HybridFileParcelable f = new HybridFileParcelable(String.format("%s/%s", path, info.getName()));
f.setName(info.getName());
f.setMode(OpenMode.SFTP);
f.setDirectory(info.isDirectory());
f.setDate(info.getAttributes().getMtime() * 1000);
f.setSize(f.isDirectory() ? 0 : info.getAttributes().getSize());
f.setPermission(Integer.toString(FilePermission.toMask(info.getAttributes().getPermissions()), 8));
retval.add(f);
}
} catch (IOException e) {
Log.w("DEBUG.listFiles", "IOException", e);
}
return retval;
}
});
} catch (Exception e) {
e.printStackTrace();
arrayList.clear();
}
break;
case SMB:
try {
SmbFile smbFile = new SmbFile(path);
for (SmbFile smbFile1 : smbFile.listFiles()) {
HybridFileParcelable baseFile = new HybridFileParcelable(smbFile1.getPath());
baseFile.setName(smbFile1.getName());
baseFile.setMode(OpenMode.SMB);
baseFile.setDirectory(smbFile1.isDirectory());
baseFile.setDate(smbFile1.lastModified());
baseFile.setSize(baseFile.isDirectory() ? 0 : smbFile1.length());
arrayList.add(baseFile);
}
} catch (MalformedURLException e) {
if (arrayList != null)
arrayList.clear();
e.printStackTrace();
} catch (SmbException e) {
if (arrayList != null)
arrayList.clear();
e.printStackTrace();
}
break;
case OTG:
arrayList = OTGUtil.getDocumentFilesList(path, context);
break;
case DROPBOX:
case BOX:
case GDRIVE:
case ONEDRIVE:
try {
arrayList = CloudUtil.listFiles(path, dataUtils.getAccount(mode), mode);
} catch (CloudPluginException e) {
e.printStackTrace();
arrayList = new ArrayList<>();
}
break;
default:
arrayList = RootHelper.getFilesList(path, isRoot, true, null);
}
return arrayList;
}
Aggregations