-rw-r--r-- | content/flickr.js | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/content/flickr.js b/content/flickr.js index b5bfa43..75d1a72 100644 --- a/content/flickr.js +++ b/content/flickr.js | |||
@@ -162,186 +162,191 @@ Flickr.prototype = { | |||
162 | token: null, | 162 | token: null, |
163 | perms: null, | 163 | perms: null, |
164 | user: null, | 164 | user: null, |
165 | authorize_1: function(on_s, on_f) { | 165 | authorize_1: function(on_s, on_f) { |
166 | var _this = this; | 166 | var _this = this; |
167 | this.api_call( | 167 | this.api_call( |
168 | { method: 'flickr.auth.getToken', frob: this.frob }, | 168 | { method: 'flickr.auth.getToken', frob: this.frob }, |
169 | function(x) { | 169 | function(x) { |
170 | _this.token = xp_str('/rsp/auth/token',x.responseXML); | 170 | _this.token = xp_str('/rsp/auth/token',x.responseXML); |
171 | _this.perms = xp_str('/rsp/auth/perms',x.responseXML); | 171 | _this.perms = xp_str('/rsp/auth/perms',x.responseXML); |
172 | var u = xp_node('/rsp/auth/user',x.responseXML); | 172 | var u = xp_node('/rsp/auth/user',x.responseXML); |
173 | _this.user = { | 173 | _this.user = { |
174 | nsid: u.getAttribute('nsid'), | 174 | nsid: u.getAttribute('nsid'), |
175 | username: u.getAttribute('username'), | 175 | username: u.getAttribute('username'), |
176 | fullname: u.getAttribute('fullname') | 176 | fullname: u.getAttribute('fullname') |
177 | }; | 177 | }; |
178 | if(on_s) on_s(x); | 178 | if(on_s) on_s(x); |
179 | }, function(x,s,c,m) { | 179 | }, function(x,s,c,m) { |
180 | if(on_f) on_f(x,s,c,m); | 180 | if(on_f) on_f(x,s,c,m); |
181 | } | 181 | } |
182 | ); | 182 | ); |
183 | }, | 183 | }, |
184 | 184 | ||
185 | prefs: Components.classes['@mozilla.org/preferences-service;1'].getService( | 185 | prefs: Components.classes['@mozilla.org/preferences-service;1'].getService( |
186 | Components.interfaces.nsIPrefBranch | 186 | Components.interfaces.nsIPrefBranch |
187 | ), | 187 | ), |
188 | prefs_root: 'net.klever.kin.flickr', | 188 | prefs_root: 'net.klever.kin.flickr', |
189 | save_token: function() { | 189 | save_token: function() { |
190 | // 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 |
191 | if(this.token) | 191 | if(this.token) |
192 | this.prefs.setCharPref(this.prefs_root+'.auth_token',this.token); | 192 | this.prefs.setCharPref(this.prefs_root+'.auth_token',this.token); |
193 | else | 193 | else |
194 | this.prefs.clearUserPref(this.prefs_root+'.auth_token'); | 194 | this.prefs.clearUserPref(this.prefs_root+'.auth_token'); |
195 | if(this.perms) | 195 | if(this.perms) |
196 | this.prefs.setCharPref(this.prefs_root+'.auth_perms',this.perms); | 196 | this.prefs.setCharPref(this.prefs_root+'.auth_perms',this.perms); |
197 | else | 197 | else |
198 | this.prefs.clearUserPref(this.prefs_root+'.auth_perms'); | 198 | this.prefs.clearUserPref(this.prefs_root+'.auth_perms'); |
199 | if(this.user && this.user.nsid!=null && this.user.nsid!=undefined) | 199 | if(this.user && this.user.nsid!=null && this.user.nsid!=undefined) |
200 | 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); |
201 | else | 201 | else |
202 | this.prefs.clearUserPref(this.prefs_root+'.auth_user.nsid'); | 202 | this.prefs.clearUserPref(this.prefs_root+'.auth_user.nsid'); |
203 | if(this.user && this.user.username!=null && this.user.username!=undefined) | 203 | if(this.user && this.user.username!=null && this.user.username!=undefined) |
204 | 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); |
205 | else | 205 | else |
206 | this.prefs.clearUserPref(this.prefs_root+'.auth_user.username'); | 206 | this.prefs.clearUserPref(this.prefs_root+'.auth_user.username'); |
207 | if(this.user && this.user.fullname!=null && this.user.fullname!=undefined) | 207 | if(this.user && this.user.fullname!=null && this.user.fullname!=undefined) |
208 | 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); |
209 | else | 209 | else |
210 | this.prefs.clearUserPref(this.prefs_root+'.auth_user.fullname'); | 210 | this.prefs.clearUserPref(this.prefs_root+'.auth_user.fullname'); |
211 | }, | 211 | }, |
212 | _reset_token: function() { | 212 | _reset_token: function() { |
213 | this.token = null; this.perms = null; this.user = null; | 213 | this.token = null; this.perms = null; this.user = null; |
214 | return false; | 214 | return false; |
215 | }, | 215 | }, |
216 | load_token: function() { | 216 | load_token: function() { |
217 | try { | 217 | try { |
218 | 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) |
219 | return this._reset_token(); | 219 | return this._reset_token(); |
220 | this.token = this.prefs.getCharPref(this.prefs_root+'.auth_token'); | 220 | this.token = this.prefs.getCharPref(this.prefs_root+'.auth_token'); |
221 | 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) |
222 | return this._reset_token(); | 222 | return this._reset_token(); |
223 | this.perms = this.prefs.getCharPref(this.prefs_root+'.auth_perms'); | 223 | this.perms = this.prefs.getCharPref(this.prefs_root+'.auth_perms'); |
224 | 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) |
225 | return this._reset_token(); | 225 | return this._reset_token(); |
226 | this.user = new Object(); | 226 | this.user = new Object(); |
227 | 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'); |
228 | 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) |
229 | return this._reset_token(); | 229 | return this._reset_token(); |
230 | 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'); |
231 | 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) |
232 | return this._reset_token(); | 232 | return this._reset_token(); |
233 | 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'); |
234 | }catch(e) { return this._reset_token(); } | 234 | }catch(e) { return this._reset_token(); } |
235 | return true; | 235 | return true; |
236 | }, | 236 | }, |
237 | reset_token: function() { | 237 | reset_token: function() { |
238 | this._reset_token(); | 238 | this._reset_token(); |
239 | this.save_token(); | 239 | this.save_token(); |
240 | }, | 240 | }, |
241 | 241 | ||
242 | get_photo_url: function(ser,id,sec,sfx,ext) { | 242 | get_photo_url: function(ser,id,sec,sfx,ext) { |
243 | var rv = this.photo_url + ser + '/' + id + '_' + sec; | 243 | var rv = this.photo_url + ser + '/' + id + '_' + sec; |
244 | if(sfx && sfx!='_') rv += '_'+sfx; | 244 | if(sfx && sfx!='_') rv += '_'+sfx; |
245 | rv += ext?'.'+ext:'.jpg'; | 245 | rv += ext?'.'+ext:'.jpg'; |
246 | return rv; | 246 | return rv; |
247 | }, | 247 | }, |
248 | get_image_url: function(o,sfx) { | 248 | get_image_url: function(o,sfx) { |
249 | return this.get_photo_url( | 249 | return this.get_photo_url( |
250 | o.server, | 250 | o.server, |
251 | (o instanceof Photoset)? o.primary : o.id, | 251 | (o instanceof Photoset)? o.primary : o.id, |
252 | o.secret, | 252 | o.secret, |
253 | sfx, | 253 | sfx, |
254 | (sfx=='o')?o.originalformat:null | 254 | (sfx=='o')?o.originalformat:null |
255 | ); | 255 | ); |
256 | }, | 256 | }, |
257 | get_photo_page_url: function(p) { | 257 | get_photo_page_url: function(p) { |
258 | if(p instanceof Photo) // TODO: half wrong, what if no owner? | 258 | if(p instanceof Photo) { |
259 | return this.photos_url + (p.owner.nsid?p.owner.nsid:this.user.nsid) + '/' + p.id; | 259 | // TODO: track photoset and user owner id from there? |
260 | else // TODO: take owner into account? | 260 | // The approach below is sheerly wrong. |
261 | var o = this.user.nsid; | ||
262 | if(p.owner && p.owner.nsid) o = p.owner.nsid; | ||
263 | var rv = this.photos_url + o +'/' + p.id; | ||
264 | return rv; | ||
265 | }else // TODO: take owner into account? | ||
261 | return this.photos_url + this.user.nsid + '/' + p; | 266 | return this.photos_url + this.user.nsid + '/' + p; |
262 | }, | 267 | }, |
263 | make_photo_url: function(p,sfx) { | 268 | make_photo_url: function(p,sfx) { |
264 | if(sfx=='p') | 269 | if(sfx=='p') |
265 | return this.get_photo_page_url(p); | 270 | return this.get_photo_page_url(p); |
266 | else | 271 | else |
267 | return this.get_image_url(p,sfx); | 272 | return this.get_image_url(p,sfx); |
268 | }, | 273 | }, |
269 | 274 | ||
270 | upload_file: function(f,fa,on_success,on_failure) { | 275 | upload_file: function(f,fa,on_success,on_failure) { |
271 | try { | 276 | try { |
272 | var fi = Components.classes["@mozilla.org/file/local;1"] | 277 | var fi = Components.classes["@mozilla.org/file/local;1"] |
273 | .createInstance(Components.interfaces.nsILocalFile); | 278 | .createInstance(Components.interfaces.nsILocalFile); |
274 | fi.initWithPath( f ); | 279 | fi.initWithPath( f ); |
275 | var st = Components.classes["@mozilla.org/network/file-input-stream;1"] | 280 | var st = Components.classes["@mozilla.org/network/file-input-stream;1"] |
276 | .createInstance(Components.interfaces.nsIFileInputStream); | 281 | .createInstance(Components.interfaces.nsIFileInputStream); |
277 | st.init(fi,0x01,00004,null); | 282 | st.init(fi,0x01,00004,null); |
278 | var bis = Components.classes["@mozilla.org/binaryinputstream;1"] | 283 | var bis = Components.classes["@mozilla.org/binaryinputstream;1"] |
279 | .createInstance(Components.interfaces.nsIBinaryInputStream); | 284 | .createInstance(Components.interfaces.nsIBinaryInputStream); |
280 | bis.setInputStream(st); | 285 | bis.setInputStream(st); |
281 | 286 | ||
282 | // allocate and initialize temp storage string | 287 | // allocate and initialize temp storage string |
283 | var pbs = Components.classes["@mozilla.org/storagestream;1"] | 288 | var pbs = Components.classes["@mozilla.org/storagestream;1"] |
284 | .createInstance(Components.interfaces.nsIStorageStream); | 289 | .createInstance(Components.interfaces.nsIStorageStream); |
285 | pbs.init(1024,10000000,null); | 290 | pbs.init(1024,10000000,null); |
286 | // create output stream | 291 | // create output stream |
287 | var pbos = pbs.getOutputStream(0); | 292 | var pbos = pbs.getOutputStream(0); |
288 | // and a binaryoutputstream interface | 293 | // and a binaryoutputstream interface |
289 | var pbbos = Components.classes["@mozilla.org/binaryoutputstream;1"] | 294 | var pbbos = Components.classes["@mozilla.org/binaryoutputstream;1"] |
290 | .createInstance(Components.interfaces.nsIBinaryOutputStream); | 295 | .createInstance(Components.interfaces.nsIBinaryOutputStream); |
291 | pbbos.setOutputStream(pbos); | 296 | pbbos.setOutputStream(pbos); |
292 | 297 | ||
293 | /* create POST body */ | 298 | /* create POST body */ |
294 | var boundarytoken = 'kadaroloongazaduviaxamma'; | 299 | var boundarytoken = 'kadaroloongazaduviaxamma'; |
295 | var boundary = '--'+boundarytoken; | 300 | var boundary = '--'+boundarytoken; |
296 | var b = ''; | 301 | var b = ''; |
297 | 302 | ||
298 | var parms = { api_key: this.api_key, auth_token: this.token }; | 303 | var parms = { api_key: this.api_key, auth_token: this.token }; |
299 | for(var p in fa) parms[p] = fa[p]; | 304 | for(var p in fa) parms[p] = fa[p]; |
300 | var pns = new Array(); | 305 | var pns = new Array(); |
301 | for(var p in parms) pns.push(p); | 306 | for(var p in parms) pns.push(p); |
302 | var pstr = ''; | 307 | var pstr = ''; |
303 | for(var p in pns.sort()) { | 308 | for(var p in pns.sort()) { |
304 | var pn = pns[p]; | 309 | var pn = pns[p]; |
305 | pstr += pn+parms[pn]; | 310 | pstr += pn+parms[pn]; |
306 | b += boundary+'\nContent-Disposition: form-data; name="'+pn+'"\n\n'+toutf8(parms[pn])+'\n'; | 311 | b += boundary+'\nContent-Disposition: form-data; name="'+pn+'"\n\n'+toutf8(parms[pn])+'\n'; |
307 | } | 312 | } |
308 | b += boundary+'\nContent-Disposition: form-data; name="api_sig"\n\n'+this.api_sig(pstr)+'\n'; | 313 | b += boundary+'\nContent-Disposition: form-data; name="api_sig"\n\n'+this.api_sig(pstr)+'\n'; |
309 | b += boundary+'\nContent-Disposition: form-data; name="photo"; filename="'+f+'"\nContent-Type: image/jpeg\nContent-Transfer-Encoding: binary\n\n'; | 314 | b += boundary+'\nContent-Disposition: form-data; name="photo"; filename="'+f+'"\nContent-Type: image/jpeg\nContent-Transfer-Encoding: binary\n\n'; |
310 | pbbos.writeBytes(b,b.length); | 315 | pbbos.writeBytes(b,b.length); |
311 | var bisbytes = bis.available(); | 316 | var bisbytes = bis.available(); |
312 | pbbos.writeBytes(bis.readBytes(bisbytes),bisbytes); | 317 | pbbos.writeBytes(bis.readBytes(bisbytes),bisbytes); |
313 | pbbos.writeBytes('\n'+boundary+'--',3+boundary.length); bis.close(); st.close(); | 318 | pbbos.writeBytes('\n'+boundary+'--',3+boundary.length); bis.close(); st.close(); |
314 | 319 | ||
315 | pbbos.close(); pbos.close(); | 320 | pbbos.close(); pbos.close(); |
316 | 321 | ||
317 | var x = new XMLHttpRequest(); | 322 | var x = new XMLHttpRequest(); |
318 | x.open("POST",this.upload_url); | 323 | x.open("POST",this.upload_url); |
319 | x.setRequestHeader('Content-Type', 'multipart/form-data; boundary="'+boundarytoken+'"'); | 324 | x.setRequestHeader('Content-Type', 'multipart/form-data; boundary="'+boundarytoken+'"'); |
320 | x.setRequestHeader('Connection','close'); | 325 | x.setRequestHeader('Connection','close'); |
321 | x.setRequestHeader('Content-Length',b.length); | 326 | x.setRequestHeader('Content-Length',b.length); |
322 | x.onreadystatechange=function() { | 327 | x.onreadystatechange=function() { |
323 | if(x.readyState!=4) return false; | 328 | if(x.readyState!=4) return false; |
324 | if(x.status==200) { | 329 | if(x.status==200) { |
325 | var stat = x.responseXML.firstChild.getAttribute('stat'); | 330 | var stat = x.responseXML.firstChild.getAttribute('stat'); |
326 | if(stat=='ok') { | 331 | if(stat=='ok') { |
327 | var pid = xp_str('/rsp/photoid',x.responseXML); | 332 | var pid = xp_str('/rsp/photoid',x.responseXML); |
328 | if(on_success) on_success(x,pid); | 333 | if(on_success) on_success(x,pid); |
329 | }else{ | 334 | }else{ |
330 | var e = x.responseXML.getElementsByTagName('err').item(0); | 335 | var e = x.responseXML.getElementsByTagName('err').item(0); |
331 | var ecode = e.getAttribute('code'); | 336 | var ecode = e.getAttribute('code'); |
332 | var emsg = e.getAttribute('msg'); | 337 | var emsg = e.getAttribute('msg'); |
333 | dump('upload failed: '+ecode+' '+emsg+'\n'); | 338 | dump('upload failed: '+ecode+' '+emsg+'\n'); |
334 | if(on_failure) on_failure(x,stat,ecode,emsg); | 339 | if(on_failure) on_failure(x,stat,ecode,emsg); |
335 | } | 340 | } |
336 | }else{ | 341 | }else{ |
337 | if(on_failure) on_failure(x); | 342 | if(on_failure) on_failure(x); |
338 | } | 343 | } |
339 | return true; | 344 | return true; |
340 | }; | 345 | }; |
341 | x.send(pbs.newInputStream(0)); | 346 | x.send(pbs.newInputStream(0)); |
342 | }catch(e) { | 347 | }catch(e) { |
343 | if(on_failure) on_failure(e,null,-1,e.message); | 348 | if(on_failure) on_failure(e,null,-1,e.message); |
344 | } | 349 | } |
345 | } | 350 | } |
346 | 351 | ||
347 | }; | 352 | }; |