aboutsummaryrefslogtreecommitdiffstats
path: root/www/js/files.js
blob: eea38c3dc012fc8a6dff11289a3880b286e68396 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
 * Things to do with Phonegap files
 * Mainly wrappers around their async functions to return Promises
 */

 (function (FMS, Backbone, _, $) {
    _.extend(FMS, {
        files: {
            // move  a file at from a uri to a desination directory. maintains file name
            moveURI: function (srcURI, newName ) {

                var fileEntry;
                return getFileFromURI(srcURI)
                    .pipe( function (file) {
                        fileEntry = file;

                        return getFileSystem();
                    })
                    .pipe( function (filesystem) {
                        FMS.printDebug('Filesystem returned: ' + filesystem);

                        return getDirectory(filesystem.root, CONFIG.FILES_DIR, {create: true});
                    })
                    .pipe( function(directory) {
                        return moveFile( fileEntry, directory, newName );
                    });
            },

            copyURI: function (srcURI, newName ) {

                var fileEntry;
                return getFileFromURI(srcURI)
                    .pipe( function (file) {
                        fileEntry = file;

                        return getFileSystem();
                    })
                    .pipe( function (filesystem) {
                        FMS.printDebug('Filesystem returned: ' + filesystem);

                        return getDirectory(filesystem.root, CONFIG.FILES_DIR, {create: true});
                    })
                    .pipe( function(directory) {
                        return copyFile( fileEntry, directory, newName );
                    });
            },

            deleteURI: function (uri) {
                FMS.printDebug('Deleting URI: ' + uri);

                return getFileFromURI(uri)
                    .pipe( function (file) {
                        var deletingFile = $.Deferred();
                        file.remove(deletingFile.resolve, deletingFile.reject);
                        return deletingFile;
                    });

            },

            // Delete a file from the filesystem
            deleteFile: function (path) {

                FMS.printDebug('Deleting file: ' + path);

                // Turn path into a filename
                var filename = path.split('/').pop();

                return getFileSystem()
                    .pipe(function (filesystem) {
                        return getDirectory(filesystem.root, CONFIG.FILES_DIR);
                    })
                    .pipe(function (directory) {
                        return getFile(directory, filename, {});
                    })
                    .pipe(function (file) {
                        var deletingFile = $.Deferred();
                        file.remove(deletingFile.resolve, deletingFile.reject);
                        return deletingFile;
                    });

            }
        }
    });

    // Wrap the async Phonegap way of getting a filesystem in a promise
    function getFileSystem() {

        FMS.printDebug('Getting the file system');

        var filesystem = $.Deferred();

        window.requestFileSystem(
            LocalFileSystem.PERSISTENT,
            0,
            filesystem.resolve,
            filesystem.reject );

        return filesystem.promise();
    }

    // Wrap the async Phonegap way of getting a directory in a promise
    function getDirectory (rootDirectory, path, options) {

        FMS.printDebug('Getting a directory: ' + path);

        var directory = $.Deferred();

        rootDirectory.getDirectory(path, options, directory.resolve, directory.reject);

        return directory.promise();
    }

    // Wrap the async Phonegap way of getting a file in a promise
    function getFile (directory, path, options) {

        FMS.printDebug('Getting a file with path: ' + path + ' in directory: ' + directory.fullPath);

        var file = $.Deferred();

        directory.getFile(path, options, file.resolve, file.reject);

        return file.promise();
    }

    function moveFile (src, dest, newName) {

        FMS.printDebug( 'moveing file ' + src.fullPath + ' to ' + dest.fullPath );

        var move = $.Deferred();

        var destPath = dest.fullPath + '/' + src.name;
        var srcPath = src.fullPath + '';
        if ( srcPath === destPath ) {
            FMS.printDebug('not moving because files are the same');
            move.resolve( src );
        } else {
            FMS.printDebug('paths differ so moving');
            src.moveTo( dest, newName, move.resolve, move.reject);
        }

        return move.promise();
    }

    function copyFile (src, dest, newName) {

        FMS.printDebug( 'copying file ' + src.fullPath + ' to ' + dest.fullPath );

        var copy = $.Deferred();

        var destPath = dest.fullPath + '/' + src.name;
        var srcPath = src.fullPath + '';
        if ( srcPath === destPath ) {
            FMS.printDebug('not copying because files are the same');
            copy.resolve( src );
        } else {
            FMS.printDebug('paths differ so copying');
            src.copyTo( dest, newName, copy.resolve, copy.reject);
        }

        return copy.promise();
    }

    function getFileFromURI(uri) {

        FMS.printDebug( 'getting file from uri ' + uri );

        var file = $.Deferred();

        window.resolveLocalFileSystemURI( uri, file.resolve, file.reject);

        return file.promise();
    }
})(FMS, Backbone, _, $);