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