aboutsummaryrefslogtreecommitdiff
path: root/js/io/system/fileio.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/io/system/fileio.js')
-rwxr-xr-xjs/io/system/fileio.js373
1 files changed, 187 insertions, 186 deletions
diff --git a/js/io/system/fileio.js b/js/io/system/fileio.js
index d4de74f9..793beace 100755
--- a/js/io/system/fileio.js
+++ b/js/io/system/fileio.js
@@ -1,24 +1,25 @@
1/* <copyright> 1/* <copyright>
2Copyright (c) 2012, Motorola Mobility, Inc 2Copyright (c) 2012, Motorola Mobility LLC.
3All Rights Reserved. 3All Rights Reserved.
4BSD License.
5 4
6Redistribution and use in source and binary forms, with or without 5Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met: 6modification, are permitted provided that the following conditions are met:
8 7
9 - Redistributions of source code must retain the above copyright notice, 8* Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer. 9 this list of conditions and the following disclaimer.
11 - Redistributions in binary form must reproduce the above copyright 10
12 notice, this list of conditions and the following disclaimer in the 11* Redistributions in binary form must reproduce the above copyright notice,
13 documentation and/or other materials provided with the distribution. 12 this list of conditions and the following disclaimer in the documentation
14 - Neither the name of Motorola Mobility nor the names of its contributors 13 and/or other materials provided with the distribution.
15 may be used to endorse or promote products derived from this software 14
16 without specific prior written permission. 15* Neither the name of Motorola Mobility LLC nor the names of its
16 contributors may be used to endorse or promote products derived from this
17 software without specific prior written permission.
17 18
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
@@ -32,208 +33,208 @@ POSSIBILITY OF SUCH DAMAGE.
32//////////////////////////////////////////////////////////////////////// 33////////////////////////////////////////////////////////////////////////
33NOTES: 34NOTES:
34 35
35 For newFile, only the 'uri' is required, if contents is empty, such 36 For newFile, only the 'uri' is required, if contents is empty, such
36 empty file will be created. 'contents' should be a string to be saved 37 empty file will be created. 'contents' should be a string to be saved
37 as the file. 'contentType' is the mime type of the file. 38 as the file. 'contentType' is the mime type of the file.
38 39
39 Core API reference in NINJA: this.application.ninja.coreIoApi 40 Core API reference in NINJA: this.application.ninja.coreIoApi
40 41
41//////////////////////////////////////////////////////////////////////// 42////////////////////////////////////////////////////////////////////////
42///////////////////////////////////////////////////////////////////// */ 43///////////////////////////////////////////////////////////////////// */
43// 44//
44var Montage = require("montage/core/core").Montage, 45var Montage = require("montage/core/core").Montage,
45 Component = require("montage/ui/component").Component; 46 Component = require("montage/ui/component").Component;
46//////////////////////////////////////////////////////////////////////// 47////////////////////////////////////////////////////////////////////////
47//Exporting as File I/O 48//Exporting as File I/O
48exports.FileIo = Montage.create(Component, { 49exports.FileIo = Montage.create(Component, {
49 //////////////////////////////////////////////////////////////////// 50 ////////////////////////////////////////////////////////////////////
50 //Creating new file 51 //Creating new file
51 newFile: { 52 newFile: {
52 enumerable: true, 53 enumerable: true,
53 value: function(file) { 54 value: function(file) {
54 //Checking for API to be available 55 //Checking for API to be available
55 if (!this.application.ninja.coreIoApi.cloudAvailable()) { 56 if (!this.application.ninja.coreIoApi.cloudAvailable()) {
56 //API not available, no IO action taken 57 //API not available, no IO action taken
57 return null; 58 return null;
58 } 59 }
59 //Peforming check for file to exist 60 //Peforming check for file to exist
60 var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), status, create; 61 var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), status, create;
61 //Upon successful check, handling results 62 //Upon successful check, handling results
62 if (check.success) { 63 if (check.success) {
63 //Handling status of check 64 //Handling status of check
64 switch (check.status) { 65 switch (check.status) {
65 case 204: 66 case 204:
66 //Storing status to be returned (for UI handling) 67 //Storing status to be returned (for UI handling)
67 status = check.status; 68 status = check.status;
68 break; 69 break;
69 case 404: 70 case 404:
70 //File does not exists, ready to be created 71 //File does not exists, ready to be created
71 create = this.application.ninja.coreIoApi.createFile(file); 72 create = this.application.ninja.coreIoApi.createFile(file);
72 status = create.status; 73 status = create.status;
73 break; 74 break;
74 default: 75 default:
75 //Unknown Error 76 //Unknown Error
76 status = 500; 77 status = 500;
77 break; 78 break;
78 } 79 }
79 } else { 80 } else {
80 //Unknown Error 81 //Unknown Error
81 status = 500; 82 status = 500;
82 } 83 }
83 //Returning resulting code 84 //Returning resulting code
84 return status; 85 return status;
85 // 204: File exists (not created) | 400: File exists | 404: File does not exists 86 // 204: File exists (not created) | 400: File exists | 404: File does not exists
86 // 201: File succesfully created | 500: Unknown 87 // 201: File succesfully created | 500: Unknown
87 } 88 }
88 }, 89 },
89 //////////////////////////////////////////////////////////////////// 90 ////////////////////////////////////////////////////////////////////
90 //Reading contents from file 91 //Reading contents from file
91 readFile: { 92 readFile: {
92 enumerable: true, 93 enumerable: true,
93 value: function(file) { 94 value: function(file) {
94 //Checking for API to be available 95 //Checking for API to be available
95 if (!this.application.ninja.coreIoApi.cloudAvailable()) { 96 if (!this.application.ninja.coreIoApi.cloudAvailable()) {
96 //API not available, no IO action taken 97 //API not available, no IO action taken
97 return null; 98 return null;
98 } 99 }
99 //Peforming check for file to exist 100 //Peforming check for file to exist
100 var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), status, create, result; 101 var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), status, create, result;
101 //Upon successful check, handling results 102 //Upon successful check, handling results
102 if (check.success) { 103 if (check.success) {
103 //Handling status of check 104 //Handling status of check
104 switch (check.status) { 105 switch (check.status) {
105 case 204: 106 case 204:
106 //File exists 107 //File exists
107 result = {}; 108 result = {};
108 result.content = this.application.ninja.coreIoApi.readFile(file).content; 109 result.content = this.application.ninja.coreIoApi.readFile(file).content;
109 result.details = this.infoFile(file); 110 result.details = this.infoFile(file);
110 status = check.status; 111 status = check.status;
111 break; 112 break;
112 case 404: 113 case 404:
113 //File does not exists 114 //File does not exists
114 status = check.status; 115 status = check.status;
115 break; 116 break;
116 default: 117 default:
117 //Unknown Error 118 //Unknown Error
118 status = 500; 119 status = 500;
119 break; 120 break;
120 } 121 }
121 } else { 122 } else {
122 //Unknown Error 123 //Unknown Error
123 status = 500; 124 status = 500;
124 } 125 }
125 //Returning status and result (null if none) 126 //Returning status and result (null if none)
126 return {status: status, file: result}; 127 return {status: status, file: result};
127 //Status Codes 128 //Status Codes
128 // 204: File exists | 404: File does not exists | 500: Unknown 129 // 204: File exists | 404: File does not exists | 500: Unknown
129 } 130 }
130 }, 131 },
131 //////////////////////////////////////////////////////////////////// 132 ////////////////////////////////////////////////////////////////////
132 //Saving file (existing file or creates and saves if none exists) 133 //Saving file (existing file or creates and saves if none exists)
133 saveFile: { 134 saveFile: {
134 enumerable: true, 135 enumerable: true,
135 value: function(file) { 136 value: function(file) {
136 //Checking for API to be available 137 //Checking for API to be available
137 if (!this.application.ninja.coreIoApi.cloudAvailable()) { 138 if (!this.application.ninja.coreIoApi.cloudAvailable()) {
138 //API not available, no IO action taken 139 //API not available, no IO action taken
139 return null; 140 return null;
140 } 141 }
141 //Peforming check for file to exist 142 //Peforming check for file to exist
142 var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), status, result; 143 var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), status, result;
143 //Upon successful check, handling results 144 //Upon successful check, handling results
144 if (check.success) { 145 if (check.success) {
145 //Handling status of check 146 //Handling status of check
146 switch (check.status) { 147 switch (check.status) {
147 case 204: 148 case 204:
148 //File exists 149 //File exists
149 result = this.application.ninja.coreIoApi.updateFile(file); 150 result = this.application.ninja.coreIoApi.updateFile(file);
150 status = 204;