diff options
Diffstat (limited to 'js/io/system/fileio.js')
-rwxr-xr-x | js/io/system/fileio.js | 350 |
1 files changed, 175 insertions, 175 deletions
diff --git a/js/io/system/fileio.js b/js/io/system/fileio.js index d4de74f9..8fabe317 100755 --- a/js/io/system/fileio.js +++ b/js/io/system/fileio.js | |||
@@ -32,208 +32,208 @@ POSSIBILITY OF SUCH DAMAGE. | |||
32 | //////////////////////////////////////////////////////////////////////// | 32 | //////////////////////////////////////////////////////////////////////// |
33 | NOTES: | 33 | NOTES: |
34 | 34 | ||
35 | For newFile, only the 'uri' is required, if contents is empty, such | 35 | 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 | 36 | 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. | 37 | as the file. 'contentType' is the mime type of the file. |
38 | 38 | ||
39 | Core API reference in NINJA: this.application.ninja.coreIoApi | 39 | Core API reference in NINJA: this.application.ninja.coreIoApi |
40 | 40 | ||
41 | //////////////////////////////////////////////////////////////////////// | 41 | //////////////////////////////////////////////////////////////////////// |
42 | ///////////////////////////////////////////////////////////////////// */ | 42 | ///////////////////////////////////////////////////////////////////// */ |
43 | // | 43 | // |
44 | var Montage = require("montage/core/core").Montage, | 44 | var Montage = require("montage/core/core").Montage, |
45 | Component = require("montage/ui/component").Component; | 45 | Component = require("montage/ui/component").Component; |
46 | //////////////////////////////////////////////////////////////////////// | 46 | //////////////////////////////////////////////////////////////////////// |
47 | //Exporting as File I/O | 47 | //Exporting as File I/O |
48 | exports.FileIo = Montage.create(Component, { | 48 | exports.FileIo = Montage.create(Component, { |
49 | //////////////////////////////////////////////////////////////////// | 49 | //////////////////////////////////////////////////////////////////// |
50 | //Creating new file | 50 | //Creating new file |
51 | newFile: { | 51 | newFile: { |
52 | enumerable: true, | 52 | enumerable: true, |
53 | value: function(file) { | 53 | value: function(file) { |
54 | //Checking for API to be available | 54 | //Checking for API to be available |
55 | if (!this.application.ninja.coreIoApi.cloudAvailable()) { | 55 | if (!this.application.ninja.coreIoApi.cloudAvailable()) { |
56 | //API not available, no IO action taken | 56 | //API not available, no IO action taken |
57 | return null; | 57 | return null; |
58 | } | 58 | } |
59 | //Peforming check for file to exist | 59 | //Peforming check for file to exist |
60 | var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), status, create; | 60 | var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), status, create; |
61 | //Upon successful check, handling results | 61 | //Upon successful check, handling results |
62 | if (check.success) { | 62 | if (check.success) { |
63 | //Handling status of check | 63 | //Handling status of check |
64 | switch (check.status) { | 64 | switch (check.status) { |
65 | case 204: | 65 | case 204: |
66 | //Storing status to be returned (for UI handling) | 66 | //Storing status to be returned (for UI handling) |
67 | status = check.status; | 67 | status = check.status; |
68 | break; | 68 | break; |
69 | case 404: | 69 | case 404: |
70 | //File does not exists, ready to be created | 70 | //File does not exists, ready to be created |
71 | create = this.application.ninja.coreIoApi.createFile(file); | 71 | create = this.application.ninja.coreIoApi.createFile(file); |
72 | status = create.status; | 72 | status = create.status; |
73 | break; | 73 | break; |
74 | default: | 74 | default: |
75 | //Unknown Error | 75 | //Unknown Error |
76 | status = 500; | 76 | status = 500; |
77 | break; | 77 | break; |
78 | } | 78 | } |
79 | } else { | 79 | } else { |
80 | //Unknown Error | 80 | //Unknown Error |
81 | status = 500; | 81 | status = 500; |
82 | } | 82 | } |
83 | //Returning resulting code | 83 | //Returning resulting code |
84 | return status; | 84 | return status; |
85 | // 204: File exists (not created) | 400: File exists | 404: File does not exists | 85 | // 204: File exists (not created) | 400: File exists | 404: File does not exists |
86 | // 201: File succesfully created | 500: Unknown | 86 | // 201: File succesfully created | 500: Unknown |
87 | } | 87 | } |
88 | }, | 88 | }, |
89 | //////////////////////////////////////////////////////////////////// | 89 | //////////////////////////////////////////////////////////////////// |
90 | //Reading contents from file | 90 | //Reading contents from file |
91 | readFile: { | 91 | readFile: { |
92 | enumerable: true, | 92 | enumerable: true, |
93 | value: function(file) { | 93 | value: function(file) { |
94 | //Checking for API to be available | 94 | //Checking for API to be available |
95 | if (!this.application.ninja.coreIoApi.cloudAvailable()) { | 95 | if (!this.application.ninja.coreIoApi.cloudAvailable()) { |
96 | //API not available, no IO action taken | 96 | //API not available, no IO action taken |
97 | return null; | 97 | return null; |
98 | } | 98 | } |
99 | //Peforming check for file to exist | 99 | //Peforming check for file to exist |
100 | var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), status, create, result; | 100 | var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), status, create, result; |
101 | //Upon successful check, handling results | 101 | //Upon successful check, handling results |
102 | if (check.success) { | 102 | if (check.success) { |
103 | //Handling status of check | 103 | //Handling status of check |
104 | switch (check.status) { | 104 | switch (check.status) { |
105 | case 204: | 105 | case 204: |
106 | //File exists | 106 | //File exists |
107 | result = {}; | 107 | result = {}; |
108 | result.content = this.application.ninja.coreIoApi.readFile(file).content; | 108 | result.content = this.application.ninja.coreIoApi.readFile(file).content; |
109 | result.details = this.infoFile(file); | 109 | result.details = this.infoFile(file); |
110 | status = check.status; | 110 | status = check.status; |
111 | break; | 111 | break; |
112 | case 404: | 112 | case 404: |
113 | //File does not exists | 113 | //File does not exists |
114 | status = check.status; | 114 | status = check.status; |
115 | break; | 115 | break; |
116 | default: | 116 | default: |
117 | //Unknown Error | 117 | //Unknown Error |
118 | status = 500; | 118 | status = 500; |
119 | break; | 119 | break; |
120 | } | 120 | } |
121 | } else { | 121 | } else { |
122 | //Unknown Error | 122 | //Unknown Error |
123 | status = 500; | 123 | status = 500; |
124 | } | 124 | } |
125 | //Returning status and result (null if none) | 125 | //Returning status and result (null if none) |
126 | return {status: status, file: result}; | 126 | return {status: status, file: result}; |
127 | //Status Codes | 127 | //Status Codes |
128 | // 204: File exists | 404: File does not exists | 500: Unknown | 128 | // 204: File exists | 404: File does not exists | 500: Unknown |
129 | } | 129 | } |
130 | }, | 130 | }, |
131 | //////////////////////////////////////////////////////////////////// | 131 | //////////////////////////////////////////////////////////////////// |
132 | //Saving file (existing file or creates and saves if none exists) | 132 | //Saving file (existing file or creates and saves if none exists) |
133 | saveFile: { | 133 | saveFile: { |
134 | enumerable: true, | 134 | enumerable: true, |
135 | value: function(file) { | 135 | value: function(file) { |
136 | //Checking for API to be available | 136 | //Checking for API to be available |
137 | if (!this.application.ninja.coreIoApi.cloudAvailable()) { | 137 | if (!this.application.ninja.coreIoApi.cloudAvailable()) { |
138 | //API not available, no IO action taken | 138 | //API not available, no IO action taken |
139 | return null; | 139 | return null; |
140 | } | 140 | } |
141 | //Peforming check for file to exist | 141 | //Peforming check for file to exist |
142 | var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), status, result; | 142 | var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), status, result; |
143 | //Upon successful check, handling results | 143 | //Upon successful check, handling results |
144 | if (check.success) { | 144 | if (check.success) { |
145 | //Handling status of check | 145 | //Handling status of check |
146 | switch (check.status) { | 146 | switch (check.status) { |
147 | case 204: | 147 | case 204: |
148 | //File exists | 148 | //File exists |
149 | result = this.application.ninja.coreIoApi.updateFile(file); | 149 | result = this.application.ninja.coreIoApi.updateFile(file); |
150 | status = 204; | 150 | status = 204; |
151 | break; | 151 | break; |
152 | case 404: | 152 | case 404: |
153 | //File does not exists, ready to be created | 153 | //File does not exists, ready to be created |
154 | result = this.application.ninja.coreIoApi.createFile(file); | 154 | result = this.application.ninja.coreIoApi.createFile(file); |
155 | status = 404; | 155 | status = 404; |
156 | break; | 156 | break; |
157 | default: | 157 | default: |
158 | //Unknown Error | 158 | //Unknown Error |
159 | status = 500; | 159 | status = 500; |
160 | break; | 160 | break; |
161 | } | 161 | } |
162 | } else { | 162 | } else { |
163 | //Unknown Error | 163 | //Unknown Error |
164 | status = 500; | 164 | status = 500; |
165 | } | 165 | } |
166 | //Returning status and result (null if none) | 166 | //Returning status and result (null if none) |
167 | return {status: status, result: result}; | 167 | return {status: status, result: result}; |
168 | //Status Codes | 168 | //Status Codes |
169 | // 204: File exists | 404: File does not exists | 500: Unknown | 169 | // 204: File exists | 404: File does not exists | 500: Unknown |
170 | } | 170 | } |
171 | }, | 171 | }, |
172 | //////////////////////////////////////////////////////////////////// | 172 | //////////////////////////////////////////////////////////////////// |
173 | //TODO: Add functionality |