fs.js 4.95 KB
/*
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2015 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 */

/**
 * See {@link https://cordova.apache.org/docs/en/3.3.0/cordova/file/directoryentry/directoryentry.html}
 * @class DirectoryEntry
 */

/** See {@link https://cordova.apache.org/docs/en/3.3.0/cordova/file/fileentry/fileentry.html}
 * @class FileEntry
 */

/**
 * See {@link https://cordova.apache.org/docs/en/3.3.0/cordova/file/fileobj/fileobj.html}
 * @class File
 */

var FILE_PROTOCOL = 'file://';

/**
 * Cordova FileSystem Utilities
 * @module screens/player/shared/fs
 */
define('screens/player/shared/fs', [], function() {
    'use strict';

    return {

        uriToPath: function(uri) {
            return decodeURI(uri.replace(FILE_PROTOCOL, ''));
        },

        pathToUri: function(path) {
            return FILE_PROTOCOL + encodeURI(path);
        },

        /**
         * Resolves the local file system url.
         * @param {string} url URL to the resource
         * @returns {Promise} A promise that resolves to the file or directory entry.
         */
        resolveLocalFileSystemURL: function(url) {
            return new Promise(function(resolve, reject) {
                console.log('resolveLocalFileSystemURL for ', url);
                window.resolveLocalFileSystemURL(url,
                    function(dir) {
                        console.log('resolveLocalFileSystemURL.success', dir.name);
                        resolve(dir);
                    }, function(error) {
                        console.log('resolveLocalFileSystemURL.error', error.code);
                        reject(error);
                    });
            });
        },

        /**
         * Retrieves the file relative to the given directory.
         * @param {DirectoryEntry} directory The Directory entry
         * @param {string} path path to the file
         * @returns {Promise} A promise that resolves to the file entry
         */
        getFileEntry: function(directory, path) {
            return new Promise(function(resolve, reject) {
                console.log('getFileEntry ', directory, path);
                directory.getFile(path, {create: false},
                    function(file) {
                        console.log('getFileEntry.success', file);
                        resolve(file);
                    }, function(error) {
                        console.log('getFileEntry.error', error);
                        reject(error);
                    });
            });
        },

        /**
         * Retrieves the file object that represents the current state of the file that the FileEntry represents.
         * @param {FileEntry} fileEntry The file entry
         * @returns {Promise} A promise that resolves to the file
         */
        getFile: function(fileEntry) {
            return new Promise(function(resolve, reject) {
                return fileEntry.file(resolve, reject);
            });
        },

        /**
         * Reads the given file as test.
         * @param {File} file The file
         * @returns {Promise} A promise that resolves to the text of the file
         */
        readFileAsText: function(file) {
            return new Promise(function(resolve, reject) {
                console.log('fileEntry.file.success ' + file);
                var reader = new FileReader();

                reader.onerror = reader.onabort = reject;
                reader.onload = function() {
                    console.log('readFileEntryAsText.success ' + this.result);
                    resolve(this.result);
                };
                reader.readAsText(file);
            });
        },

        /**
         * Deletes a directory and all of its contents. In the event of an error (such as trying to delete a directory
         * containing a file that can't be removed), some of the contents of the directory may be deleted.
         *
         * @param {DirectoryEntry} dirEntry The directory entry to remove
         * @returns {Promise} A promise that resolves once the directory is removed.
         */
        removeRecursively: function(dirEntry) {
            return new Promise(function(resolve, reject) {
                dirEntry.removeRecursively(function() {
                    console.log('removeRecursively.success', dirEntry);
                    resolve();
                }, function(e) {
                    console.log('removeRecursively.failed', e);
                    reject(e);
                });
            });
        }

    };
});