ftp.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. try { module.exports = {ftp:ftp}; } catch (e) { }
  2. // N2O File Transfer Protocol
  3. var ftp = {
  4. queue: [],
  5. init: function(file) {
  6. var item = {
  7. id: performance.now().toString(),
  8. status: 'init',
  9. status_block_id: ftp.status_block_id || 'ftp_status',
  10. autostart: ftp.autostart || false,
  11. name: ftp.filename || file.name,
  12. sid: ftp.sid || co(session),
  13. meta: ftp.meta || bin(''),
  14. other1: ftp.other1 || bin(''),
  15. other2: ftp.other2 || bin(''),
  16. other3: ftp.other3 || bin(''),
  17. offset: ftp.offset || 0,
  18. block: 1,
  19. total: file.size,
  20. file: file
  21. };
  22. ftp.queue.push(item);
  23. ftp.send(item, '', 1);
  24. return item.id;
  25. },
  26. start: function(id) {
  27. if(ftp.active) { id && ( ftp.item(id).autostart = true ); return false; }
  28. var item = id ? ftp.item(id) : ftp.next();
  29. if(item) { ftp.active = true; ftp.send_slice(item); }
  30. },
  31. stop: function(id) {
  32. var item = ftp.item(id);
  33. var index = ftp.queue.indexOf(item);
  34. ftp.queue.splice(index, 1);
  35. ftp.active = false;
  36. ftp.start();
  37. },
  38. send: function(item, data) {
  39. //console.log('item ', item);
  40. //console.log('data ', data);
  41. ws.send(enc(tuple(atom('ftp'),
  42. bin(item.id),
  43. bin(item.sid),
  44. bin(item.name),
  45. item.meta,
  46. item.other1,
  47. item.other2,
  48. item.other3,
  49. number(item.total),
  50. number(item.offset),
  51. number(item.block || data.byteLength),
  52. bin(data),
  53. bin(item.status || 'send')
  54. ))); },
  55. send_slice: function(item) {
  56. this.reader = new FileReader();
  57. this.reader.onloadend = function(e) {
  58. var res = e.target, data = e.target.result;
  59. if(res.readyState === FileReader.DONE && data.byteLength > 0) ftp.send(item,data);
  60. };
  61. this.reader.readAsArrayBuffer(item.file.slice(item.offset, item.offset + item.block)); },
  62. item: function(id) { return ftp.queue.find(function(item){ return item && item.id === id; }); },
  63. next: function() { return ftp.queue.find(function(next){ return next && next.autostart }); }
  64. };
  65. $file.do = function(rsp) {
  66. //console.log('rsp ', rsp);
  67. var offset = rsp.v[6].v, block = rsp.v[7].v, status = utf8_dec(rsp.v[9].v);
  68. switch (status) {
  69. case 'init':
  70. var item = ftp.item(utf8_dec(rsp.v[1].v));
  71. item.offset = offset;
  72. item.block = block;
  73. item.name = utf8_dec(rsp.v[3].v);
  74. item.status = undefined;
  75. if(item.autostart) ftp.start(item.id);
  76. break;
  77. case 'send':
  78. var item = ftp.item(utf8_dec(rsp.v[1].v));
  79. var x = qi(item.status_block_id); if(x) x.innerHTML = offset;
  80. item.offset = offset;
  81. item.block = block;
  82. (block > 0 && ftp.active) ? ftp.send_slice(item) : ftp.stop(item.id)
  83. break;
  84. case 'relay': if (typeof ftp.relay === 'function') ftp.relay(rsp); break;
  85. }
  86. };