updown.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. upload.modules.addmodule({
  2. name: 'updown',
  3. init: function () {
  4. // We do this to try to hide the fragment from the referral in IE
  5. this.requestframe = document.createElement('iframe')
  6. this.requestframe.src = 'about:blank'
  7. this.requestframe.style.visibility = 'hidden'
  8. document.body.appendChild(this.requestframe)
  9. },
  10. downloadfromident: function(seed, progress, done, ident) {
  11. var filename = ident.ident ;
  12. //console.log ( 'download from ident : filename : ', filename );
  13. var name = filename,
  14. serviceName = "safeshare_"+filename,
  15. filePath = filename;
  16. options = {};
  17. Safe.dns.getFile(serviceName, name, filePath, options).then(function(file) {
  18. //console.log ( 'downloaded :' , file );
  19. var u8 = StringView.base64ToBytes( file.body )
  20. var filebody = [ u8 ];
  21. //console.log ( 'downloaded :' , filebody, typeof(filebody) );
  22. //console.log ( 'size :' , filebody.length );
  23. upload.updown.downloaded(seed, progress, done, filebody )
  24. }, addErrorreadservice.bind(null, filename));
  25. },
  26. onerror: function(progress) {
  27. progress('error')
  28. },
  29. downloaded: function (seed, progress, done, response) {
  30. //console.log ( 'downloaded > response : ', response, typeof ( response ) );
  31. //if (response.target.status != 200) { TODO : handle errors here
  32. if ( false ) {
  33. this.onerror(progress)
  34. } else {
  35. this.cache(seed, response)
  36. progress('decrypting')
  37. var blob = new Blob( response, { type: 'application/octet-stream' })
  38. crypt.decrypt(blob, seed).done(done)
  39. }
  40. },
  41. encrypted: function(progress, whendone, data) {
  42. var filename = data.ident;
  43. var reader = new FileReader();
  44. var content = data.encrypted;
  45. var b64encoded = StringView.bytesToBase64( content )
  46. //console.log ( 'filename', filename );
  47. createservice ( filename ).done (createfile( filename )).done(writetofile ( filename, b64encoded, whendone, data )) // whendone : upload.home.uploaded
  48. },
  49. cache: function(seed, data) {
  50. this.cached = data
  51. this.cached_seed = seed
  52. //console.log ( 'cached');
  53. },
  54. cacheresult: function(data) {
  55. //console.log ( 'caching');
  56. //console.log ( 'cacheresult > data.encrypted : ', data.encrypted , typeof ( data.encrypted ) );
  57. this.cache(data.seed, [data.encrypted] )
  58. },
  59. download: function (seed, progress, done) {
  60. if (this.cached_seed == seed) {
  61. progress('decrypting');
  62. //console.log ( 'download > this.cached : ', this.cached , typeof ( this.cached ) );
  63. var blob = new Blob ( this.cached , { type: 'application/octet-stream' });
  64. crypt.decrypt(blob, seed).done(done).progress(progress)
  65. } else {
  66. crypt.ident(seed).done(this.downloadfromident.bind(this, seed, progress, done))
  67. }
  68. },
  69. upload: function (blob, progress, whendone) {
  70. //console.log ( "blob : ", blob );
  71. crypt.encrypt(blob)
  72. .done(
  73. this.encrypted.bind(this, progress, whendone) // whendone : upload.home.uploaded.bind(this)
  74. )
  75. .done( this.cacheresult.bind(this) ).progress(progress);
  76. }
  77. })