Search in sources :

Example 1 with InvalidModificationException

use of org.apache.cordova.file.InvalidModificationException in project cordova-android-chromeview by thedracle.

the class FileUtils method execute.

/**
     * Executes the request and returns whether the action was valid.
     *
     * @param action 		The action to execute.
     * @param args 		JSONArray of arguments for the plugin.
     * @param callbackContext	The callback context used when calling back into JavaScript.
     * @return 			True if the action was valid, false otherwise.
     */
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    try {
        if (action.equals("testSaveLocationExists")) {
            boolean b = DirectoryManager.testSaveLocationExists();
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b));
        } else if (action.equals("getFreeDiskSpace")) {
            long l = DirectoryManager.getFreeDiskSpace(false);
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, l));
        } else if (action.equals("testFileExists")) {
            boolean b = DirectoryManager.testFileExists(args.getString(0));
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b));
        } else if (action.equals("testDirectoryExists")) {
            boolean b = DirectoryManager.testFileExists(args.getString(0));
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b));
        } else if (action.equals("readAsText")) {
            String encoding = args.getString(1);
            int start = args.getInt(2);
            int end = args.getInt(3);
            this.readFileAs(args.getString(0), start, end, callbackContext, encoding, PluginResult.MESSAGE_TYPE_STRING);
        } else if (action.equals("readAsDataURL")) {
            int start = args.getInt(1);
            int end = args.getInt(2);
            this.readFileAs(args.getString(0), start, end, callbackContext, null, -1);
        } else if (action.equals("readAsArrayBuffer")) {
            int start = args.getInt(1);
            int end = args.getInt(2);
            this.readFileAs(args.getString(0), start, end, callbackContext, null, PluginResult.MESSAGE_TYPE_ARRAYBUFFER);
        } else if (action.equals("readAsBinaryString")) {
            int start = args.getInt(1);
            int end = args.getInt(2);
            this.readFileAs(args.getString(0), start, end, callbackContext, null, PluginResult.MESSAGE_TYPE_BINARYSTRING);
        } else if (action.equals("write")) {
            long fileSize = this.write(args.getString(0), args.getString(1), args.getInt(2), args.getBoolean(3));
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, fileSize));
        } else if (action.equals("truncate")) {
            long fileSize = this.truncateFile(args.getString(0), args.getLong(1));
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, fileSize));
        } else if (action.equals("requestFileSystem")) {
            long size = args.optLong(1);
            if (size != 0 && size > (DirectoryManager.getFreeDiskSpace(true) * 1024)) {
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, FileUtils.QUOTA_EXCEEDED_ERR));
            } else {
                JSONObject obj = requestFileSystem(args.getInt(0));
                callbackContext.success(obj);
            }
        } else if (action.equals("resolveLocalFileSystemURI")) {
            JSONObject obj = resolveLocalFileSystemURI(args.getString(0));
            callbackContext.success(obj);
        } else if (action.equals("getMetadata")) {
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, getMetadata(args.getString(0))));
        } else if (action.equals("getFileMetadata")) {
            JSONObject obj = getFileMetadata(args.getString(0));
            callbackContext.success(obj);
        } else if (action.equals("getParent")) {
            JSONObject obj = getParent(args.getString(0));
            callbackContext.success(obj);
        } else if (action.equals("getDirectory")) {
            JSONObject obj = getFile(args.getString(0), args.getString(1), args.optJSONObject(2), true);
            callbackContext.success(obj);
        } else if (action.equals("getFile")) {
            JSONObject obj = getFile(args.getString(0), args.getString(1), args.optJSONObject(2), false);
            callbackContext.success(obj);
        } else if (action.equals("remove")) {
            boolean success;
            success = remove(args.getString(0));
            if (success) {
                notifyDelete(args.getString(0));
                callbackContext.success();
            } else {
                callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR);
            }
        } else if (action.equals("removeRecursively")) {
            boolean success = removeRecursively(args.getString(0));
            if (success) {
                callbackContext.success();
            } else {
                callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR);
            }
        } else if (action.equals("moveTo")) {
            JSONObject entry = transferTo(args.getString(0), args.getString(1), args.getString(2), true);
            callbackContext.success(entry);
        } else if (action.equals("copyTo")) {
            JSONObject entry = transferTo(args.getString(0), args.getString(1), args.getString(2), false);
            callbackContext.success(entry);
        } else if (action.equals("readEntries")) {
            JSONArray entries = readEntries(args.getString(0));
            callbackContext.success(entries);
        } else {
            return false;
        }
    } catch (FileNotFoundException e) {
        callbackContext.error(FileUtils.NOT_FOUND_ERR);
    } catch (FileExistsException e) {
        callbackContext.error(FileUtils.PATH_EXISTS_ERR);
    } catch (NoModificationAllowedException e) {
        callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR);
    } catch (InvalidModificationException e) {
        callbackContext.error(FileUtils.INVALID_MODIFICATION_ERR);
    } catch (MalformedURLException e) {
        callbackContext.error(FileUtils.ENCODING_ERR);
    } catch (IOException e) {
        callbackContext.error(FileUtils.INVALID_MODIFICATION_ERR);
    } catch (EncodingException e) {
        callbackContext.error(FileUtils.ENCODING_ERR);
    } catch (TypeMismatchException e) {
        callbackContext.error(FileUtils.TYPE_MISMATCH_ERR);
    }
    return true;
}
Also used : MalformedURLException(java.net.MalformedURLException) PluginResult(org.apache.cordova.api.PluginResult) EncodingException(org.apache.cordova.file.EncodingException) TypeMismatchException(org.apache.cordova.file.TypeMismatchException) JSONArray(org.json.JSONArray) FileNotFoundException(java.io.FileNotFoundException) InvalidModificationException(org.apache.cordova.file.InvalidModificationException) NoModificationAllowedException(org.apache.cordova.file.NoModificationAllowedException) IOException(java.io.IOException) JSONObject(org.json.JSONObject) FileExistsException(org.apache.cordova.file.FileExistsException)

