summaryrefslogtreecommitdiffabout
path: root/content/flickr.js
Unidiff
Diffstat (limited to 'content/flickr.js') (more/less context) (ignore whitespace changes)
-rw-r--r--content/flickr.js4
1 files changed, 4 insertions, 0 deletions
diff --git a/content/flickr.js b/content/flickr.js
index 75d1a72..b8360c1 100644
--- a/content/flickr.js
+++ b/content/flickr.js
@@ -210,128 +210,132 @@ Flickr.prototype = {
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) { 258 if(p instanceof Photo) {
259 // TODO: track photoset and user owner id from there? 259 // TODO: track photoset and user owner id from there?
260 // The approach below is sheerly wrong. 260 // The approach below is sheerly wrong.
261 var o = this.user.nsid; 261 var o = this.user.nsid;
262 if(p.owner && p.owner.nsid) o = p.owner.nsid; 262 if(p.owner && p.owner.nsid) o = p.owner.nsid;
263 var rv = this.photos_url + o +'/' + p.id; 263 var rv = this.photos_url + o +'/' + p.id;
264 return rv; 264 return rv;
265 }else // TODO: take owner into account? 265 }else // TODO: take owner into account?
266 return this.photos_url + this.user.nsid + '/' + p; 266 return this.photos_url + this.user.nsid + '/' + p;
267 }, 267 },
268 make_photo_url: function(p,sfx) { 268 make_photo_url: function(p,sfx) {
269 if(sfx=='p') 269 if(sfx=='p')
270 return this.get_photo_page_url(p); 270 return this.get_photo_page_url(p);
271 else 271 else
272 return this.get_image_url(p,sfx); 272 return this.get_image_url(p,sfx);
273 }, 273 },
274 make_photoset_url: function(ps) {
275 // TODO: allow for using someone else's photoset?
276 return this.photos_url+this.user.nsid+'/sets/'+ps.id;
277 },
274 278
275 upload_file: function(f,fa,on_success,on_failure) { 279 upload_file: function(f,fa,on_success,on_failure) {
276 try { 280 try {
277 var fi = Components.classes["@mozilla.org/file/local;1"] 281 var fi = Components.classes["@mozilla.org/file/local;1"]
278 .createInstance(Components.interfaces.nsILocalFile); 282 .createInstance(Components.interfaces.nsILocalFile);
279 fi.initWithPath( f ); 283 fi.initWithPath( f );
280 var st = Components.classes["@mozilla.org/network/file-input-stream;1"] 284 var st = Components.classes["@mozilla.org/network/file-input-stream;1"]
281 .createInstance(Components.interfaces.nsIFileInputStream); 285 .createInstance(Components.interfaces.nsIFileInputStream);
282 st.init(fi,0x01,00004,null); 286 st.init(fi,0x01,00004,null);
283 var bis = Components.classes["@mozilla.org/binaryinputstream;1"] 287 var bis = Components.classes["@mozilla.org/binaryinputstream;1"]
284 .createInstance(Components.interfaces.nsIBinaryInputStream); 288 .createInstance(Components.interfaces.nsIBinaryInputStream);
285 bis.setInputStream(st); 289 bis.setInputStream(st);
286 290
287 // allocate and initialize temp storage string 291 // allocate and initialize temp storage string
288 var pbs = Components.classes["@mozilla.org/storagestream;1"] 292 var pbs = Components.classes["@mozilla.org/storagestream;1"]
289 .createInstance(Components.interfaces.nsIStorageStream); 293 .createInstance(Components.interfaces.nsIStorageStream);
290 pbs.init(1024,10000000,null); 294 pbs.init(1024,10000000,null);
291 // create output stream 295 // create output stream
292 var pbos = pbs.getOutputStream(0); 296 var pbos = pbs.getOutputStream(0);
293 // and a binaryoutputstream interface 297 // and a binaryoutputstream interface
294 var pbbos = Components.classes["@mozilla.org/binaryoutputstream;1"] 298 var pbbos = Components.classes["@mozilla.org/binaryoutputstream;1"]
295 .createInstance(Components.interfaces.nsIBinaryOutputStream); 299 .createInstance(Components.interfaces.nsIBinaryOutputStream);
296 pbbos.setOutputStream(pbos); 300 pbbos.setOutputStream(pbos);
297 301
298 /* create POST body */ 302 /* create POST body */
299 var boundarytoken = 'kadaroloongazaduviaxamma'; 303 var boundarytoken = 'kadaroloongazaduviaxamma';
300 var boundary = '--'+boundarytoken; 304 var boundary = '--'+boundarytoken;
301 var b = ''; 305 var b = '';
302 306
303 var parms = { api_key: this.api_key, auth_token: this.token }; 307 var parms = { api_key: this.api_key, auth_token: this.token };
304 for(var p in fa) parms[p] = fa[p]; 308 for(var p in fa) parms[p] = fa[p];
305 var pns = new Array(); 309 var pns = new Array();
306 for(var p in parms) pns.push(p); 310 for(var p in parms) pns.push(p);
307 var pstr = ''; 311 var pstr = '';
308 for(var p in pns.sort()) { 312 for(var p in pns.sort()) {
309 var pn = pns[p]; 313 var pn = pns[p];
310 pstr += pn+parms[pn]; 314 pstr += pn+parms[pn];
311 b += boundary+'\nContent-Disposition: form-data; name="'+pn+'"\n\n'+toutf8(parms[pn])+'\n'; 315 b += boundary+'\nContent-Disposition: form-data; name="'+pn+'"\n\n'+toutf8(parms[pn])+'\n';
312 } 316 }
313 b += boundary+'\nContent-Disposition: form-data; name="api_sig"\n\n'+this.api_sig(pstr)+'\n'; 317 b += boundary+'\nContent-Disposition: form-data; name="api_sig"\n\n'+this.api_sig(pstr)+'\n';
314 b += boundary+'\nContent-Disposition: form-data; name="photo"; filename="'+f+'"\nContent-Type: image/jpeg\nContent-Transfer-Encoding: binary\n\n'; 318 b += boundary+'\nContent-Disposition: form-data; name="photo"; filename="'+f+'"\nContent-Type: image/jpeg\nContent-Transfer-Encoding: binary\n\n';
315 pbbos.writeBytes(b,b.length); 319 pbbos.writeBytes(b,b.length);
316 var bisbytes = bis.available(); 320 var bisbytes = bis.available();
317 pbbos.writeBytes(bis.readBytes(bisbytes),bisbytes); 321 pbbos.writeBytes(bis.readBytes(bisbytes),bisbytes);
318 pbbos.writeBytes('\n'+boundary+'--',3+boundary.length); bis.close(); st.close(); 322 pbbos.writeBytes('\n'+boundary+'--',3+boundary.length); bis.close(); st.close();
319 323
320 pbbos.close(); pbos.close(); 324 pbbos.close(); pbos.close();
321 325
322 var x = new XMLHttpRequest(); 326 var x = new XMLHttpRequest();
323 x.open("POST",this.upload_url); 327 x.open("POST",this.upload_url);
324 x.setRequestHeader('Content-Type', 'multipart/form-data; boundary="'+boundarytoken+'"'); 328 x.setRequestHeader('Content-Type', 'multipart/form-data; boundary="'+boundarytoken+'"');
325 x.setRequestHeader('Connection','close'); 329 x.setRequestHeader('Connection','close');
326 x.setRequestHeader('Content-Length',b.length); 330 x.setRequestHeader('Content-Length',b.length);
327 x.onreadystatechange=function() { 331 x.onreadystatechange=function() {
328 if(x.readyState!=4) return false; 332 if(x.readyState!=4) return false;
329 if(x.status==200) { 333 if(x.status==200) {
330 var stat = x.responseXML.firstChild.getAttribute('stat'); 334 var stat = x.responseXML.firstChild.getAttribute('stat');
331 if(stat=='ok') { 335 if(stat=='ok') {
332 var pid = xp_str('/rsp/photoid',x.responseXML); 336 var pid = xp_str('/rsp/photoid',x.responseXML);
333 if(on_success) on_success(x,pid); 337 if(on_success) on_success(x,pid);
334 }else{ 338 }else{
335 var e = x.responseXML.getElementsByTagName('err').item(0); 339 var e = x.responseXML.getElementsByTagName('err').item(0);
336 var ecode = e.getAttribute('code'); 340 var ecode = e.getAttribute('code');
337 var emsg = e.getAttribute('msg'); 341 var emsg = e.getAttribute('msg');