-rw-r--r-- | content/flickr.js | 4 |
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 = { this.prefs.clearUserPref(this.prefs_root+'.auth_user.fullname'); }, _reset_token: function() { this.token = null; this.perms = null; this.user = null; return false; }, load_token: function() { try { if(this.prefs.getPrefType(this.prefs_root+'.auth_token')!=this.prefs.PREF_STRING) return this._reset_token(); this.token = this.prefs.getCharPref(this.prefs_root+'.auth_token'); if(this.prefs.getPrefType(this.prefs_root+'.auth_perms')!=this.prefs.PREF_STRING) return this._reset_token(); this.perms = this.prefs.getCharPref(this.prefs_root+'.auth_perms'); if(this.prefs.getPrefType(this.prefs_root+'.auth_user.nsid')!=this.prefs.PREF_STRING) return this._reset_token(); this.user = new Object(); this.user.nsid = this.prefs.getCharPref(this.prefs_root+'.auth_user.nsid'); if(this.prefs.getPrefType(this.prefs_root+'.auth_user.username')!=this.prefs.PREF_STRING) return this._reset_token(); this.user.username = this.prefs.getCharPref(this.prefs_root+'.auth_user.username'); if(this.prefs.getPrefType(this.prefs_root+'.auth_user.fullname')!=this.prefs.PREF_STRING) return this._reset_token(); this.user.fullname = this.prefs.getCharPref(this.prefs_root+'.auth_user.fullname'); }catch(e) { return this._reset_token(); } return true; }, reset_token: function() { this._reset_token(); this.save_token(); }, get_photo_url: function(ser,id,sec,sfx,ext) { var rv = this.photo_url + ser + '/' + id + '_' + sec; if(sfx && sfx!='_') rv += '_'+sfx; rv += ext?'.'+ext:'.jpg'; return rv; }, get_image_url: function(o,sfx) { return this.get_photo_url( o.server, (o instanceof Photoset)? o.primary : o.id, o.secret, sfx, (sfx=='o')?o.originalformat:null ); }, get_photo_page_url: function(p) { if(p instanceof Photo) { // TODO: track photoset and user owner id from there? // The approach below is sheerly wrong. var o = this.user.nsid; if(p.owner && p.owner.nsid) o = p.owner.nsid; var rv = this.photos_url + o +'/' + p.id; return rv; }else // TODO: take owner into account? return this.photos_url + this.user.nsid + '/' + p; }, make_photo_url: function(p,sfx) { if(sfx=='p') return this.get_photo_page_url(p); else return this.get_image_url(p,sfx); }, + make_photoset_url: function(ps) { + // TODO: allow for using someone else's photoset? + return this.photos_url+this.user.nsid+'/sets/'+ps.id; + }, upload_file: function(f,fa,on_success,on_failure) { try { var fi = Components.classes["@mozilla.org/file/local;1"] .createInstance(Components.interfaces.nsILocalFile); fi.initWithPath( f ); var st = Components.classes["@mozilla.org/network/file-input-stream;1"] .createInstance(Components.interfaces.nsIFileInputStream); st.init(fi,0x01,00004,null); var bis = Components.classes["@mozilla.org/binaryinputstream;1"] .createInstance(Components.interfaces.nsIBinaryInputStream); bis.setInputStream(st); // allocate and initialize temp storage string var pbs = Components.classes["@mozilla.org/storagestream;1"] .createInstance(Components.interfaces.nsIStorageStream); pbs.init(1024,10000000,null); // create output stream var pbos = pbs.getOutputStream(0); // and a binaryoutputstream interface var pbbos = Components.classes["@mozilla.org/binaryoutputstream;1"] .createInstance(Components.interfaces.nsIBinaryOutputStream); pbbos.setOutputStream(pbos); /* create POST body */ var boundarytoken = 'kadaroloongazaduviaxamma'; var boundary = '--'+boundarytoken; var b = ''; var parms = { api_key: this.api_key, auth_token: this.token }; for(var p in fa) parms[p] = fa[p]; var pns = new Array(); for(var p in parms) pns.push(p); var pstr = ''; for(var p in pns.sort()) { var pn = pns[p]; pstr += pn+parms[pn]; b += boundary+'\nContent-Disposition: form-data; name="'+pn+'"\n\n'+toutf8(parms[pn])+'\n'; } b += boundary+'\nContent-Disposition: form-data; name="api_sig"\n\n'+this.api_sig(pstr)+'\n'; b += boundary+'\nContent-Disposition: form-data; name="photo"; filename="'+f+'"\nContent-Type: image/jpeg\nContent-Transfer-Encoding: binary\n\n'; pbbos.writeBytes(b,b.length); var bisbytes = bis.available(); pbbos.writeBytes(bis.readBytes(bisbytes),bisbytes); pbbos.writeBytes('\n'+boundary+'--',3+boundary.length); bis.close(); st.close(); pbbos.close(); pbos.close(); var x = new XMLHttpRequest(); x.open("POST",this.upload_url); x.setRequestHeader('Content-Type', 'multipart/form-data; boundary="'+boundarytoken+'"'); x.setRequestHeader('Connection','close'); x.setRequestHeader('Content-Length',b.length); x.onreadystatechange=function() { if(x.readyState!=4) return false; if(x.status==200) { var stat = x.responseXML.firstChild.getAttribute('stat'); if(stat=='ok') { var pid = xp_str('/rsp/photoid',x.responseXML); if(on_success) on_success(x,pid); }else{ var e = x.responseXML.getElementsByTagName('err').item(0); var ecode = e.getAttribute('code'); var emsg = e.getAttribute('msg'); |