Search in sources :

Example 11 with CloudStorage

use of com.cloudrail.si.interfaces.CloudStorage 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 12 with CloudStorage

use of com.cloudrail.si.interfaces.CloudStorage 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)

Example 13 with CloudStorage

use of com.cloudrail.si.interfaces.CloudStorage in project AmazeFileManager by TeamAmaze.

the class GenericCopyUtil method startCopy.

/**
 * Starts copy of file
 * Supports : {@link File}, {@link jcifs.smb.SmbFile}, {@link DocumentFile}, {@link CloudStorage}
 * @param lowOnMemory defines whether system is running low on memory, in which case we'll switch to
 *                    using streams instead of channel which maps the who buffer in memory.
 *                    TODO: Use buffers even on low memory but don't map the whole file to memory but
 *                          parts of it, and transfer each part instead.
 * @throws IOException
 */
private void startCopy(boolean lowOnMemory) throws IOException {
    FileInputStream inputStream = null;
    FileOutputStream outputStream = null;
    FileChannel inChannel = null;
    FileChannel outChannel = null;
    BufferedInputStream bufferedInputStream = null;
    BufferedOutputStream bufferedOutputStream = null;
    try {
        // initializing the input channels based on file types
        if (mSourceFile.isOtgFile()) {
            // source is in otg
            ContentResolver contentResolver = mContext.getContentResolver();
            DocumentFile documentSourceFile = OTGUtil.getDocumentFile(mSourceFile.getPath(), mContext, false);
            bufferedInputStream = new BufferedInputStream(contentResolver.openInputStream(documentSourceFile.getUri()), DEFAULT_BUFFER_SIZE);
        } else if (mSourceFile.isSmb()) {
            // source is in smb
            bufferedInputStream = new BufferedInputStream(mSourceFile.getInputStream(), DEFAULT_BUFFER_SIZE);
        } else if (mSourceFile.isSftp()) {
            bufferedInputStream = new BufferedInputStream(mSourceFile.getInputStream(mContext), DEFAULT_BUFFER_SIZE);
        } else if (mSourceFile.isDropBoxFile()) {
            CloudStorage cloudStorageDropbox = dataUtils.getAccount(OpenMode.DROPBOX);
            bufferedInputStream = new BufferedInputStream(cloudStorageDropbox.download(CloudUtil.stripPath(OpenMode.DROPBOX, mSourceFile.getPath())));
        } else if (mSourceFile.isBoxFile()) {
            CloudStorage cloudStorageBox = dataUtils.getAccount(OpenMode.BOX);
            bufferedInputStream = new BufferedInputStream(cloudStorageBox.download(CloudUtil.stripPath(OpenMode.BOX, mSourceFile.getPath())));
        } else if (mSourceFile.isGoogleDriveFile()) {
            CloudStorage cloudStorageGdrive = dataUtils.getAccount(OpenMode.GDRIVE);
            bufferedInputStream = new BufferedInputStream(cloudStorageGdrive.download(CloudUtil.stripPath(OpenMode.GDRIVE, mSourceFile.getPath())));
        } else if (mSourceFile.isOneDriveFile()) {
            CloudStorage cloudStorageOnedrive = dataUtils.getAccount(OpenMode.ONEDRIVE);
            bufferedInputStream = new BufferedInputStream(cloudStorageOnedrive.download(CloudUtil.stripPath(OpenMode.ONEDRIVE, mSourceFile.getPath())));
        } else {
            // source file is neither smb nor otg; getting a channel from direct file instead of stream
            File file = new File(mSourceFile.getPath());
            if (FileUtil.isReadable(file)) {
                if (mTargetFile.isOneDriveFile() || mTargetFile.isDropBoxFile() || mTargetFile.isGoogleDriveFile() || mTargetFile.isBoxFile() || lowOnMemory) {
                    // our target is cloud, we need a stream not channel
                    bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
                } else {
                    inChannel = new RandomAccessFile(file, "r").getChannel();
                }
            } else {
                ContentResolver contentResolver = mContext.getContentResolver();
                DocumentFile documentSourceFile = FileUtil.getDocumentFile(file, mSourceFile.isDirectory(), mContext);
                bufferedInputStream = new BufferedInputStream(contentResolver.openInputStream(documentSourceFile.getUri()), DEFAULT_BUFFER_SIZE);
            }
        }
        // initializing the output channels based on file types
        if (mTargetFile.isOtgFile()) {
            // target in OTG, obtain streams from DocumentFile Uri's
            ContentResolver contentResolver = mContext.getContentResolver();
            DocumentFile documentTargetFile = OTGUtil.getDocumentFile(mTargetFile.getPath(), mContext, true);
            bufferedOutputStream = new BufferedOutputStream(contentResolver.openOutputStream(documentTargetFile.getUri()), DEFAULT_BUFFER_SIZE);
        } else if (mTargetFile.isSftp()) {
            bufferedOutputStream = new BufferedOutputStream(mTargetFile.getOutputStream(mContext), DEFAULT_BUFFER_SIZE);
        } else if (mTargetFile.isSmb()) {
            bufferedOutputStream = new BufferedOutputStream(mTargetFile.getOutputStream(mContext), DEFAULT_BUFFER_SIZE);
        } else if (mTargetFile.isDropBoxFile()) {
            // API doesn't support output stream, we'll upload the file directly
            CloudStorage cloudStorageDropbox = dataUtils.getAccount(OpenMode.DROPBOX);
            if (mSourceFile.isDropBoxFile()) {
                // we're in the same provider, use api method
                cloudStorageDropbox.copy(CloudUtil.stripPath(OpenMode.DROPBOX, mSourceFile.getPath()), CloudUtil.stripPath(OpenMode.DROPBOX, mTargetFile.getPath()));
                return;
            } else {
                cloudStorageDropbox.upload(CloudUtil.stripPath(OpenMode.DROPBOX, mTargetFile.getPath()), bufferedInputStream, mSourceFile.getSize(), true);
                return;
            }
        } else if (mTargetFile.isBoxFile()) {
            // API doesn't support output stream, we'll upload the file directly
            CloudStorage cloudStorageBox = dataUtils.getAccount(OpenMode.BOX);
            if (mSourceFile.isBoxFile()) {
                // we're in the same provider, use api method
                cloudStorageBox.copy(CloudUtil.stripPath(OpenMode.BOX, mSourceFile.getPath()), CloudUtil.stripPath(OpenMode.BOX, mTargetFile.getPath()));
                return;
            } else {
                cloudStorageBox.upload(CloudUtil.stripPath(OpenMode.BOX, mTargetFile.getPath()), bufferedInputStream, mSourceFile.getSize(), true);
                bufferedInputStream.close();
                return;
            }
        } else if (mTargetFile.isGoogleDriveFile()) {
            // API doesn't support output stream, we'll upload the file directly
            CloudStorage cloudStorageGdrive = dataUtils.getAccount(OpenMode.GDRIVE);
            if (mSourceFile.isGoogleDriveFile()) {
                // we're in the same provider, use api method
                cloudStorageGdrive.copy(CloudUtil.stripPath(OpenMode.GDRIVE, mSourceFile.getPath()), CloudUtil.stripPath(OpenMode.GDRIVE, mTargetFile.getPath()));
                return;
            } else {
                cloudStorageGdrive.upload(CloudUtil.stripPath(OpenMode.GDRIVE, mTargetFile.getPath()), bufferedInputStream, mSourceFile.getSize(), true);
                bufferedInputStream.close();
                return;
            }
        } else if (mTargetFile.isOneDriveFile()) {
            // API doesn't support output stream, we'll upload the file directly
            CloudStorage cloudStorageOnedrive = dataUtils.getAccount(OpenMode.ONEDRIVE);
            if (mSourceFile.isOneDriveFile()) {
                // we're in the same provider, use api method
                cloudStorageOnedrive.copy(CloudUtil.stripPath(OpenMode.ONEDRIVE, mSourceFile.getPath()), CloudUtil.stripPath(OpenMode.ONEDRIVE, mTargetFile.getPath()));
                return;
            } else {
                cloudStorageOnedrive.upload(CloudUtil.stripPath(OpenMode.ONEDRIVE, mTargetFile.getPath()), bufferedInputStream, mSourceFile.getSize(), true);
                bufferedInputStream.close();
                return;
            }
        } else {
            // copying normal file, target not in OTG
            File file = new File(mTargetFile.getPath());
            if (FileUtil.isWritable(file)) {
                if (lowOnMemory) {
                    bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
                } else {
                    outChannel = new RandomAccessFile(file, "rw").getChannel();
                }
            } else {
                ContentResolver contentResolver = mContext.getContentResolver();
                DocumentFile documentTargetFile = FileUtil.getDocumentFile(file, mTargetFile.isDirectory(), mContext);
                bufferedOutputStream = new BufferedOutputStream(contentResolver.openOutputStream(documentTargetFile.getUri()), DEFAULT_BUFFER_SIZE);
            }
        }
        if (bufferedInputStream != null) {
            if (bufferedOutputStream != null)
                copyFile(bufferedInputStream, bufferedOutputStream);
            else if (outChannel != null) {
                copyFile(bufferedInputStream, outChannel);
            }
        } else if (inChannel != null) {
            if (bufferedOutputStream != null)
                copyFile(inChannel, bufferedOutputStream);
            else if (outChannel != null)
                copyFile(inChannel, outChannel);
        }
    } catch (IOException e) {
        e.printStackTrace();
        Log.d(getClass().getSimpleName(), "I/O Error!");
        throw new IOException();
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        // we ran out of memory to map the whole channel, let's switch to streams
        AppConfig.toast(mContext, mContext.getResources().getString(R.string.copy_low_memory));
        startCopy(true);
    } finally {
        try {
            if (inChannel != null)
                inChannel.close();
            if (outChannel != null)
                outChannel.close();
            if (inputStream != null)
                inputStream.close();
            if (outputStream != null)
                outputStream.close();
            if (bufferedInputStream != null)
                bufferedInputStream.close();
            if (bufferedOutputStream != null)
                bufferedOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        // failure in closing stream
        }
    }
}
Also used : CloudStorage(com.cloudrail.si.interfaces.CloudStorage) DocumentFile(android.support.v4.provider.DocumentFile) RandomAccessFile(java.io.RandomAccessFile) BufferedInputStream(java.io.BufferedInputStream) FileChannel(java.nio.channels.FileChannel) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) RandomAccessFile(java.io.RandomAccessFile) HybridFile(com.amaze.filemanager.filesystem.HybridFile) File(java.io.File) DocumentFile(android.support.v4.provider.DocumentFile) FileInputStream(java.io.FileInputStream) ContentResolver(android.content.ContentResolver)

