-rw-r--r-- | content/flickr.js | 12 |
1 files changed, 3 insertions, 9 deletions
diff --git a/content/flickr.js b/content/flickr.js index add628a..b5bfa43 100644 --- a/content/flickr.js +++ b/content/flickr.js | |||
@@ -1,353 +1,347 @@ | |||
1 | /* | 1 | /* |
2 | * Photoset | 2 | * Photoset |
3 | */ | 3 | */ |
4 | 4 | ||
5 | function Photoset(s) { | 5 | function Photoset(s) { |
6 | if(s instanceof Photoset) { | 6 | if(s instanceof Photoset) { |
7 | for(var p in s) this[p]=s[p]; | 7 | for(var p in s) this[p]=s[p]; |
8 | }else | 8 | }else |
9 | this.fromNode(s); | 9 | this.fromNode(s); |
10 | } | 10 | } |
11 | Photoset.prototype = { | 11 | Photoset.prototype = { |
12 | id: null, | 12 | id: null, |
13 | primary: null, | 13 | primary: null, |
14 | secret: null, | 14 | secret: null, |
15 | server: null, | 15 | server: null, |
16 | photos: null, | 16 | photos: null, |
17 | title: null, | 17 | title: null, |
18 | description: null, | 18 | description: null, |
19 | fromNode: function(n) { | 19 | fromNode: function(n) { |
20 | this.id = n.getAttribute('id'); | 20 | this.id = n.getAttribute('id'); |
21 | this.primary = n.getAttribute('primary'); | 21 | this.primary = n.getAttribute('primary'); |
22 | this.secret = n.getAttribute('secret'); | 22 | this.secret = n.getAttribute('secret'); |
23 | this.server = n.getAttribute('server'); | 23 | this.server = n.getAttribute('server'); |
24 | this.photos = n.getAttribute('photos'); | 24 | this.photos = n.getAttribute('photos'); |
25 | this.title = n.getElementsByTagName('title').item(0).firstChild.nodeValue; | 25 | this.title = n.getElementsByTagName('title').item(0).firstChild.nodeValue; |
26 | this.description = n.getElementsByTagName('description').item(0).firstChild; | 26 | this.description = n.getElementsByTagName('description').item(0).firstChild; |
27 | if(this.description) this.description = this.description.nodeValue; | 27 | if(this.description) this.description = this.description.nodeValue; |
28 | } | 28 | } |
29 | }; | 29 | }; |
30 | 30 | ||
31 | /* | 31 | /* |
32 | * Photo | 32 | * Photo |
33 | */ | 33 | */ |
34 | function Photo(s) { | 34 | function Photo(s) { |
35 | if(s instanceof Photo) { | 35 | if(s instanceof Photo) { |
36 | for(var p in s) this[p]=s[p]; | 36 | for(var p in s) this[p]=s[p]; |
37 | }else | 37 | }else |
38 | this.fromNode(s); | 38 | this.fromNode(s); |
39 | } | 39 | } |
40 | Photo.prototype = { | 40 | Photo.prototype = { |
41 | id: null, secret: null, | 41 | id: null, secret: null, |
42 | server: null, | 42 | server: null, |
43 | title: null, | 43 | title: null, |
44 | isprimary: null, | 44 | isprimary: null, |
45 | license: null, | 45 | license: null, |
46 | dateupload: null, datetaken: null, datetakengranularity: null, | 46 | dateupload: null, datetaken: null, datetakengranularity: null, |
47 | ownername: null, | 47 | ownername: null, |
48 | iconserver: null, | 48 | iconserver: null, |
49 | originalformat: null, | 49 | originalformat: null, |
50 | lastupdate: null, | 50 | lastupdate: null, |
51 | fromNode: function(n) { | 51 | fromNode: function(n) { |
52 | this.id = n.getAttribute('id'); this.secret = n.getAttribute('secret'); | 52 | this.id = n.getAttribute('id'); this.secret = n.getAttribute('secret'); |
53 | this.server = n.getAttribute('server'); | 53 | this.server = n.getAttribute('server'); |
54 | this.title = n.getAttribute('title'); | 54 | this.title = n.getAttribute('title'); |
55 | this.isprimary = n.getAttribute('isprimary'); | 55 | this.isprimary = n.getAttribute('isprimary'); |
56 | this.license = n.getAttribute('license'); | 56 | this.license = n.getAttribute('license'); |
57 | this.dateupload = n.getAttribute('dateupload'); | 57 | this.dateupload = n.getAttribute('dateupload'); |
58 | this.datetaken = n.getAttribute('datetaken'); this.datetakengranularity = n.getAttribute('datetakengranularity'); | 58 | this.datetaken = n.getAttribute('datetaken'); this.datetakengranularity = n.getAttribute('datetakengranularity'); |
59 | this.ownername = n.getAttribute('ownername'); | 59 | this.ownername = n.getAttribute('ownername'); |
60 | this.iconserver = n.getAttribute('iconserver'); | 60 | this.iconserver = n.getAttribute('iconserver'); |
61 | this.originalformat = n.getAttribute('originalformat'); | 61 | this.originalformat = n.getAttribute('originalformat'); |
62 | this.lastupdate = n.getAttribute('lastupdate'); | 62 | this.lastupdate = n.getAttribute('lastupdate'); |
63 | }, | 63 | }, |
64 | fromNode_: function(n) { | 64 | fromNode_: function(n) { |
65 | var t; | 65 | var t; |
66 | // TODO: @rotation @isfavorite | 66 | // TODO: @rotation @isfavorite |
67 | this.owner = {}; | 67 | this.owner = {}; |
68 | t = n.getElementsByTagName('owner').item(0); | 68 | t = n.getElementsByTagName('owner').item(0); |
69 | if(t) { | 69 | if(t) { |
70 | this.owner.nsid=t.getAttribute('nsid'); | 70 | this.owner.nsid=t.getAttribute('nsid'); |
71 | this.owner.username=t.getAttribute('username'); | 71 | this.owner.username=t.getAttribute('username'); |
72 | this.owner.realname=t.getAttribute('realname'); | 72 | this.owner.realname=t.getAttribute('realname'); |
73 | this.owner.location=t.getAttribute.location; | 73 | this.owner.location=t.getAttribute.location; |
74 | } | 74 | } |
75 | t = n.getElementsByTagName('description').item(0); | 75 | t = n.getElementsByTagName('description').item(0); |
76 | if(t && t.firstChild) { | 76 | if(t && t.firstChild) { |
77 | this.description = t.firstChild.nodeValue; | 77 | this.description = t.firstChild.nodeValue; |
78 | } | 78 | } |
79 | // TODO: visibility/@ispublic visibility/@isfriend visibility/@isfamily | 79 | // TODO: visibility/@ispublic visibility/@isfriend visibility/@isfamily |
80 | // TODO: dates/@posted dates/@taken dates/@takengranularity dates/@lastupdate | 80 | // TODO: dates/@posted dates/@taken dates/@takengranularity dates/@lastupdate |
81 | // TODO: permissions/@permcomment permsiions/@permaddmeta | 81 | // TODO: permissions/@permcomment permsiions/@permaddmeta |
82 | // TODO: editability/@canaddcomment editability/@canaddmeta | 82 | // TODO: editability/@canaddcomment editability/@canaddmeta |
83 | // TODO: comments | 83 | // TODO: comments |
84 | // TODO: notes/note/@id notes/note/@author notes/note/@authorname | 84 | // TODO: notes/note/@id notes/note/@author notes/note/@authorname |
85 | // TODO: notes/note/@x notes/note/@y notes/note/@w notes/note/@h | 85 | // TODO: notes/note/@x notes/note/@y notes/note/@w notes/note/@h |
86 | // TODO: notes/note | 86 | // TODO: notes/note |
87 | // TODO: tags/tag/@id tags/tag/@author tags/tag/@raw tags/tag | 87 | // TODO: tags/tag/@id tags/tag/@author tags/tag/@raw tags/tag |
88 | // TODO: urls/url/@type urls/url | 88 | // TODO: urls/url/@type urls/url |
89 | } | 89 | } |
90 | }; | 90 | }; |
91 | 91 | ||
92 | function Flickr() { } | 92 | function Flickr() { } |
93 | Flickr.prototype = { | 93 | Flickr.prototype = { |
94 | 94 | ||
95 | rest_url: 'http://www.flickr.com/services/rest/', | 95 | rest_url: 'http://www.flickr.com/services/rest/', |
96 | auth_url: 'http://flickr.com/services/auth/', | 96 | auth_url: 'http://flickr.com/services/auth/', |
97 | photo_url: 'http://static.flickr.com/', | 97 | photo_url: 'http://static.flickr.com/', |
98 | photos_url: 'http://www.flickr.com/photos/', | 98 | photos_url: 'http://www.flickr.com/photos/', |
99 | upload_url: 'http://www.flickr.com/services/upload/', | 99 | upload_url: 'http://www.flickr.com/services/upload/', |
100 | 100 | ||
101 | api_sig: function(paramstr) { | 101 | api_sig: function(paramstr) { |
102 | return MD5(toutf8(this.api_shs+paramstr)); | 102 | return MD5(toutf8(this.api_shs+paramstr)); |
103 | }, | 103 | }, |
104 | api_call_url: function(params,url) { | 104 | api_call_url: function(params,url) { |
105 | params.api_key = this.api_key; | 105 | params.api_key = this.api_key; |
106 | var pp = new Array(); | 106 | var pp = new Array(); |
107 | for(var p in params) { | 107 | for(var p in params) { |
108 | pp.push(p); | 108 | pp.push(p); |
109 | } | 109 | } |
110 | var pstr = ''; | 110 | var pstr = ''; |
111 | var rv = (url?url:this.rest_url)+'?'; | 111 | var rv = (url?url:this.rest_url)+'?'; |
112 | for(var p in pp.sort()) { | 112 | for(var p in pp.sort()) { |
113 | var pn = pp[p]; | 113 | var pn = pp[p]; |
114 | pstr += pn+params[pn]; | 114 | pstr += pn+params[pn]; |
115 | rv += pn+'='+params[pn]+'&'; | 115 | rv += pn+'='+params[pn]+'&'; |
116 | } | 116 | } |
117 | rv += 'api_sig='+this.api_sig(pstr); | 117 | rv += 'api_sig='+this.api_sig(pstr); |
118 | return rv; | 118 | return rv; |
119 | }, | 119 | }, |
120 | api_call: function(params, on_success, on_failure) { | 120 | api_call: function(params, on_success, on_failure) { |
121 | if(params.auth_token == 'default') | 121 | if(params.auth_token == 'default') |
122 | params.auth_token = this.token; | 122 | params.auth_token = this.token; |
123 | var x = new XMLHttpRequest(); | 123 | var x = new XMLHttpRequest(); |
124 | x.open("GET",this.api_call_url(params)); | 124 | x.open("GET",this.api_call_url(params)); |
125 | x.onreadystatechange=function() { | 125 | x.onreadystatechange=function() { |
126 | if(x.readyState!=4) return false; | 126 | if(x.readyState!=4) return false; |
127 | if(x.status==200) { | 127 | if(x.status==200) { |
128 | var stat = x.responseXML.firstChild.getAttribute('stat'); | 128 | var stat = x.responseXML.firstChild.getAttribute('stat'); |
129 | if(stat=='ok') { | 129 | if(stat=='ok') { |
130 | if(on_success) on_success(x); | 130 | if(on_success) on_success(x); |
131 | }else{ | 131 | }else{ |
132 | var e = x.responseXML.getElementsByTagName('err').item(0); | 132 | var e = x.responseXML.getElementsByTagName('err').item(0); |
133 | var ecode = e.getAttribute('code'); | 133 | var ecode = e.getAttribute('code'); |
134 | var emsg = e.getAttribute('msg'); | 134 | var emsg = e.getAttribute('msg'); |
135 | dump(params.method+' failed: '+ecode+' '+emsg+'\n'); | 135 | dump(params.method+' failed: '+ecode+' '+emsg+'\n'); |
136 | if(on_failure) on_failure(x,stat,ecode,emsg); | 136 | if(on_failure) on_failure(x,stat,ecode,emsg); |
137 | } | 137 | } |
138 | }else{ | 138 | }else{ |
139 | if(on_failure) on_failure(x); | 139 | if(on_failure) on_failure(x); |
140 | } | 140 | } |
141 | return true; | 141 | return true; |
142 | } | 142 | } |
143 | x.send(null); | 143 | x.send(null); |
144 | return true; | 144 | return true; |
145 | }, | 145 | }, |
146 | 146 | ||
147 | frob: null, | 147 | frob: null, |
148 | authorize_0: function(on_s, on_f) { | 148 | authorize_0: function(perms, on_s, on_f) { |
149 | var _this = this; | 149 | var _this = this; |
150 | this.api_call( | 150 | this.api_call( |
151 | { method: 'flickr.auth.getFrob' }, | 151 | { method: 'flickr.auth.getFrob' }, |
152 | function(x) { | 152 | function(x) { |
153 | _this.frob = xp_str('/rsp/frob',x.responseXML); | 153 | _this.frob = xp_str('/rsp/frob',x.responseXML); |
154 | var u = _this.api_call_url( | 154 | var u = _this.api_call_url( |
155 | { frob: _this.frob, perms: 'delete' }, _this.auth_url ); | 155 | { frob: _this.frob, perms: perms?perms:'delete' }, _this.auth_url ); |
156 | var wm = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService( | 156 | if(on_s) on_s(x,_this.frob,u); |
157 | Components.interfaces.nsIWindowMediator ); | ||
158 | var bw = wm.getMostRecentWindow('navigator:browser'); | ||
159 | var b = bw.getBrowser(); | ||
160 | var t = b.addTab(u); | ||
161 | b.selectedTab = t; | ||
162 | if(on_s) on_s(); | ||
163 | }, function(x,s,c,m) { | 157 | }, function(x,s,c,m) { |
164 | if(on_f) on_f(x,s,c,m); | 158 | if(on_f) on_f(x,s,c,m); |
165 | } | 159 | } |
166 | ); | 160 | ); |
167 | }, | 161 | }, |
168 | token: null, | 162 | token: null, |
169 | perms: null, | 163 | perms: null, |
170 | user: null, | 164 | user: null, |
171 | authorize_1: function(on_s, on_f) { | 165 | authorize_1: function(on_s, on_f) { |
172 | var _this = this; | 166 | var _this = this; |
173 | this.api_call( | 167 | this.api_call( |
174 | { method: 'flickr.auth.getToken', frob: this.frob }, | 168 | { method: 'flickr.auth.getToken', frob: this.frob }, |
175 | function(x) { | 169 | function(x) { |
176 | _this.token = xp_str('/rsp/auth/token',x.responseXML); | 170 | _this.token = xp_str('/rsp/auth/token',x.responseXML); |
177 | _this.perms = xp_str('/rsp/auth/perms',x.responseXML); | 171 | _this.perms = xp_str('/rsp/auth/perms',x.responseXML); |
178 | var u = xp_node('/rsp/auth/user',x.responseXML); | 172 | var u = xp_node('/rsp/auth/user',x.responseXML); |
179 | _this.user = { | 173 | _this.user = { |
180 | nsid: u.getAttribute('nsid'), | 174 | nsid: u.getAttribute('nsid'), |
181 | username: u.getAttribute('username'), | 175 | username: u.getAttribute('username'), |
182 | fullname: u.getAttribute('fullname') | 176 | fullname: u.getAttribute('fullname') |
183 | }; | 177 | }; |
184 | if(on_s) on_s(x); | 178 | if(on_s) on_s(x); |
185 | }, function(x,s,c,m) { | 179 | }, function(x,s,c,m) { |
186 | if(on_f) on_f(x,s,c,m); | 180 | if(on_f) on_f(x,s,c,m); |
187 | } | 181 | } |
188 | ); | 182 | ); |
189 | }, | 183 | }, |
190 | 184 | ||
191 | prefs: Components.classes['@mozilla.org/preferences-service;1'].getService( | 185 | prefs: Components.classes['@mozilla.org/preferences-service;1'].getService( |
192 | Components.interfaces.nsIPrefBranch | 186 | Components.interfaces.nsIPrefBranch |
193 | ), | 187 | ), |
194 | prefs_root: 'net.klever.kin.flickr', | 188 | prefs_root: 'net.klever.kin.flickr', |
195 | save_token: function() { | 189 | save_token: function() { |
196 | // TODO: don't clear when there's nothing to clear or catch exceptions | 190 | // TODO: don't clear when there's nothing to clear or catch exceptions |
197 | if(this.token) | 191 | if(this.token) |
198 | this.prefs.setCharPref(this.prefs_root+'.auth_token',this.token); | 192 | this.prefs.setCharPref(this.prefs_root+'.auth_token',this.token); |
199 | else | 193 | else |
200 | this.prefs.clearUserPref(this.prefs_root+'.auth_token'); | 194 | this.prefs.clearUserPref(this.prefs_root+'.auth_token'); |
201 | if(this.perms) | 195 | if(this.perms) |
202 | this.prefs.setCharPref(this.prefs_root+'.auth_perms',this.perms); | 196 | this.prefs.setCharPref(this.prefs_root+'.auth_perms',this.perms); |
203 | else | 197 | else |
204 | this.prefs.clearUserPref(this.prefs_root+'.auth_perms'); | 198 | this.prefs.clearUserPref(this.prefs_root+'.auth_perms'); |
205 | if(this.user && this.user.nsid!=null && this.user.nsid!=undefined) | 199 | if(this.user && this.user.nsid!=null && this.user.nsid!=undefined) |
206 | this.prefs.setCharPref(this.prefs_root+'.auth_user.nsid',this.user.nsid); | 200 | this.prefs.setCharPref(this.prefs_root+'.auth_user.nsid',this.user.nsid); |
207 | else | 201 | else |
208 | this.prefs.clearUserPref(this.prefs_root+'.auth_user.nsid'); | 202 | this.prefs.clearUserPref(this.prefs_root+'.auth_user.nsid'); |
209 | if(this.user && this.user.username!=null && this.user.username!=undefined) | 203 | if(this.user && this.user.username!=null && this.user.username!=undefined) |
210 | this.prefs.setCharPref(this.prefs_root+'.auth_user.username',this.user.username); | 204 | this.prefs.setCharPref(this.prefs_root+'.auth_user.username',this.user.username); |
211 | else | 205 | else |
212 | this.prefs.clearUserPref(this.prefs_root+'.auth_user.username'); | 206 | this.prefs.clearUserPref(this.prefs_root+'.auth_user.username'); |
213 | if(this.user && this.user.fullname!=null && this.user.fullname!=undefined) | 207 | if(this.user && this.user.fullname!=null && this.user.fullname!=undefined) |
214 | this.prefs.setCharPref(this.prefs_root+'.auth_user.fullname',this.user.fullname); | 208 | this.prefs.setCharPref(this.prefs_root+'.auth_user.fullname',this.user.fullname); |
215 | else | 209 | else |
216 | this.prefs.clearUserPref(this.prefs_root+'.auth_user.fullname'); | 210 | this.prefs.clearUserPref(this.prefs_root+'.auth_user.fullname'); |
217 | }, | 211 | }, |
218 | _reset_token: function() { | 212 | _reset_token: function() { |
219 | this.token = null; this.perms = null; this.user = null; | 213 | this.token = null; this.perms = null; this.user = null; |
220 | return false; | 214 | return false; |
221 | }, | 215 | }, |
222 | load_token: function() { | 216 | load_token: function() { |
223 | try { | 217 | try { |
224 | if(this.prefs.getPrefType(this.prefs_root+'.auth_token')!=this.prefs.PREF_STRING) | 218 | if(this.prefs.getPrefType(this.prefs_root+'.auth_token')!=this.prefs.PREF_STRING) |
225 | return this._reset_token(); | 219 | return this._reset_token(); |
226 | this.token = this.prefs.getCharPref(this.prefs_root+'.auth_token'); | 220 | this.token = this.prefs.getCharPref(this.prefs_root+'.auth_token'); |
227 | if(this.prefs.getPrefType(this.prefs_root+'.auth_perms')!=this.prefs.PREF_STRING) | 221 | if(this.prefs.getPrefType(this.prefs_root+'.auth_perms')!=this.prefs.PREF_STRING) |
228 | return this._reset_token(); | 222 | return this._reset_token(); |
229 | this.perms = this.prefs.getCharPref(this.prefs_root+'.auth_perms'); | 223 | this.perms = this.prefs.getCharPref(this.prefs_root+'.auth_perms'); |
230 | if(this.prefs.getPrefType(this.prefs_root+'.auth_user.nsid')!=this.prefs.PREF_STRING) | 224 | if(this.prefs.getPrefType(this.prefs_root+'.auth_user.nsid')!=this.prefs.PREF_STRING) |
231 | return this._reset_token(); | 225 | return this._reset_token(); |
232 | this.user = new Object(); | 226 | this.user = new Object(); |
233 | this.user.nsid = this.prefs.getCharPref(this.prefs_root+'.auth_user.nsid'); | 227 | this.user.nsid = this.prefs.getCharPref(this.prefs_root+'.auth_user.nsid'); |
234 | if(this.prefs.getPrefType(this.prefs_root+'.auth_user.username')!=this.prefs.PREF_STRING) | 228 | if(this.prefs.getPrefType(this.prefs_root+'.auth_user.username')!=this.prefs.PREF_STRING) |
235 | return this._reset_token(); | 229 | return this._reset_token(); |
236 | this.user.username = this.prefs.getCharPref(this.prefs_root+'.auth_user.username'); | 230 | this.user.username = this.prefs.getCharPref(this.prefs_root+'.auth_user.username'); |
237 | if(this.prefs.getPrefType(this.prefs_root+'.auth_user.fullname')!=this.prefs.PREF_STRING) | 231 | if(this.prefs.getPrefType(this.prefs_root+'.auth_user.fullname')!=this.prefs.PREF_STRING) |
238 | return this._reset_token(); | 232 | return this._reset_token(); |
239 | this.user.fullname = this.prefs.getCharPref(this.prefs_root+'.auth_user.fullname'); | 233 | this.user.fullname = this.prefs.getCharPref(this.prefs_root+'.auth_user.fullname'); |
240 | }catch(e) { return this._reset_token(); } | 234 | }catch(e) { return this._reset_token(); } |
241 | return true; | 235 | return true; |
242 | }, | 236 | }, |
243 | reset_token: function() { | 237 | reset_token: function() { |
244 | this._reset_token(); | 238 | this._reset_token(); |
245 | this.save_token(); | 239 | this.save_token(); |
246 | }, | 240 | }, |
247 | 241 | ||
248 | get_photo_url: function(ser,id,sec,sfx,ext) { | 242 | get_photo_url: function(ser,id,sec,sfx,ext) { |
249 | var rv = this.photo_url + ser + '/' + id + '_' + sec; | 243 | var rv = this.photo_url + ser + '/' + id + '_' + sec; |
250 | if(sfx && sfx!='_') rv += '_'+sfx; | 244 | if(sfx && sfx!='_') rv += '_'+sfx; |
251 | rv += ext?'.'+ext:'.jpg'; | 245 | rv += ext?'.'+ext:'.jpg'; |
252 | return rv; | 246 | return rv; |
253 | }, | 247 | }, |
254 | get_image_url: function(o,sfx) { | 248 | get_image_url: function(o,sfx) { |
255 | return this.get_photo_url( | 249 | return this.get_photo_url( |
256 | o.server, | 250 | o.server, |
257 | (o instanceof Photoset)? o.primary : o.id, | 251 | (o instanceof Photoset)? o.primary : o.id, |
258 | o.secret, | 252 | o.secret, |
259 | sfx, | 253 | sfx, |
260 | (sfx=='o')?o.originalformat:null | 254 | (sfx=='o')?o.originalformat:null |
261 | ); | 255 | ); |
262 | }, | 256 | }, |
263 | get_photo_page_url: function(p) { | 257 | get_photo_page_url: function(p) { |
264 | if(p instanceof Photo) // TODO: half wrong, what if no owner? | 258 | if(p instanceof Photo) // TODO: half wrong, what if no owner? |
265 | return this.photos_url + (p.owner.nsid?p.owner.nsid:this.user.nsid) + '/' + p.id; | 259 | return this.photos_url + (p.owner.nsid?p.owner.nsid:this.user.nsid) + '/' + p.id; |
266 | else // TODO: take owner into account? | 260 | else // TODO: take owner into account? |
267 | return this.photos_url + this.user.nsid + '/' + p; | 261 | return this.photos_url + this.user.nsid + '/' + p; |
268 | }, | 262 | }, |
269 | make_photo_url: function(p,sfx) { | 263 | make_photo_url: function(p,sfx) { |
270 | if(sfx=='p') | 264 | if(sfx=='p') |
271 | return this.get_photo_page_url(p); | 265 | return this.get_photo_page_url(p); |
272 | else | 266 | else |
273 | return this.get_image_url(p,sfx); | 267 | return this.get_image_url(p,sfx); |
274 | }, | 268 | }, |
275 | 269 | ||
276 | upload_file: function(f,fa,on_success,on_failure) { | 270 | upload_file: function(f,fa,on_success,on_failure) { |
277 | try { | 271 | try { |
278 | var fi = Components.classes["@mozilla.org/file/local;1"] | 272 | var fi = Components.classes["@mozilla.org/file/local;1"] |
279 | .createInstance(Components.interfaces.nsILocalFile); | 273 | .createInstance(Components.interfaces.nsILocalFile); |
280 | fi.initWithPath( f ); | 274 | fi.initWithPath( f ); |
281 | var st = Components.classes["@mozilla.org/network/file-input-stream;1"] | 275 | var st = Components.classes["@mozilla.org/network/file-input-stream;1"] |
282 | .createInstance(Components.interfaces.nsIFileInputStream); | 276 | .createInstance(Components.interfaces.nsIFileInputStream); |
283 | st.init(fi,0x01,00004,null); | 277 | st.init(fi,0x01,00004,null); |
284 | var bis = Components.classes["@mozilla.org/binaryinputstream;1"] | 278 | var bis = Components.classes["@mozilla.org/binaryinputstream;1"] |
285 | .createInstance(Components.interfaces.nsIBinaryInputStream); | 279 | .createInstance(Components.interfaces.nsIBinaryInputStream); |
286 | bis.setInputStream(st); | 280 | bis.setInputStream(st); |
287 | 281 | ||
288 | // allocate and initialize temp storage string | 282 | // allocate and initialize temp storage string |
289 | var pbs = Components.classes["@mozilla.org/storagestream;1"] | 283 | var pbs = Components.classes["@mozilla.org/storagestream;1"] |
290 | .createInstance(Components.interfaces.nsIStorageStream); | 284 | .createInstance(Components.interfaces.nsIStorageStream); |
291 | pbs.init(1024,10000000,null); | 285 | pbs.init(1024,10000000,null); |
292 | // create output stream | 286 | // create output stream |
293 | var pbos = pbs.getOutputStream(0); | 287 | var pbos = pbs.getOutputStream(0); |
294 | // and a binaryoutputstream interface | 288 | // and a binaryoutputstream interface |
295 | var pbbos = Components.classes["@mozilla.org/binaryoutputstream;1"] | 289 | var pbbos = Components.classes["@mozilla.org/binaryoutputstream;1"] |
296 | .createInstance(Components.interfaces.nsIBinaryOutputStream); | 290 | .createInstance(Components.interfaces.nsIBinaryOutputStream); |
297 | pbbos.setOutputStream(pbos); | 291 | pbbos.setOutputStream(pbos); |
298 | 292 | ||
299 | /* create POST body */ | 293 | /* create POST body */ |
300 | var boundarytoken = 'kadaroloongazaduviaxamma'; | 294 | var boundarytoken = 'kadaroloongazaduviaxamma'; |
301 | var boundary = '--'+boundarytoken; | 295 | var boundary = '--'+boundarytoken; |
302 | var b = ''; | 296 | var b = ''; |
303 | 297 | ||
304 | var parms = { api_key: this.api_key, auth_token: this.token }; | 298 | var parms = { api_key: this.api_key, auth_token: this.token }; |
305 | for(var p in fa) parms[p] = fa[p]; | 299 | for(var p in fa) parms[p] = fa[p]; |
306 | var pns = new Array(); | 300 | var pns = new Array(); |
307 | for(var p in parms) pns.push(p); | 301 | for(var p in parms) pns.push(p); |
308 | var pstr = ''; | 302 | var pstr = ''; |
309 | for(var p in pns.sort()) { | 303 | for(var p in pns.sort()) { |
310 | var pn = pns[p]; | 304 | var pn = pns[p]; |
311 | pstr += pn+parms[pn]; | 305 | pstr += pn+parms[pn]; |
312 | b += boundary+'\nContent-Disposition: form-data; name="'+pn+'"\n\n'+toutf8(parms[pn])+'\n'; | 306 | b += boundary+'\nContent-Disposition: form-data; name="'+pn+'"\n\n'+toutf8(parms[pn])+'\n'; |
313 | } | 307 | } |
314 | b += boundary+'\nContent-Disposition: form-data; name="api_sig"\n\n'+this.api_sig(pstr)+'\n'; | 308 | b += boundary+'\nContent-Disposition: form-data; name="api_sig"\n\n'+this.api_sig(pstr)+'\n'; |
315 | b += boundary+'\nContent-Disposition: form-data; name="photo"; filename="'+f+'"\nContent-Type: image/jpeg\nContent-Transfer-Encoding: binary\n\n'; | 309 | b += boundary+'\nContent-Disposition: form-data; name="photo"; filename="'+f+'"\nContent-Type: image/jpeg\nContent-Transfer-Encoding: binary\n\n'; |
316 | pbbos.writeBytes(b,b.length); | 310 | pbbos.writeBytes(b,b.length); |
317 | var bisbytes = bis.available(); | 311 | var bisbytes = bis.available(); |
318 | pbbos.writeBytes(bis.readBytes(bisbytes),bisbytes); | 312 | pbbos.writeBytes(bis.readBytes(bisbytes),bisbytes); |
319 | pbbos.writeBytes('\n'+boundary+'--',3+boundary.length); bis.close(); st.close(); | 313 | pbbos.writeBytes('\n'+boundary+'--',3+boundary.length); bis.close(); st.close(); |
320 | 314 | ||
321 | pbbos.close(); pbos.close(); | 315 | pbbos.close(); pbos.close(); |
322 | 316 | ||
323 | var x = new XMLHttpRequest(); | 317 | var x = new XMLHttpRequest(); |
324 | x.open("POST",this.upload_url); | 318 | x.open("POST",this.upload_url); |
325 | x.setRequestHeader('Content-Type', 'multipart/form-data; boundary="'+boundarytoken+'"'); | 319 | x.setRequestHeader('Content-Type', 'multipart/form-data; boundary="'+boundarytoken+'"'); |
326 | x.setRequestHeader('Connection','close'); | 320 | x.setRequestHeader('Connection','close'); |
327 | x.setRequestHeader('Content-Length',b.length); | 321 | x.setRequestHeader('Content-Length',b.length); |
328 | x.onreadystatechange=function() { | 322 | x.onreadystatechange=function() { |
329 | if(x.readyState!=4) return false; | 323 | if(x.readyState!=4) return false; |
330 | if(x.status==200) { | 324 | if(x.status==200) { |
331 | var stat = x.responseXML.firstChild.getAttribute('stat'); | 325 | var stat = x.responseXML.firstChild.getAttribute('stat'); |
332 | if(stat=='ok') { | 326 | if(stat=='ok') { |
333 | var pid = xp_str('/rsp/photoid',x.responseXML); | 327 | var pid = xp_str('/rsp/photoid',x.responseXML); |
334 | if(on_success) on_success(x,pid); | 328 | if(on_success) on_success(x,pid); |
335 | }else{ | 329 | }else{ |
336 | var e = x.responseXML.getElementsByTagName('err').item(0); | 330 | var e = x.responseXML.getElementsByTagName('err').item(0); |
337 | var ecode = e.getAttribute('code'); | 331 | var ecode = e.getAttribute('code'); |
338 | var emsg = e.getAttribute('msg'); | 332 | var emsg = e.getAttribute('msg'); |
339 | dump('upload failed: '+ecode+' '+emsg+'\n'); | 333 | dump('upload failed: '+ecode+' '+emsg+'\n'); |
340 | if(on_failure) on_failure(x,stat,ecode,emsg); | 334 | if(on_failure) on_failure(x,stat,ecode,emsg); |
341 | } | 335 | } |
342 | }else{ | 336 | }else{ |
343 | if(on_failure) on_failure(x); | 337 | if(on_failure) on_failure(x); |
344 | } | 338 | } |
345 | return true; | 339 | return true; |
346 | }; | 340 | }; |
347 | x.send(pbs.newInputStream(0)); | 341 | x.send(pbs.newInputStream(0)); |
348 | }catch(e) { | 342 | }catch(e) { |
349 | if(on_failure) on_failure(e,null,-1,e.message); | 343 | if(on_failure) on_failure(e,null,-1,e.message); |
350 | } | 344 | } |
351 | } | 345 | } |
352 | 346 | ||
353 | }; | 347 | }; |