fs.js
4.95 KB
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
/*
* 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);
});
});
}
};
});