Aggregations

CloudStorage (com.cloudrail.si.interfaces.CloudStorage)13 DocumentFile (android.support.v4.provider.DocumentFile)7 File (java.io.File)7 SmbFile (jcifs.smb.SmbFile)7 ShellNotRunningException (com.amaze.filemanager.exceptions.ShellNotRunningException)6 DataUtils (com.amaze.filemanager.utils.DataUtils)6 SmbException (jcifs.smb.SmbException)6 IOException (java.io.IOException)5 MalformedURLException (java.net.MalformedURLException)4 CloudPluginException (com.amaze.filemanager.exceptions.CloudPluginException)3 HybridFile (com.amaze.filemanager.filesystem.HybridFile)3 HybridFileParcelable (com.amaze.filemanager.filesystem.HybridFileParcelable)3 SFtpClientTemplate (com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate)3 SFTPClient (net.schmizz.sshj.sftp.SFTPClient)3 ContentResolver (android.content.ContentResolver)2 OpenMode (com.amaze.filemanager.utils.OpenMode)2 Box (com.cloudrail.si.services.Box)2 Dropbox (com.cloudrail.si.services.Dropbox)2 GoogleDrive (com.cloudrail.si.services.GoogleDrive)2 OneDrive (com.cloudrail.si.services.OneDrive)2