Search in sources :

Example 1 with ShellNotRunningException

use of com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException in project AmazeFileManager by TeamAmaze.

the class Operations method mkfile.

public static void mkfile(final HybridFile parentFile, @NonNull final HybridFile file, final Context context, final boolean rootMode, @NonNull final ErrorCallBack errorCallBack) {
    new AsyncTask<Void, Void, Void>() {

        private DataUtils dataUtils = DataUtils.getInstance();

        private Function<DocumentFile, Void> safCreateFile = input -> {
            if (input != null && input.isDirectory()) {
                boolean result = false;
                try {
                    result = input.createFile(file.getName(context).substring(file.getName(context).lastIndexOf(".")), file.getName(context)) != null;
                } catch (Exception e) {
                    Log.w(getClass().getSimpleName(), "Failed to make file", e);
                }
                errorCallBack.done(file, result);
            } else
                errorCallBack.done(file, false);
            return null;
        };

        @Override
        protected Void doInBackground(Void... params) {
            // check whether filename is valid or not
            if (!Operations.isFileNameValid(file.getName(context))) {
                errorCallBack.invalidName(file);
                return null;
            }
            if (file.exists()) {
                errorCallBack.exists(file);
                return null;
            }
            if (file.isSftp()) {
                OutputStream out = file.getOutputStream(context);
                if (out == null) {
                    errorCallBack.done(file, false);
                    return null;
                }
                try {
                    out.close();
                    errorCallBack.done(file, true);
                    return null;
                } catch (IOException e) {
                    errorCallBack.done(file, false);
                    return null;
                }
            }
            if (file.isSmb()) {
                try {
                    file.getSmbFile(2000).createNewFile();
                } catch (SmbException e) {
                    e.printStackTrace();
                    errorCallBack.done(file, false);
                    return null;
                }
                errorCallBack.done(file, file.exists());
                return null;
            } else if (file.isDropBoxFile()) {
                CloudStorage cloudStorageDropbox = dataUtils.getAccount(OpenMode.DROPBOX);
                try {
                    byte[] tempBytes = new byte[0];
                    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(tempBytes);
                    cloudStorageDropbox.upload(CloudUtil.stripPath(OpenMode.DROPBOX, file.getPath()), byteArrayInputStream, 0l, true);
                    errorCallBack.done(file, true);
                } catch (Exception e) {
                    e.printStackTrace();
                    errorCallBack.done(file, false);
                }
            } else if (file.isBoxFile()) {
                CloudStorage cloudStorageBox = dataUtils.getAccount(OpenMode.BOX);
                try {
                    byte[] tempBytes = new byte[0];
                    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(tempBytes);
                    cloudStorageBox.upload(CloudUtil.stripPath(OpenMode.BOX, file.getPath()), byteArrayInputStream, 0l, true);
                    errorCallBack.done(file, true);
                } catch (Exception e) {
                    e.printStackTrace();
                    errorCallBack.done(file, false);
                }
            } else if (file.isOneDriveFile()) {
                CloudStorage cloudStorageOneDrive = dataUtils.getAccount(OpenMode.ONEDRIVE);
                try {
                    byte[] tempBytes = new byte[0];
                    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(tempBytes);
                    cloudStorageOneDrive.upload(CloudUtil.stripPath(OpenMode.ONEDRIVE, file.getPath()), byteArrayInputStream, 0l, true);
                    errorCallBack.done(file, true);
                } catch (Exception e) {
                    e.printStackTrace();
                    errorCallBack.done(file, false);
                }
            } else if (file.isGoogleDriveFile()) {
                CloudStorage cloudStorageGdrive = dataUtils.getAccount(OpenMode.GDRIVE);
                try {
                    byte[] tempBytes = new byte[0];
                    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(tempBytes);
                    cloudStorageGdrive.upload(CloudUtil.stripPath(OpenMode.GDRIVE, file.getPath()), byteArrayInputStream, 0l, true);
                    errorCallBack.done(file, true);
                } catch (Exception e) {
                    e.printStackTrace();
                    errorCallBack.done(file, false);
                }
            } else if (file.isOtgFile()) {
                if (checkOtgNewFileExists(file, context)) {
                    errorCallBack.exists(file);
                    return null;
                }
                safCreateFile.apply(OTGUtil.getDocumentFile(parentFile.getPath(), context, false));
                return null;
            } else if (file.isDocumentFile()) {
                if (checkDocumentFileNewFileExists(file, context)) {
                    errorCallBack.exists(file);
                    return null;
                }
                safCreateFile.apply(OTGUtil.getDocumentFile(parentFile.getPath(), SafRootHolder.getUriRoot(), context, OpenMode.DOCUMENT_FILE, false));
                return null;
            } else {
                if (file.isLocal() || file.isRoot()) {
                    int mode = checkFolder(new File(file.getParent(context)), context);
                    if (mode == 2) {
                        errorCallBack.launchSAF(file);
                        return null;
                    }
                    if (mode == 1 || mode == 0)
                        MakeFileOperation.mkfile(file.getFile(), context);
                    if (!file.exists() && rootMode) {
                        file.setMode(OpenMode.ROOT);
                        if (file.exists())
                            errorCallBack.exists(file);
                        try {
                            MakeFileCommand.INSTANCE.makeFile(file.getPath());
                        } catch (ShellNotRunningException e) {
                            e.printStackTrace();
                        }
                        errorCallBack.done(file, file.exists());
                        return null;
                    }
                    errorCallBack.done(file, file.exists());
                    return null;
                }
                errorCallBack.done(file, file.exists());
            }
            return null;
        }
    }.executeOnExecutor(executor);
}
Also used : Context(android.content.Context) CloudStorage(com.cloudrail.si.interfaces.CloudStorage) URL(java.net.URL) MakeDirectoryCommand(com.amaze.filemanager.filesystem.root.MakeDirectoryCommand) NonNull(androidx.annotation.NonNull) MakeFileCommand(com.amaze.filemanager.filesystem.root.MakeFileCommand) Intent(android.content.Intent) SmbException(jcifs.smb.SmbException) OpenMode(com.amaze.filemanager.file_operations.filesystem.OpenMode) CloudUtil(com.amaze.filemanager.filesystem.cloud.CloudUtil) TAG_INTENT_FILTER_GENERAL(com.amaze.filemanager.ui.activities.MainActivity.TAG_INTENT_FILTER_GENERAL) ArrayList(java.util.ArrayList) ByteArrayInputStream(java.io.ByteArrayInputStream) SshClientUtils(com.amaze.filemanager.filesystem.ssh.SshClientUtils) ShellNotRunningException(com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException) DataUtils(com.amaze.filemanager.utils.DataUtils) SFTPClient(net.schmizz.sshj.sftp.SFTPClient) Build(android.os.Build) Function(androidx.arch.core.util.Function) TAG_INTENT_FILTER_FAILED_OPS(com.amaze.filemanager.ui.activities.MainActivity.TAG_INTENT_FILTER_FAILED_OPS) Log(android.util.Log) OutputStream(java.io.OutputStream) MalformedURLException(java.net.MalformedURLException) Executor(java.util.concurrent.Executor) OTGUtil(com.amaze.filemanager.utils.OTGUtil) AsyncTask(android.os.AsyncTask) IOException(java.io.IOException) TextUtils(android.text.TextUtils) FileUtils(com.amaze.filemanager.filesystem.files.FileUtils) File(java.io.File) SmbFile(jcifs.smb.SmbFile) DocumentFile(androidx.documentfile.provider.DocumentFile) RenameFileCommand(com.amaze.filemanager.filesystem.root.RenameFileCommand) R(com.amaze.filemanager.R) SFtpClientTemplate(com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate) DocumentFile(androidx.documentfile.provider.DocumentFile) OutputStream(java.io.OutputStream) IOException(java.io.IOException) SmbException(jcifs.smb.SmbException) ShellNotRunningException(com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SmbException(jcifs.smb.SmbException) CloudStorage(com.cloudrail.si.interfaces.CloudStorage) ByteArrayInputStream(java.io.ByteArrayInputStream) ShellNotRunningException(com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException) DataUtils(com.amaze.filemanager.utils.DataUtils) File(java.io.File) SmbFile(jcifs.smb.SmbFile) DocumentFile(androidx.documentfile.provider.DocumentFile)

Example 2 with ShellNotRunningException

use of com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException in project AmazeFileManager by TeamAmaze.

the class MoveFiles method doInBackground.

@Override
protected Boolean doInBackground(ArrayList<String>... strings) {
    paths = strings[0];
    if (files.size() == 0)
        return true;
    for (ArrayList<HybridFileParcelable> filesCurrent : files) {
        totalBytes += FileUtils.getTotalBytes(filesCurrent, context);
    }
    HybridFile destination = new HybridFile(mode, paths.get(0));
    destinationSize = destination.getUsableSpace();
    for (int i = 0; i < paths.size(); i++) {
        for (HybridFileParcelable baseFile : files.get(i)) {
            String destPath = paths.get(i) + "/" + baseFile.getName(context);
            if (baseFile.getPath().indexOf('?') > 0)
                destPath += baseFile.getPath().substring(baseFile.getPath().indexOf('?'));
            if (!isMoveOperationValid(baseFile, new HybridFile(mode, paths.get(i)))) {
                // TODO: 30/06/20 Replace runtime exception with generic exception
                Log.w(getClass().getSimpleName(), "Some files failed to be moved", new RuntimeException());
                invalidOperation = true;
                continue;
            }
            switch(mode) {
                case FILE:
                    File dest = new File(destPath);
                    File source = new File(baseFile.getPath());
                    if (!source.renameTo(dest)) {
                        // check if we have root
                        if (isRootExplorer) {
                            try {
                                if (!RenameFileCommand.INSTANCE.renameFile(baseFile.getPath(), destPath))
                                    return false;
                            } catch (ShellNotRunningException e) {
                                e.printStackTrace();
                                return false;
                            }
                        } else
                            return false;
                    }
                    break;
                case DROPBOX:
                case BOX:
                case ONEDRIVE:
                case GDRIVE:
                    DataUtils dataUtils = DataUtils.getInstance();
                    CloudStorage cloudStorage = dataUtils.getAccount(mode);
                    if (baseFile.getMode() == mode) {
                        // source and target both in same filesystem, use API method
                        try {
                            cloudStorage.move(CloudUtil.stripPath(mode, baseFile.getPath()), CloudUtil.stripPath(mode, destPath));
                        } catch (Exception e) {
                            e.printStackTrace();
                            return false;
                        }
                    } else {
                        // not in same filesystem, execute service
                        return false;
                    }
                default:
                    return false;
            }
        }
    }
    return true;
}
Also used : HybridFileParcelable(com.amaze.filemanager.filesystem.HybridFileParcelable) CloudStorage(com.cloudrail.si.interfaces.CloudStorage) HybridFile(com.amaze.filemanager.filesystem.HybridFile) ShellNotRunningException(com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException) DataUtils(com.amaze.filemanager.utils.DataUtils) HybridFile(com.amaze.filemanager.filesystem.HybridFile) File(java.io.File) ShellNotRunningException(com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException)

Example 3 with ShellNotRunningException

use of com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException in project AmazeFileManager by TeamAmaze.

the class ReadFileTask method loadFile.

private InputStream loadFile(File file) {
    InputStream inputStream = null;
    if (!file.canWrite() && isRootExplorer) {
        // try loading stream associated using root
        try {
            cachedFile = new File(externalCacheDir, file.getName());
            // creating a cache file
            CopyFilesCommand.INSTANCE.copyFiles(file.getAbsolutePath(), cachedFile.getPath());
            inputStream = new FileInputStream(cachedFile);
        } catch (ShellNotRunningException e) {
            e.printStackTrace();
            inputStream = null;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            inputStream = null;
        }
    } else if (file.canRead()) {
        // readable file in filesystem
        try {
            inputStream = new FileInputStream(file.getAbsolutePath());
        } catch (FileNotFoundException e) {
            Log.e(TAG, "Unable to open file [" + file.getAbsolutePath() + "] for reading", e);
            inputStream = null;
        }
    }
    return inputStream;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ShellNotRunningException(com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) DocumentFile(androidx.documentfile.provider.DocumentFile) FileInputStream(java.io.FileInputStream)

Example 4 with ShellNotRunningException

use of com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException in project AmazeFileManager by TeamAmaze.

the class WriteFileAbstraction method doInBackground.

@Override
protected Integer doInBackground(Void... voids) {
    try {
        OutputStream outputStream;
        File destFile = null;
        switch(fileAbstraction.scheme) {
            case CONTENT:
                if (fileAbstraction.uri == null)
                    throw new NullPointerException("Something went really wrong!");
                try {
                    if (fileAbstraction.uri.getAuthority().equals(context.get().getPackageName())) {
                        DocumentFile documentFile = DocumentFile.fromSingleUri(AppConfig.getInstance(), fileAbstraction.uri);
                        if (documentFile != null && documentFile.exists() && documentFile.canWrite())
                            outputStream = contentResolver.openOutputStream(fileAbstraction.uri);
                        else {
                            destFile = FileUtils.fromContentUri(fileAbstraction.uri);
                            outputStream = openFile(destFile, context.get());
                        }
                    } else {
                        outputStream = contentResolver.openOutputStream(fileAbstraction.uri);
                    }
                } catch (RuntimeException e) {
                    throw new StreamNotFoundException(e);
                }
                break;
            case FILE:
                final HybridFileParcelable hybridFileParcelable = fileAbstraction.hybridFileParcelable;
                if (hybridFileParcelable == null)
                    throw new NullPointerException("Something went really wrong!");
                Context context = this.context.get();
                if (context == null) {
                    cancel(true);
                    return null;
                }
                outputStream = openFile(hybridFileParcelable.getFile(), context);
                destFile = fileAbstraction.hybridFileParcelable.getFile();
                break;
            default:
                throw new IllegalArgumentException("The scheme for '" + fileAbstraction.scheme + "' cannot be processed!");
        }
        if (outputStream == null)
            throw new StreamNotFoundException();
        outputStream.write(dataToSave.getBytes());
        outputStream.close();
        if (cachedFile != null && cachedFile.exists() && destFile != null) {
            // cat cache content to original file and delete cache file
            ConcatenateFileCommand.INSTANCE.concatenateFile(cachedFile.getPath(), destFile.getPath());
            cachedFile.delete();
        }
    } catch (IOException e) {
        e.printStackTrace();
        return EXCEPTION_IO;
    } catch (StreamNotFoundException e) {
        e.printStackTrace();
        return EXCEPTION_STREAM_NOT_FOUND;
    } catch (ShellNotRunningException e) {
        e.printStackTrace();
        return EXCEPTION_SHELL_NOT_RUNNING;
    }
    return NORMAL;
}
Also used : HybridFileParcelable(com.amaze.filemanager.filesystem.HybridFileParcelable) Context(android.content.Context) DocumentFile(androidx.documentfile.provider.DocumentFile) ShellNotRunningException(com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) StreamNotFoundException(com.amaze.filemanager.file_operations.exceptions.StreamNotFoundException) IOException(java.io.IOException) File(java.io.File) DocumentFile(androidx.documentfile.provider.DocumentFile)

Example 5 with ShellNotRunningException

use of com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException in project AmazeFileManager by TeamAmaze.

the class DatabaseViewerActivity method load.

private void load(final File file) {
    new Thread(() -> {
        File file1 = getExternalCacheDir();
        // first copying it in cache dir
        if (!file.canRead() && isRootExplorer()) {
            try {
                CopyFilesCommand.INSTANCE.copyFiles(pathFile.getPath(), new File(file1.getPath(), file.getName()).getPath());
                pathFile = new File(file1.getPath(), file.getName());
            } catch (ShellNotRunningException e) {
                e.printStackTrace();
            }
            delete = true;
        }
        try {
            sqLiteDatabase = SQLiteDatabase.openDatabase(pathFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);
            c = sqLiteDatabase.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
            arrayList = getDbTableNames(c);
            arrayAdapter = new ArrayAdapter(DatabaseViewerActivity.this, android.R.layout.simple_list_item_1, arrayList);
        } catch (Exception e) {
            e.printStackTrace();
            finish();
        }
        runOnUiThread(() -> {
            listView.setAdapter(arrayAdapter);
        });
    }).start();
}
Also used : ShellNotRunningException(com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException) File(java.io.File) ArrayAdapter(android.widget.ArrayAdapter) ShellNotRunningException(com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException)

Aggregations

ShellNotRunningException (com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException)8 File (java.io.File)7 DocumentFile (androidx.documentfile.provider.DocumentFile)5 Context (android.content.Context)4 DataUtils (com.amaze.filemanager.utils.DataUtils)4 CloudStorage (com.cloudrail.si.interfaces.CloudStorage)4 IOException (java.io.IOException)4 OutputStream (java.io.OutputStream)4 Intent (android.content.Intent)3 AsyncTask (android.os.AsyncTask)3 Build (android.os.Build)3 TextUtils (android.text.TextUtils)3 Log (android.util.Log)3 NonNull (androidx.annotation.NonNull)3 Function (androidx.arch.core.util.Function)3 R (com.amaze.filemanager.R)3 OpenMode (com.amaze.filemanager.file_operations.filesystem.OpenMode)3 CloudUtil (com.amaze.filemanager.filesystem.cloud.CloudUtil)3 FileUtils (com.amaze.filemanager.filesystem.files.FileUtils)3 MakeDirectoryCommand (com.amaze.filemanager.filesystem.root.MakeDirectoryCommand)3