Example 2 with InvalidModificationException

use of org.apache.cordova.file.InvalidModificationException in project cordova-android-chromeview by thedracle.

the class FileUtils method copyDirectory.

/**
     * Copy a directory
     *
     * @param srcDir directory to be copied
     * @param destinationDir destination to be copied to
     * @return a DirectoryEntry object
     * @throws JSONException
     * @throws IOException
     * @throws NoModificationAllowedException
     * @throws InvalidModificationException
     */
private JSONObject copyDirectory(File srcDir, File destinationDir) throws JSONException, IOException, NoModificationAllowedException, InvalidModificationException {
    // Renaming a file to an existing directory should fail
    if (destinationDir.exists() && destinationDir.isFile()) {
        throw new InvalidModificationException("Can't rename a file to a directory");
    }
    // Check to make sure we are not copying the directory into itself
    if (isCopyOnItself(srcDir.getAbsolutePath(), destinationDir.getAbsolutePath())) {
        throw new InvalidModificationException("Can't copy itself into itself");
    }
    // See if the destination directory exists. If not create it.
    if (!destinationDir.exists()) {
        if (!destinationDir.mkdir()) {
            // If we can't create the directory then fail
            throw new NoModificationAllowedException("Couldn't create the destination directory");
        }
    }
    for (File file : srcDir.listFiles()) {
        if (file.isDirectory()) {
            copyDirectory(file, destinationDir);
        } else {
            File destination = new File(destinationDir.getAbsoluteFile() + File.separator + file.getName());
            copyFile(file, destination);
        }
    }
    return getEntry(destinationDir);
}
Also used : InvalidModificationException(org.apache.cordova.file.InvalidModificationException) NoModificationAllowedException(org.apache.cordova.file.NoModificationAllowedException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 3 with InvalidModificationException

use of org.apache.cordova.file.InvalidModificationException in project cordova-android-chromeview by thedracle.

the class FileUtils method transferTo.

/**
     * A setup method that handles the move/copy of files/directories
     *
     * @param fileName to be copied/moved
     * @param newParent is the location where the file will be copied/moved to
     * @param newName for the file directory to be called, if null use existing file name
     * @param move if false do a copy, if true do a move
     * @return a Entry object
     * @throws NoModificationAllowedException
     * @throws IOException
     * @throws InvalidModificationException
     * @throws EncodingException
     * @throws JSONException
     * @throws FileExistsException
     */
private JSONObject transferTo(String fileName, String newParent, String newName, boolean move) throws JSONException, NoModificationAllowedException, IOException, InvalidModificationException, EncodingException, FileExistsException {
    String newFileName = FileHelper.getRealPath(fileName, cordova);
    newParent = FileHelper.getRealPath(newParent, cordova);
    // Check for invalid file name
    if (newName != null && newName.contains(":")) {
        throw new EncodingException("Bad file name");
    }
    File source = new File(newFileName);
    if (!source.exists()) {
        // The file/directory we are copying doesn't exist so we should fail.
        throw new FileNotFoundException("The source does not exist");
    }
    File destinationDir = new File(newParent);
    if (!destinationDir.exists()) {
        // The destination does not exist so we should fail.
        throw new FileNotFoundException("The source does not exist");
    }
    // Figure out where we should be copying to
    File destination = createDestination(newName, source, destinationDir);
    // Check to see if source and destination are the same file
    if (source.getAbsolutePath().equals(destination.getAbsolutePath())) {
        throw new InvalidModificationException("Can't copy a file onto itself");
    }
    if (source.isDirectory()) {
        if (move) {
            return moveDirectory(source, destination);
        } else {
            return copyDirectory(source, destination);
        }
    } else {
        if (move) {
            JSONObject newFileEntry = moveFile(source, destination);
            // If we've moved a file given its content URI, we need to clean up.
            if (fileName.startsWith("content://")) {
                notifyDelete(fileName);
            }
            return newFileEntry;
        } else {
            return copyFile(source, destination);
        }
    }
}
Also used : JSONObject(org.json.JSONObject) EncodingException(org.apache.cordova.file.EncodingException) FileNotFoundException(java.io.FileNotFoundException) InvalidModificationException(org.apache.cordova.file.InvalidModificationException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

InvalidModificationException (org.apache.cordova.file.InvalidModificationException)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 RandomAccessFile (java.io.RandomAccessFile)2 EncodingException (org.apache.cordova.file.EncodingException)2 NoModificationAllowedException (org.apache.cordova.file.NoModificationAllowedException)2 JSONObject (org.json.JSONObject)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 PluginResult (org.apache.cordova.api.PluginResult)1 FileExistsException (org.apache.cordova.file.FileExistsException)1 TypeMismatchException (org.apache.cordova.file.TypeMismatchException)1 JSONArray (org.json.JSONArray)1