summaryrefslogtreecommitdiffabout
path: root/content/flickr.js
Unidiff
Diffstat (limited to 'content/flickr.js') (more/less context) (ignore whitespace changes)
-rw-r--r--content/flickr.js20
1 files changed, 20 insertions, 0 deletions
diff --git a/content/flickr.js b/content/flickr.js
index e09d5f0..545144c 100644
--- a/content/flickr.js
+++ b/content/flickr.js
@@ -1,361 +1,381 @@
1/* 1/*
2 * Photoset 2 * Photoset
3 */ 3 */
4 4
5function Photoset(s) { 5function 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}
11Photoset.prototype = { 11Photoset.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 */
34function Photo(s) { 34function 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}
40Photo.prototype = { 40Photo.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
92function Flickr() { } 92function Flickr() { }
93Flickr.prototype = { 93Flickr.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 uploader_edit_url: 'http://www.flickr.com/tools/uploader_edit.gne', 100 uploader_edit_url: 'http://www.flickr.com/tools/uploader_edit.gne',
101 101
102 api_sig: function(paramstr) { 102 api_sig: function(paramstr) {
103 return MD5(toutf8(this.api_shs+paramstr)); 103 return MD5(toutf8(this.api_shs+paramstr));
104 }, 104 },
105 api_call_url: function(params,url) { 105 api_call_url: function(params,url) {
106 params.api_key = this.api_key; 106 params.api_key = this.api_key;
107 var pp = new Array(); 107 var pp = new Array();
108 for(var p in params) { 108 for(var p in params) {
109 pp.push(p); 109 pp.push(p);
110 } 110 }
111 var pstr = ''; 111 var pstr = '';
112 var rv = (url?url:this.rest_url)+'?'; 112 var rv = (url?url:this.rest_url)+'?';
113 for(var p in pp.sort()) { 113 for(var p in pp.sort()) {
114 var pn = pp[p]; 114 var pn = pp[p];
115 pstr += pn+params[pn]; 115 pstr += pn+params[pn];
116 rv += pn+'='+params[pn]+'&'; 116 rv += pn+'='+params[pn]+'&';
117 } 117 }
118 rv += 'api_sig='+this.api_sig(pstr); 118 rv += 'api_sig='+this.api_sig(pstr);
119 return rv; 119 return rv;
120 }, 120 },
121 api_call_json: function(params,on_success,on_failure) {
122 if(params.auth_token=='default') params.auth_token=this.token;
123 params.format = 'json'; params.nojsoncallback=1;
124 var x = new XMLHttpRequest();
125 x.open("GET",this.api_call_url(params));
126 x.onreadystatechange=function() {
127 if(x.readyState!=4) return false;
128 if(x.status==200) {
129 var rsp=JSON.parse(x.responseText);
130 if(rsp.stat=='ok')
131 on_success && on_success(x,rsp);
132 else
133 on_failure && on_failure(rsp,rsp.stat,rsp.code,rsp.message);
134 }else
135 on_failure && on_failure(x);
136 return true;
137 };
138 x.send(null);
139 return true;
140 },
121 api_call: function(params, on_success, on_failure) { 141 api_call: function(params, on_success, on_failure) {
122 if(params.auth_token == 'default') 142 if(params.auth_token == 'default')
123 params.auth_token = this.token; 143 params.auth_token = this.token;
124 var x = new XMLHttpRequest(); 144 var x = new XMLHttpRequest();
125 x.open("GET",this.api_call_url(params)); 145 x.open("GET",this.api_call_url(params));
126 x.onreadystatechange=function() { 146 x.onreadystatechange=function() {
127 if(x.readyState!=4) return false; 147 if(x.readyState!=4) return false;
128 if(x.status==200) { 148 if(x.status==200) {
129 var stat = x.responseXML.firstChild.getAttribute('stat'); 149 var stat = x.responseXML.firstChild.getAttribute('stat');
130 if(stat=='ok') { 150 if(stat=='ok') {
131 if(on_success) on_success(x); 151 if(on_success) on_success(x);
132 }else{ 152 }else{
133 var e = x.responseXML.getElementsByTagName('err').item(0); 153 var e = x.responseXML.getElementsByTagName('err').item(0);
134 var ecode = e.getAttribute('code'); 154 var ecode = e.getAttribute('code');
135 var emsg = e.getAttribute('msg'); 155 var emsg = e.getAttribute('msg');
136 dump(params.method+' failed: '+ecode+' '+emsg+'\n'); 156 dump(params.method+' failed: '+ecode+' '+emsg+'\n');
137 if(on_failure) on_failure(x,stat,ecode,emsg); 157 if(on_failure) on_failure(x,stat,ecode,emsg);
138 } 158 }
139 }else{ 159 }else{
140 if(on_failure) on_failure(x); 160 if(on_failure) on_failure(x);
141 } 161 }
142 return true; 162 return true;
143 } 163 }
144 x.send(null); 164 x.send(null);
145 return true; 165 return true;
146 }, 166 },
147 167
148 frob: null, 168 frob: null,
149 authorize_0: function(perms, on_s, on_f) { 169 authorize_0: function(perms, on_s, on_f) {
150 var _this = this; 170 var _this = this;
151 this.api_call( 171 this.api_call(
152 { method: 'flickr.auth.getFrob' }, 172 { method: 'flickr.auth.getFrob' },
153 function(x) { 173 function(x) {
154 _this.frob = xp_str('/rsp/frob',x.responseXML); 174 _this.frob = xp_str('/rsp/frob',x.responseXML);
155 var u = _this.api_call_url( 175 var u = _this.api_call_url(
156 { frob: _this.frob, perms: perms?perms:'delete' }, _this.auth_url ); 176 { frob: _this.frob, perms: perms?perms:'delete' }, _this.auth_url );
157 if(on_s) on_s(x,_this.frob,u); 177 if(on_s) on_s(x,_this.frob,u);
158 }, function(x,s,c,m) { 178 }, function(x,s,c,m) {
159 if(on_f) on_f(x,s,c,m); 179 if(on_f) on_f(x,s,c,m);
160 } 180 }
161 ); 181 );
162 }, 182 },
163 token: null, 183 token: null,
164 perms: null, 184 perms: null,
165 user: null, 185 user: null,
166 authorize_1: function(on_s, on_f) { 186 authorize_1: function(on_s, on_f) {
167 var _this = this; 187 var _this = this;
168 this.api_call( 188 this.api_call(
169 { method: 'flickr.auth.getToken', frob: this.frob }, 189 { method: 'flickr.auth.getToken', frob: this.frob },
170 function(x) { 190 function(x) {
171 _this.token = xp_str('/rsp/auth/token',x.responseXML); 191 _this.token = xp_str('/rsp/auth/token',x.responseXML);
172 _this.perms = xp_str('/rsp/auth/perms',x.responseXML); 192 _this.perms = xp_str('/rsp/auth/perms',x.responseXML);
173 var u = xp_node('/rsp/auth/user',x.responseXML); 193 var u = xp_node('/rsp/auth/user',x.responseXML);
174 _this.user = { 194 _this.user = {
175 nsid: u.getAttribute('nsid'), 195 nsid: u.getAttribute('nsid'),
176 username: u.getAttribute('username'), 196 username: u.getAttribute('username'),
177 fullname: u.getAttribute('fullname') 197 fullname: u.getAttribute('fullname')
178 }; 198 };
179 if(on_s) on_s(x); 199 if(on_s) on_s(x);
180 }, function(x,s,c,m) { 200 }, function(x,s,c,m) {
181 if(on_f) on_f(x,s,c,m); 201 if(on_f) on_f(x,s,c,m);
182 } 202 }
183 ); 203 );
184 }, 204 },
185 205
186 prefs: Components.classes['@mozilla.org/preferences-service;1'].getService( 206 prefs: Components.classes['@mozilla.org/preferences-service;1'].getService(
187 Components.interfaces.nsIPrefBranch 207 Components.interfaces.nsIPrefBranch
188 ), 208 ),
189 prefs_root: 'net.klever.kin.flickr', 209 prefs_root: 'net.klever.kin.flickr',
190 save_token: function() { 210 save_token: function() {
191 // TODO: don't clear when there's nothing to clear or catch exceptions 211 // TODO: don't clear when there's nothing to clear or catch exceptions
192 if(this.token) 212 if(this.token)
193 this.prefs.setCharPref(this.prefs_root+'.auth_token',this.token); 213 this.prefs.setCharPref(this.prefs_root+'.auth_token',this.token);
194 else 214 else
195 this.prefs.clearUserPref(this.prefs_root+'.auth_token'); 215 this.prefs.clearUserPref(this.prefs_root+'.auth_token');
196 if(this.perms) 216 if(this.perms)
197 this.prefs.setCharPref(this.prefs_root+'.auth_perms',this.perms); 217 this.prefs.setCharPref(this.prefs_root+'.auth_perms',this.perms);
198 else 218 else
199 this.prefs.clearUserPref(this.prefs_root+'.auth_perms'); 219 this.prefs.clearUserPref(this.prefs_root+'.auth_perms');
200 if(this.user && this.user.nsid!=null && this.user.nsid!=undefined) 220 if(this.user && this.user.nsid!=null && this.user.nsid!=undefined)
201 this.prefs.setCharPref(this.prefs_root+'.auth_user.nsid',this.user.nsid); 221 this.prefs.setCharPref(this.prefs_root+'.auth_user.nsid',this.user.nsid);
202 else 222 else
203 this.prefs.clearUserPref(this.prefs_root+'.auth_user.nsid'); 223 this.prefs.clearUserPref(this.prefs_root+'.auth_user.nsid');
204 if(this.user && this.user.username!=null && this.user.username!=undefined) 224 if(this.user && this.user.username!=null && this.user.username!=undefined)
205 this.prefs.setCharPref(this.prefs_root+'.auth_user.username',this.user.username); 225 this.prefs.setCharPref(this.prefs_root+'.auth_user.username',this.user.username);
206 else 226 else
207 this.prefs.clearUserPref(this.prefs_root+'.auth_user.username'); 227 this.prefs.clearUserPref(this.prefs_root+'.auth_user.username');
208 if(this.user && this.user.fullname!=null && this.user.fullname!=undefined) 228 if(this.user && this.user.fullname!=null && this.user.fullname!=undefined)
209 this.prefs.setCharPref(this.prefs_root+'.auth_user.fullname',this.user.fullname); 229 this.prefs.setCharPref(this.prefs_root+'.auth_user.fullname',this.user.fullname);
210 else 230 else
211 this.prefs.clearUserPref(this.prefs_root+'.auth_user.fullname'); 231 this.prefs.clearUserPref(this.prefs_root+'.auth_user.fullname');
212 }, 232 },
213 _reset_token: function() { 233 _reset_token: function() {
214 this.token = null; this.perms = null; this.user = null; 234 this.token = null; this.perms = null; this.user = null;
215 return false; 235 return false;
216 }, 236 },
217 load_token: function() { 237 load_token: function() {
218 try { 238 try {
219 if(this.prefs.getPrefType(this.prefs_root+'.auth_token')!=this.prefs.PREF_STRING) 239 if(this.prefs.getPrefType(this.prefs_root+'.auth_token')!=this.prefs.PREF_STRING)
220 return this._reset_token(); 240 return this._reset_token();
221 this.token = this.prefs.getCharPref(this.prefs_root+'.auth_token'); 241 this.token = this.prefs.getCharPref(this.prefs_root+'.auth_token');
222 if(this.prefs.getPrefType(this.prefs_root+'.auth_perms')!=this.prefs.PREF_STRING) 242 if(this.prefs.getPrefType(this.prefs_root+'.auth_perms')!=this.prefs.PREF_STRING)
223 return this._reset_token(); 243 return this._reset_token();
224 this.perms = this.prefs.getCharPref(this.prefs_root+'.auth_perms'); 244 this.perms = this.prefs.getCharPref(this.prefs_root+'.auth_perms');
225 if(this.prefs.getPrefType(this.prefs_root+'.auth_user.nsid')!=this.prefs.PREF_STRING) 245 if(this.prefs.getPrefType(this.prefs_root+'.auth_user.nsid')!=this.prefs.PREF_STRING)
226 return this._reset_token(); 246 return this._reset_token();
227 this.user = new Object(); 247 this.user = new Object();
228 this.user.nsid = this.prefs.getCharPref(this.prefs_root+'.auth_user.nsid'); 248 this.user.nsid = this.prefs.getCharPref(this.prefs_root+'.auth_user.nsid');
229 if(this.prefs.getPrefType(this.prefs_root+'.auth_user.username')!=this.prefs.PREF_STRING) 249 if(this.prefs.getPrefType(this.prefs_root+'.auth_user.username')!=this.prefs.PREF_STRING)
230 return this._reset_token(); 250 return this._reset_token();
231 this.user.username = this.prefs.getCharPref(this.prefs_root+'.auth_user.username'); 251 this.user.username = this.prefs.getCharPref(this.prefs_root+'.auth_user.username');
232 if(this.prefs.getPrefType(this.prefs_root+'.auth_user.fullname')!=this.prefs.PREF_STRING) 252 if(this.prefs.getPrefType(this.prefs_root+'.auth_user.fullname')!=this.prefs.PREF_STRING)
233 return this._reset_token(); 253 return this._reset_token();
234 this.user.fullname = this.prefs.getCharPref(this.prefs_root+'.auth_user.fullname'); 254 this.user.fullname = this.prefs.getCharPref(this.prefs_root+'.auth_user.fullname');
235 }catch(e) { return this._reset_token(); } 255 }catch(e) { return this._reset_token(); }
236 return true; 256 return true;
237 }, 257 },
238 reset_token: function() { 258 reset_token: function() {
239 this._reset_token(); 259 this._reset_token();
240 this.save_token(); 260 this.save_token();
241 }, 261 },
242 262
243 get_photo_url: function(ser,id,sec,sfx,ext) { 263 get_photo_url: function(ser,id,sec,sfx,ext) {
244 var rv = this.photo_url + ser + '/' + id + '_' + sec; 264 var rv = this.photo_url + ser + '/' + id + '_' + sec;
245 if(sfx && sfx!='_') rv += '_'+sfx; 265 if(sfx && sfx!='_') rv += '_'+sfx;
246 rv += ext?'.'+ext:'.jpg'; 266 rv += ext?'.'+ext:'.jpg';
247 return rv; 267 return rv;
248 }, 268 },
249 get_image_url: function(o,sfx) { 269 get_image_url: function(o,sfx) {
250 return this.get_photo_url( 270 return this.get_photo_url(
251 o.server, 271 o.server,
252 (o instanceof Photoset)? o.primary : o.id, 272 (o instanceof Photoset)? o.primary : o.id,
253 o.secret, 273 o.secret,
254 sfx, 274 sfx,
255 (sfx=='o')?o.originalformat:null 275 (sfx=='o')?o.originalformat:null
256 ); 276 );
257 }, 277 },
258 get_photo_page_url: function(p) { 278 get_photo_page_url: function(p) {
259 if(p instanceof Photo) { 279 if(p instanceof Photo) {
260 // TODO: track photoset and user owner id from there? 280 // TODO: track photoset and user owner id from there?
261 // The approach below is sheerly wrong. 281 // The approach below is sheerly wrong.
262 var o = this.user.nsid; 282 var o = this.user.nsid;
263 if(p.owner && p.owner.nsid) o = p.owner.nsid; 283 if(p.owner && p.owner.nsid) o = p.owner.nsid;
264 var rv = this.photos_url + o +'/' + p.id; 284 var rv = this.photos_url + o +'/' + p.id;
265 return rv; 285 return rv;
266 }else // TODO: take owner into account? 286 }else // TODO: take owner into account?
267 return this.photos_url + this.user.nsid + '/' + p; 287 return this.photos_url + this.user.nsid + '/' + p;
268 }, 288 },
269 make_photo_url: function(p,sfx) { 289 make_photo_url: function(p,sfx) {
270 if(sfx=='p') 290 if(sfx=='p')
271 return this.get_photo_page_url(p); 291 return this.get_photo_page_url(p);
272 else 292 else
273 return this.get_image_url(p,sfx); 293 return this.get_image_url(p,sfx);
274 }, 294 },
275 make_photoset_url: function(ps) { 295 make_photoset_url: function(ps) {
276 // TODO: allow for using someone else's photoset? 296 // TODO: allow for using someone else's photoset?
277 return this.photos_url+this.user.nsid+'/sets/'+ps.id; 297 return this.photos_url+this.user.nsid+'/sets/'+ps.id;
278 }, 298 },
279 make_uploader_edit_url: function(pid) { 299 make_uploader_edit_url: function(pid) {
280 // TODO: handle arrays 300 // TODO: handle arrays
281 return this.uploader_edit_url+'?ids='+pid; 301 return this.uploader_edit_url+'?ids='+pid;
282 }, 302 },
283 303
284 upload_file: function(f,fa,on_success,on_failure) { 304 upload_file: function(f,fa,on_success,on_failure) {
285 try { 305 try {
286 var fi = Components.classes["@mozilla.org/file/local;1"] 306 var fi = Components.classes["@mozilla.org/file/local;1"]
287 .createInstance(Components.interfaces.nsILocalFile); 307 .createInstance(Components.interfaces.nsILocalFile);
288 fi.initWithPath( f ); 308 fi.initWithPath( f );
289 var st = Components.classes["@mozilla.org/network/file-input-stream;1"] 309 var st = Components.classes["@mozilla.org/network/file-input-stream;1"]
290 .createInstance(Components.interfaces.nsIFileInputStream); 310 .createInstance(Components.interfaces.nsIFileInputStream);
291 st.init(fi,0x01,00004,null); 311 st.init(fi,0x01,00004,null);
292 var bis = Components.classes["@mozilla.org/binaryinputstream;1"] 312 var bis = Components.classes["@mozilla.org/binaryinputstream;1"]
293 .createInstance(Components.interfaces.nsIBinaryInputStream); 313 .createInstance(Components.interfaces.nsIBinaryInputStream);
294 bis.setInputStream(st); 314 bis.setInputStream(st);
295 315
296 // allocate and initialize temp storage string 316 // allocate and initialize temp storage string
297 var pbs = Components.classes["@mozilla.org/storagestream;1"] 317 var pbs = Components.classes["@mozilla.org/storagestream;1"]
298 .createInstance(Components.interfaces.nsIStorageStream); 318 .createInstance(Components.interfaces.nsIStorageStream);
299 pbs.init(1024,10000000,null); 319 pbs.init(1024,10000000,null);
300 // create output stream 320 // create output stream
301 var pbos = pbs.getOutputStream(0); 321 var pbos = pbs.getOutputStream(0);
302 // and a binaryoutputstream interface 322 // and a binaryoutputstream interface
303 var pbbos = Components.classes["@mozilla.org/binaryoutputstream;1"] 323 var pbbos = Components.classes["@mozilla.org/binaryoutputstream;1"]
304 .createInstance(Components.interfaces.nsIBinaryOutputStream); 324 .createInstance(Components.interfaces.nsIBinaryOutputStream);
305 pbbos.setOutputStream(pbos); 325 pbbos.setOutputStream(pbos);
306 326
307 /* create POST body */ 327 /* create POST body */
308 var boundarytoken = 'kadaroloongazaduviaxamma'; 328 var boundarytoken = 'kadaroloongazaduviaxamma';
309 var boundary = '--'+boundarytoken; 329 var boundary = '--'+boundarytoken;
310 var b = ''; 330 var b = '';
311 331
312 var parms = { api_key: this.api_key, auth_token: this.token }; 332 var parms = { api_key: this.api_key, auth_token: this.token };
313 for(var p in fa) parms[p] = fa[p]; 333 for(var p in fa) parms[p] = fa[p];
314 var pns = new Array(); 334 var pns = new Array();
315 for(var p in parms) pns.push(p); 335 for(var p in parms) pns.push(p);
316 var pstr = ''; 336 var pstr = '';
317 for(var p in pns.sort()) { 337 for(var p in pns.sort()) {
318 var pn = pns[p]; 338 var pn = pns[p];
319 pstr += pn+parms[pn]; 339 pstr += pn+parms[pn];
320 b += boundary+'\nContent-Disposition: form-data; name="'+pn+'"\n\n'+toutf8(parms[pn])+'\n'; 340 b += boundary+'\nContent-Disposition: form-data; name="'+pn+'"\n\n'+toutf8(parms[pn])+'\n';
321 } 341 }
322 b += boundary+'\nContent-Disposition: form-data; name="api_sig"\n\n'+this.api_sig(pstr)+'\n'; 342 b += boundary+'\nContent-Disposition: form-data; name="api_sig"\n\n'+this.api_sig(pstr)+'\n';
323 b += boundary+'\nContent-Disposition: form-data; name="photo"; filename="'+f+'"\nContent-Type: image/jpeg\nContent-Transfer-Encoding: binary\n\n'; 343 b += boundary+'\nContent-Disposition: form-data; name="photo"; filename="'+f+'"\nContent-Type: image/jpeg\nContent-Transfer-Encoding: binary\n\n';
324 pbbos.writeBytes(b,b.length); 344 pbbos.writeBytes(b,b.length);
325 var bisbytes = bis.available(); 345 var bisbytes = bis.available();
326 pbbos.writeBytes(bis.readBytes(bisbytes),bisbytes); 346 pbbos.writeBytes(bis.readBytes(bisbytes),bisbytes);
327 pbbos.writeBytes('\n'+boundary+'--',3+boundary.length); bis.close(); st.close(); 347 pbbos.writeBytes('\n'+boundary+'--',3+boundary.length); bis.close(); st.close();
328 348
329 pbbos.close(); pbos.close(); 349 pbbos.close(); pbos.close();
330 350
331 var x = new XMLHttpRequest(); 351 var x = new XMLHttpRequest();
332 x.open("POST",this.upload_url); 352 x.open("POST",this.upload_url);
333 x.setRequestHeader('Content-Type', 'multipart/form-data; boundary="'+boundarytoken+'"'); 353 x.setRequestHeader('Content-Type', 'multipart/form-data; boundary="'+boundarytoken+'"');
334 x.setRequestHeader('Connection','close'); 354 x.setRequestHeader('Connection','close');
335 x.setRequestHeader('Content-Length',b.length); 355 x.setRequestHeader('Content-Length',b.length);
336 x.onreadystatechange=function() { 356 x.onreadystatechange=function() {
337 if(x.readyState!=4) return false; 357 if(x.readyState!=4) return false;
338 if(x.status==200) { 358 if(x.status==200) {
339 var stat = x.responseXML.firstChild.getAttribute('stat'); 359 var stat = x.responseXML.firstChild.getAttribute('stat');
340 if(stat=='ok') { 360 if(stat=='ok') {
341 var pid = xp_str('/rsp/photoid',x.responseXML); 361 var pid = xp_str('/rsp/photoid',x.responseXML);
342 if(on_success) on_success(x,pid); 362 if(on_success) on_success(x,pid);
343 }else{ 363 }else{
344 var e = x.responseXML.getElementsByTagName('err').item(0); 364 var e = x.responseXML.getElementsByTagName('err').item(0);
345 var ecode = e.getAttribute('code'); 365 var ecode = e.getAttribute('code');
346 var emsg = e.getAttribute('msg'); 366 var emsg = e.getAttribute('msg');
347 dump('upload failed: '+ecode+' '+emsg+'\n'); 367 dump('upload failed: '+ecode+' '+emsg+'\n');
348 if(on_failure) on_failure(x,stat,ecode,emsg); 368 if(on_failure) on_failure(x,stat,ecode,emsg);
349 } 369 }
350 }else{ 370 }else{
351 if(on_failure) on_failure(x); 371 if(on_failure) on_failure(x);
352 } 372 }
353 return true; 373 return true;
354 }; 374 };
355 x.send(pbs.newInputStream(0)); 375 x.send(pbs.newInputStream(0));
356 }catch(e) { 376 }catch(e) {
357 if(on_failure) on_failure(e,null,-1,e.message); 377 if(on_failure) on_failure(e,null,-1,e.message);
358 } 378 }
359 } 379 }
360 380
361}; 381};