webdav.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. var webdav={};
  2. webdav.set=
  3. function(parentClass){
  4. this.parent=parentClass;
  5. }
  6. webdav.uploadFile=
  7. function(file,context){
  8. let url=LABKEY.ActionURL.getBaseURL();
  9. url+='_webdav';
  10. url+=LABKEY.ActionURL.getContainer();
  11. url+='/@files';
  12. url+='/'+context['dirName'];
  13. this.parent.print('uploadFile url: '+url);
  14. let uploadConfig=new Object();
  15. uploadConfig.file=file;
  16. uploadConfig.context=context;
  17. uploadConfig.url=url;
  18. let that=this;
  19. uploadConfig.success=function(cfg){that.afterBaseDir(cfg);};
  20. uploadConfig.failure=function(cfg){that.tryMakeDir(cfg);};
  21. this.webdavCheck(uploadConfig);
  22. }
  23. webdav.afterBaseDir=
  24. function(cfg){
  25. this.parent.print('afterBaseDir');
  26. cfg.url+='/'+cfg.context['ID'];
  27. let that=this;
  28. cfg.success=function(x){that.afterIDDir(x);};
  29. cfg.failure=function(x){that.tryMakeDir(x);};
  30. this.webdavCheck(cfg);
  31. }
  32. webdav.afterIDDir=
  33. function(cfg){
  34. this.parent.print('afterIDDir');
  35. this.parent.print('Uploading '+cfg.file.name);
  36. let suf=cfg.file.name.split('.').pop();
  37. cfg.url+='/'+cfg.context['ID']+'.'+suf;
  38. cfg.data=cfg.file;
  39. let that=this;
  40. cfg.success=function(x){that.afterUpload(x);};
  41. cfg.failure=function(x){that.onFailure(x);};
  42. this.webdavPut(cfg);
  43. }
  44. webdav.afterUpload=
  45. function(cfg){
  46. this.parent.print('afterUpload');
  47. }
  48. webdav.tryMakeDir=
  49. function(cfg){
  50. this.parent.print('tryMakeDir '+cfg.url);
  51. let that=this;
  52. cfg.failure=function(x){that.onFailure(x);};
  53. this.webdavMakeDir(cfg);
  54. }
  55. webdav.request=
  56. function (cfg,verb,data){
  57. this.parent.print('request['+verb+'] '+cfg.url);
  58. let connRequest=new XMLHttpRequest();
  59. let that=this;
  60. let action=function(connRequest,cfg){that.checkResponse(connRequest,cfg);};
  61. connRequest.addEventListener("loadend",action);
  62. connRequest.open(verb, cfg.url);
  63. connRequest.send(data);
  64. //this.print('request['+verb+'] sent');
  65. }
  66. webdav.checkResponse=
  67. function(xrq,cfg){
  68. //this.print('checkResponse: readyState '+xrq.readyState);
  69. //this.print('checkResponse: status '+xrq.status);
  70. if (xrq.status<400) {
  71. //client errors 400-499
  72. //server errors 500-599
  73. cfg.success(cfg);
  74. return;
  75. }
  76. cfg.status=xrq.status;
  77. cfg.failure(cfg);
  78. }
  79. webdav.webdavMakeDir=function(cfg){ this.request(cfg,'MKCOL',null);}
  80. webdav.webdavCheck=function(cfg) { this.request(cfg,'GET',null);}
  81. webdav.webdavPut=function(cfg) { this.request(cfg,'PUT',cfg.data);}
  82. webdav.onFailuer=function(cfg){
  83. this.parent.print('request failed with status='+cfg.status);
  84. }