Search in sources :

Example 6 with DataUtils

use of com.amaze.filemanager.utils.DataUtils 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);
}
Also used : MalformedURLException(java.net.MalformedURLException) DocumentFile(android.support.v4.provider.DocumentFile) SFTPClient(net.schmizz.sshj.sftp.SFTPClient) IOException(java.io.IOException) MalformedURLException(java.net.MalformedURLException) ShellNotRunningException(com.amaze.filemanager.exceptions.ShellNotRunningException) IOException(java.io.IOException) SmbException(jcifs.smb.SmbException) SmbFile(jcifs.smb.SmbFile) SmbException(jcifs.smb.SmbException) CloudStorage(com.cloudrail.si.interfaces.CloudStorage) SFtpClientTemplate(com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate) ShellNotRunningException(com.amaze.filemanager.exceptions.ShellNotRunningException) DataUtils(com.amaze.filemanager.utils.DataUtils) File(java.io.File) SmbFile(jcifs.smb.SmbFile) DocumentFile(android.support.v4.provider.DocumentFile)

Example 7 with DataUtils

use of com.amaze.filemanager.utils.DataUtils in project AmazeFileManager by TeamAmaze.

the class FileUtils method folderSizeCloud.

public static long folderSizeCloud(OpenMode openMode, CloudMetaData sourceFileMeta) {
    DataUtils dataUtils = DataUtils.getInstance();
    long length = 0;
    CloudStorage cloudStorage = dataUtils.getAccount(openMode);
    for (CloudMetaData metaData : cloudStorage.getChildren(CloudUtil.stripPath(openMode, sourceFileMeta.getPath()))) {
        if (metaData.getFolder()) {
            length += folderSizeCloud(openMode, metaData);
        } else {
            length += metaData.getSize();
        }
    }
    return length;
}
Also used : CloudStorage(com.cloudrail.si.interfaces.CloudStorage) DataUtils(com.amaze.filemanager.utils.DataUtils) CloudMetaData(com.cloudrail.si.types.CloudMetaData)

Aggregations

DataUtils (com.amaze.filemanager.utils.DataUtils)7 CloudStorage (com.cloudrail.si.interfaces.CloudStorage)6 File (java.io.File)5 ShellNotRunningException (com.amaze.filemanager.exceptions.ShellNotRunningException)4 MalformedURLException (java.net.MalformedURLException)4 SmbException (jcifs.smb.SmbException)4 SmbFile (jcifs.smb.SmbFile)4 DocumentFile (android.support.v4.provider.DocumentFile)3 IOException (java.io.IOException)3 HybridFileParcelable (com.amaze.filemanager.filesystem.HybridFileParcelable)2 ActivityNotFoundException (android.content.ActivityNotFoundException)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 LayoutElementParcelable (com.amaze.filemanager.adapters.data.LayoutElementParcelable)1 CloudPluginException (com.amaze.filemanager.exceptions.CloudPluginException)1 PasteHelper (com.amaze.filemanager.filesystem.PasteHelper)1 SFtpClientTemplate (com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate)1 OpenMode (com.amaze.filemanager.utils.OpenMode)1 EncryptDecryptUtils (com.amaze.filemanager.utils.files.EncryptDecryptUtils)1 CloudMetaData (com.cloudrail.si.types.CloudMetaData)1