use of com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate in project AmazeFileManager by TeamAmaze.
the class HybridFile method getTotal.
/**
* Gets total size of the disk
* @param context
* @return
*/
public long getTotal(Context context) {
long size = 0l;
switch(mode) {
case SMB:
// TODO: Find total storage space of SMB when JCIFS adds support
try {
size = new SmbFile(path).getDiskFreeSpace();
} catch (SmbException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
break;
case FILE:
case ROOT:
size = new File(path).getTotalSpace();
break;
case DROPBOX:
case BOX:
case ONEDRIVE:
case GDRIVE:
SpaceAllocation spaceAllocation = dataUtils.getAccount(mode).getAllocation();
size = spaceAllocation.getTotal();
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.diskSize();
} 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: Find total storage space of OTG when {@link DocumentFile} API adds support
DocumentFile documentFile = OTGUtil.getDocumentFile(path, context, false);
documentFile.length();
break;
}
return size;
}
use of com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate in project AmazeFileManager by TeamAmaze.
the class HybridFile method exists.
public boolean exists() {
boolean exists = false;
if (isSftp()) {
exists = SshClientUtils.execute(new SFtpClientTemplate(path) {
@Override
public Boolean execute(SFTPClient client) throws IOException {
try {
return client.stat(SshClientUtils.extractRemotePathFrom(path)) != null;
} catch (SFTPException notFound) {
return false;
}
}
});
} else if (isSmb()) {
try {
SmbFile smbFile = getSmbFile(2000);
exists = smbFile != null && smbFile.exists();
} catch (SmbException e) {
exists = false;
}
} else if (isDropBoxFile()) {
CloudStorage cloudStorageDropbox = dataUtils.getAccount(OpenMode.DROPBOX);
exists = cloudStorageDropbox.exists(CloudUtil.stripPath(OpenMode.DROPBOX, path));
} else if (isBoxFile()) {
CloudStorage cloudStorageBox = dataUtils.getAccount(OpenMode.BOX);
exists = cloudStorageBox.exists(CloudUtil.stripPath(OpenMode.BOX, path));
} else if (isGoogleDriveFile()) {
CloudStorage cloudStorageGoogleDrive = dataUtils.getAccount(OpenMode.GDRIVE);
exists = cloudStorageGoogleDrive.exists(CloudUtil.stripPath(OpenMode.GDRIVE, path));
} else if (isOneDriveFile()) {
CloudStorage cloudStorageOneDrive = dataUtils.getAccount(OpenMode.ONEDRIVE);
exists = cloudStorageOneDrive.exists(CloudUtil.stripPath(OpenMode.ONEDRIVE, path));
} else if (isLocal()) {
exists = new File(path).exists();
} else if (isRoot()) {
try {
return RootHelper.fileExists(path);
} catch (ShellNotRunningException e) {
e.printStackTrace();
return false;
}
}
return exists;
}
use of com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate in project AmazeFileManager by TeamAmaze.
the class HybridFile method getInputStream.
public InputStream getInputStream(Context context) {
InputStream inputStream;
switch(mode) {
case SFTP:
inputStream = SshClientUtils.execute(new SFtpClientTemplate(path, false) {
@Override
public InputStream execute(final 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();
client.close();
}
}
};
}
});
break;
case SMB:
try {
inputStream = new SmbFile(path).getInputStream();
} catch (IOException e) {
inputStream = null;
e.printStackTrace();
}
break;
case OTG:
ContentResolver contentResolver = context.getContentResolver();
DocumentFile documentSourceFile = OTGUtil.getDocumentFile(path, context, false);
try {
inputStream = contentResolver.openInputStream(documentSourceFile.getUri());
} catch (FileNotFoundException e) {
e.printStackTrace();
inputStream = null;
}
break;
case DROPBOX:
CloudStorage cloudStorageDropbox = dataUtils.getAccount(OpenMode.DROPBOX);
Log.d(getClass().getSimpleName(), CloudUtil.stripPath(OpenMode.DROPBOX, path));
inputStream = cloudStorageDropbox.download(CloudUtil.stripPath(OpenMode.DROPBOX, path));
break;
case BOX:
CloudStorage cloudStorageBox = dataUtils.getAccount(OpenMode.BOX);
inputStream = cloudStorageBox.download(CloudUtil.stripPath(OpenMode.BOX, path));
break;
case GDRIVE:
CloudStorage cloudStorageGDrive = dataUtils.getAccount(OpenMode.GDRIVE);
inputStream = cloudStorageGDrive.download(CloudUtil.stripPath(OpenMode.GDRIVE, path));
break;
case ONEDRIVE:
CloudStorage cloudStorageOneDrive = dataUtils.getAccount(OpenMode.ONEDRIVE);
inputStream = cloudStorageOneDrive.download(CloudUtil.stripPath(OpenMode.ONEDRIVE, path));
break;
default:
try {
inputStream = new FileInputStream(path);
} catch (FileNotFoundException e) {
inputStream = null;
e.printStackTrace();
}
break;
}
return inputStream;
}
use of com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate in project AmazeFileManager by TeamAmaze.
the class Operations method rename.
public static void rename(final HybridFile oldFile, final HybridFile newFile, final boolean rootMode, final Context context, final ErrorCallBack errorCallBack) {
new AsyncTask<Void, Void, Void>() {
private DataUtils dataUtils = DataUtils.getInstance();
@Override
protected Void doInBackground(Void... params) {
// check whether file names for new file are valid or recursion occurs
if (MainActivityHelper.isNewDirectoryRecursive(newFile) || !Operations.isFileNameValid(newFile.getName(context))) {
errorCallBack.invalidName(newFile);
return null;
}
if (newFile.exists()) {
errorCallBack.exists(newFile);
return null;
}
if (oldFile.isSmb()) {
try {
SmbFile smbFile = new SmbFile(oldFile.getPath());
SmbFile smbFile1 = new SmbFile(newFile.getPath());
if (smbFile1.exists()) {
errorCallBack.exists(newFile);
return null;
}
smbFile.renameTo(smbFile1);
if (!smbFile.exists() && smbFile1.exists())
errorCallBack.done(newFile, true);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (SmbException e) {
e.printStackTrace();
}
return null;
} else if (oldFile.isSftp()) {
SshClientUtils.execute(new SFtpClientTemplate(oldFile.getPath()) {
@Override
public <Void> Void execute(@NonNull SFTPClient client) throws IOException {
try {
client.rename(SshClientUtils.extractRemotePathFrom(oldFile.getPath()), SshClientUtils.extractRemotePathFrom(newFile.getPath()));
errorCallBack.done(newFile, true);
} catch (IOException e) {
e.printStackTrace();
errorCallBack.done(newFile, false);
}
return null;
}
});
} else if (oldFile.isDropBoxFile()) {
CloudStorage cloudStorageDropbox = dataUtils.getAccount(OpenMode.DROPBOX);
try {
cloudStorageDropbox.move(CloudUtil.stripPath(OpenMode.DROPBOX, oldFile.getPath()), CloudUtil.stripPath(OpenMode.DROPBOX, newFile.getPath()));
errorCallBack.done(newFile, true);
} catch (Exception e) {
e.printStackTrace();
errorCallBack.done(newFile, false);
}
} else if (oldFile.isBoxFile()) {
CloudStorage cloudStorageBox = dataUtils.getAccount(OpenMode.BOX);
try {
cloudStorageBox.move(CloudUtil.stripPath(OpenMode.BOX, oldFile.getPath()), CloudUtil.stripPath(OpenMode.BOX, newFile.getPath()));
errorCallBack.done(newFile, true);
} catch (Exception e) {
e.printStackTrace();
errorCallBack.done(newFile, false);
}
} else if (oldFile.isOneDriveFile()) {
CloudStorage cloudStorageOneDrive = dataUtils.getAccount(OpenMode.ONEDRIVE);
try {
cloudStorageOneDrive.move(CloudUtil.stripPath(OpenMode.ONEDRIVE, oldFile.getPath()), CloudUtil.stripPath(OpenMode.ONEDRIVE, newFile.getPath()));
errorCallBack.done(newFile, true);
} catch (Exception e) {
e.printStackTrace();
errorCallBack.done(newFile, false);
}
} else if (oldFile.isGoogleDriveFile()) {
CloudStorage cloudStorageGdrive = dataUtils.getAccount(OpenMode.GDRIVE);
try {
cloudStorageGdrive.move(CloudUtil.stripPath(OpenMode.GDRIVE, oldFile.getPath()), CloudUtil.stripPath(OpenMode.GDRIVE, newFile.getPath()));
errorCallBack.done(newFile, true);
} catch (Exception e) {
e.printStackTrace();
errorCallBack.done(newFile, false);
}
} else if (oldFile.isOtgFile()) {
DocumentFile oldDocumentFile = OTGUtil.getDocumentFile(oldFile.getPath(), context, false);
DocumentFile newDocumentFile = OTGUtil.getDocumentFile(newFile.getPath(), context, false);
if (newDocumentFile != null) {
errorCallBack.exists(newFile);
return null;
}
errorCallBack.done(newFile, oldDocumentFile.renameTo(newFile.getName(context)));
return null;
} else {
File file = new File(oldFile.getPath());
File file1 = new File(newFile.getPath());
switch(oldFile.getMode()) {
case FILE:
int mode = checkFolder(file.getParentFile(), context);
if (mode == 2) {
errorCallBack.launchSAF(oldFile, newFile);
} else if (mode == 1 || mode == 0) {
try {
FileUtil.renameFolder(file, file1, context);
} catch (ShellNotRunningException e) {
e.printStackTrace();
}
boolean a = !file.exists() && file1.exists();
if (!a && rootMode) {
try {
RootUtils.rename(file.getPath(), file1.getPath());
} catch (ShellNotRunningException e) {
e.printStackTrace();
}
oldFile.setMode(OpenMode.ROOT);
newFile.setMode(OpenMode.ROOT);
a = !file.exists() && file1.exists();
}
errorCallBack.done(newFile, a);
return null;
}
break;
case ROOT:
try {
RootUtils.rename(file.getPath(), file1.getPath());
} catch (ShellNotRunningException e) {
e.printStackTrace();
}
newFile.setMode(OpenMode.ROOT);
errorCallBack.done(newFile, true);
break;
}
}
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Aggregations