Search in sources :

Example 1 with EncodingException

use of org.apache.cordova.file.EncodingException 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 EncodingException

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

the class FileUtils method getFile.

/**
     * Creates or looks up a file.
     *
     * @param dirPath base directory
     * @param fileName file/directory to lookup or create
     * @param options specify whether to create or not
     * @param directory if true look up directory, if false look up file
     * @return a Entry object
     * @throws FileExistsException
     * @throws IOException
     * @throws TypeMismatchException
     * @throws EncodingException
     * @throws JSONException
     */
private JSONObject getFile(String dirPath, String fileName, JSONObject options, boolean directory) throws FileExistsException, IOException, TypeMismatchException, EncodingException, JSONException {
    boolean create = false;
    boolean exclusive = false;
    if (options != null) {
        create = options.optBoolean("create");
        if (create) {
            exclusive = options.optBoolean("exclusive");
        }
    }
    // Check for a ":" character in the file to line up with BB and iOS
    if (fileName.contains(":")) {
        throw new EncodingException("This file has a : in it's name");
    }
    File fp = createFileObject(dirPath, fileName);
    if (create) {
        if (exclusive && fp.exists()) {
            throw new FileExistsException("create/exclusive fails");
        }
        if (directory) {
            fp.mkdir();
        } else {
            fp.createNewFile();
        }
        if (!fp.exists()) {
            throw new FileExistsException("create fails");
        }
    } else {
        if (!fp.exists()) {
            throw new FileNotFoundException("path does not exist");
        }
        if (directory) {
            if (fp.isFile()) {
                throw new TypeMismatchException("path doesn't exist or is file");
            }
        } else {
            if (fp.isDirectory()) {
                throw new TypeMismatchException("path doesn't exist or is directory");
            }
        }
    }
    // Return the directory
    return getEntry(fp);
}
Also used : EncodingException(org.apache.cordova.file.EncodingException) TypeMismatchException(org.apache.cordova.file.TypeMismatchException) FileNotFoundException(java.io.FileNotFoundException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileExistsException(org.apache.cordova.file.FileExistsException)

Example 3 with EncodingException

use of org.apache.cordova.file.EncodingException 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

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