Search in sources :

Example 1 with FileExistsException

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

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

Aggregations

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