1. /**
  2. * davfs.js - WebDAV settings and XHTTPRequest object Management API and
  3. * XHTTPRequest High-level WebDAV Client API.
  4. *
  5. * This is a Manager of WebDAV and XHTTPRequest for File system.
  6. * and a High-level WebDAV client API for File system.
  7. * Path base request.
  8. * Resource base request: @see xhrdav.ResourceController
  9. *
  10. * @license Copyright 2011 The xhrdavclient library authors.
  11. * All rights reserved.
  12. */
  13.  
  14. goog.provide('xhrdav.DavFs');
  15. goog.provide('xhrdav.DavFs.Request');
  16. goog.require('goog.net.XhrManager');
  17. goog.require('xhrdav.Client');
  18. goog.require('xhrdav.Conf');
  19. goog.require('xhrdav.Errors');
  20. goog.require('xhrdav.ResourceBuilder');
  21. goog.require('xhrdav.ResourceController');
  22.  
  23.  
  24. /**
  25. * high-level WebDAV client API Singleton
  26. *
  27. * @constructor
  28. */
  29. xhrdav.DavFs = function() {
  30. /**
  31. * @private
  32. * @type {goog.net.XhrManager}
  33. */
  34. this.xhrMgr_ = null;
  35.  
  36. /**
  37. * @private
  38. * @type {Object.<string,xhrdav.Client>}
  39. */
  40. this.clients_ = {};
  41.  
  42. this.initXhrMgr_();
  43. this.addConnection();
  44. };
  45. goog.addSingletonGetter(xhrdav.DavFs);
  46.  
  47. /** @type {string} */
  48. xhrdav.DavFs.DEFAULT_DAV_SITE_NAME = 'default';
  49.  
  50. /**
  51. * Init XhrManager with config.
  52. *
  53. * @private
  54. */
  55. xhrdav.DavFs.prototype.initXhrMgr_ = function() {
  56. var config = xhrdav.Conf.getInstance();
  57. var configXhrMgr = config.getXhrMgrConfig();
  58.  
  59. if (goog.isDefAndNotNull(configXhrMgr) &&
  60. !goog.object.isEmpty(configXhrMgr)) {
  61. this.xhrMgr_ = new goog.net.XhrManager(
  62. configXhrMgr.maxRetries || 1,
  63. configXhrMgr.headers || {},
  64. configXhrMgr.minCount || 1,
  65. configXhrMgr.maxCount || 10,
  66. configXhrMgr.timeoutInterval || 0);
  67. } else {
  68. this.xhrMgr_ = new goog.net.XhrManager();
  69. }
  70. };
  71.  
  72. /**
  73. * Get XhrManager of davfs for monitoring, progress, abort, etc.
  74. *
  75. * Example:
  76. * var davFs = xhrdav.DavFs.getInstance();
  77. * var xhrMgr = davFs.getXhrManager();
  78. *
  79. * var onComplete = function(file, e) {
  80. * console.log('Uploaded ' + file.name); };
  81. *
  82. * var delay = new goog.async.ConditionalDelay(function() {
  83. * var sendingCount = xhrMgr.getOutstandingCount();
  84. * console.log('Sending... ' + sendingCount);
  85. * return (sendingCount == 0); });
  86. * delay.onSuccess = function() { alert('Update files on completely!!'); };
  87. * delay.onFailure = function() {
  88. * alert('Failed to upload files by timeout'); };
  89. *
  90. * delay.start(500, 5000);
  91. * goog.array.forEach(files, function(file, i) {
  92. * fs.upload(WEBDAV_ROOT + file.name, file,
  93. * goog.bind(onComplete, this, file), null, null, this);
  94. * }
  95. *
  96. * @return {goog.net.XhrManager} XhrManager object.
  97. */
  98. xhrdav.DavFs.prototype.getXhrManager = function() {
  99. return this.xhrMgr_;
  100. };
  101.  
  102. /**
  103. * Setter XhrManager
  104. *
  105. * @param {goog.net.XhrManager} xhrMgr XhrManager object.
  106. * @see goog.net.XhrManager
  107. */
  108. xhrdav.DavFs.prototype.setXhrManager = function(xhrMgr) {
  109. if (xhrMgr && xhrMgr instanceof goog.net.XhrManager) {
  110. this.xhrMgr_ = xhrMgr;
  111. }
  112. };
  113.  
  114. /**
  115. * Create WebDAV client object.
  116. *
  117. * @private
  118. * @param {{scheme:string=, domain:stirng=, port:nubmer=}=} opt_uri
  119. * davclient Parameters(opt_uri: scheme, domain, port)
  120. * @param {string} site Settings name of WebDAV site.
  121. */
  122. xhrdav.DavFs.prototype.createClient_ = function(opt_uri, site) {
  123. var config = xhrdav.Conf.getInstance();
  124. var client = new xhrdav.Client(opt_uri);
  125. client.setXmlParseFunction(goog.getObjectByName(config.xmlParseFuncObj));
  126. goog.object.set(this.clients_, site, client);
  127. };
  128.  
  129. /**
  130. * Get and Create Connection xhrdav.Client.
  131. *
  132. * @param {string=} opt_davSiteName Any settings name of WebDAV site.
  133. * @return {xhrdav.Client} WebDAV Client connection object.
  134. */
  135. xhrdav.DavFs.prototype.getConnection = function(opt_davSiteName) {
  136. if (goog.string.isEmptySafe(opt_davSiteName)) {
  137. opt_davSiteName = xhrdav.DavFs.DEFAULT_DAV_SITE_NAME;
  138. }
  139. return this.clients_[opt_davSiteName];
  140. };
  141.  
  142. /**
  143. * Add WebDAV connection setting(For multiple WebDAV root)
  144. *
  145. * If connection exists, overwrite WebDAV site settings.
  146. *
  147. * @param {{scheme:string=, domain:string=, port:number=}=} opt_uri
  148. * davclient Parameters(opt_uri: scheme, domain, port)
  149. * @param {string=} opt_davSiteName Any settings name of WebDAV site.
  150. */
  151. xhrdav.DavFs.prototype.addConnection = function(opt_uri, opt_davSiteName) {
  152. if (goog.string.isEmptySafe(opt_davSiteName)) {
  153. opt_davSiteName = xhrdav.DavFs.DEFAULT_DAV_SITE_NAME;
  154. }
  155. this.createClient_(opt_uri, opt_davSiteName);
  156. };
  157.  
  158. /**
  159. * Get Request object for WebDAV request.
  160. *
  161. * @param {{davSiteName:string=, xhrIo:(goog.net.XhrIo|goog.net.XhrManager)=,
  162. * auth:string=, authOverwrite:boolean=}} options
  163. * davSiteName Any settings name of WebDAV site.
  164. * xhrIo request object of closure library
  165. * (For Cross-site resource sharing[CORS]).
  166. * auth: authorization credentials.
  167. * authOverwrite: overwrite flag for auth credentials.
  168. * @return {xhrdav.DavFs.Request} WebDAV Fs Request object.
  169. * @see xhrdav.DavFs.Request
  170. */
  171. xhrdav.DavFs.prototype.getRequest = function(options) {
  172. if (!goog.isDefAndNotNull(options)) options = {};
  173. if (goog.string.isEmptySafe(options.davSiteName)) {
  174. options.davSiteName = xhrdav.DavFs.DEFAULT_DAV_SITE_NAME;
  175. }
  176. var davSite = this.getConnection(options.davSiteName);
  177. if (!goog.string.isEmptySafe(options.auth)) {
  178. if (!!options.authOverwrite || !davSite.hasAuthCredentials()) {
  179. davSite.setAuthCredentials(options.auth);
  180. }
  181. }
  182.  
  183. if (!goog.isDefAndNotNull(options.xhrIo) ||
  184. !(options.xhrIo instanceof goog.net.XhrIo ||
  185. options.xhrIo instanceof goog.net.XhrManager)) {
  186. options.xhrIo = this.xhrMgr_;
  187. }
  188.  
  189. return new xhrdav.DavFs.Request(davSite, options.xhrIo);
  190. };
  191.  
  192.  
  193. /**
  194. * An encapsulation of everything needed to make a DavFs request.
  195. *
  196. *
  197. * @constructor
  198. * @param {xhrdav.Client} davSite WebDAV site.
  199. * @param {(goog.net.XhrIo|goog.net.XhrManager)} xhrIo request object
  200. * of closure library (For Cross-site resource sharing[CORS]).
  201. */
  202. xhrdav.DavFs.Request = function(davSite, xhrIo) {
  203. /**
  204. * @private
  205. * @type {xhrdav.Client}
  206. */
  207. this.davSite_ = davSite;
  208.  
  209. /**
  210. * @private
  211. * @type {(goog.net.XhrIo|goog.net.XhrManager)}
  212. */
  213. this.xhrIo_ = xhrIo;
  214. };
  215.  
  216. /**
  217. * WebDAV Response process handler(callback)
  218. *
  219. * @private
  220. * @param {Function} handler callback client.
  221. * @param {Function} processHandler callback process response handler.
  222. * @param {string} path Request path.
  223. * @param {*} context callback function scope.
  224. * @param {number} statusCode HTTP Status code.
  225. * @param {Object} content Response body data.
  226. * @param {Object} headers Response headers.
  227. */
  228. xhrdav.DavFs.Request.prototype.responseHandler_ = function(
  229. handler, processHandler, path, context, statusCode, content, headers) {
  230. var httpStatus = xhrdav.HttpStatus;
  231. var args = processHandler(path, statusCode, content, headers);
  232. if (goog.isDefAndNotNull(context) && goog.isObject(context)) {
  233. handler.apply(context, args);
  234. } else {
  235. handler(args);
  236. }
  237. };
  238.  
  239. /**
  240. * Content read Handler
  241. *
  242. * @private
  243. * @param {string} path HTTP Request path.
  244. * @param {number} statusCode HTTP Status code.
  245. * @param {Object} content Response body data.
  246. * @param {Object} headers Response headers.
  247. * @return {Array.<xhrdav.Errors, string=>} Errors, new Location.
  248. * @see xhrdav.Errors
  249. */
  250. xhrdav.DavFs.Request.prototype.contentReadHandler_ = function(
  251. path, statusCode, content, headers) {
  252. var config = xhrdav.Conf.getInstance();
  253. var httpStatus = xhrdav.HttpStatus;
  254. var errors = new xhrdav.Errors();
  255.  
  256. var args = [];
  257. if (statusCode != httpStatus.OK) {
  258. var data = {statusCode: statusCode, path: path, content: content};
  259. errors.setRequest(xhrdav.DavFs.Request.buildRequestErrors(data));
  260. }
  261. args.push(errors);
  262. args.push(content);
  263.  
  264. return args;
  265. };
  266.  
  267. /**
  268. * Exists Handler
  269. *
  270. * @private
  271. * @param {string} path HTTP Request path.
  272. * @param {number} statusCode HTTP Status code.
  273. * @param {Object} content Response body data.
  274. * @param {Object} headers Response headers.
  275. * @return {Array.<xhrdav.Errors, boolean>} Errors, new Location.
  276. * @see xhrdav.Errors
  277. */
  278. xhrdav.DavFs.Request.prototype.existsHandler_ = function(
  279. path, statusCode, content, headers) {
  280. var config = xhrdav.Conf.getInstance();
  281. var httpStatus = xhrdav.HttpStatus;
  282. var errors = new xhrdav.Errors();
  283.  
  284. var args = [];
  285. if (!goog.array.contains(
  286. [httpStatus.OK, httpStatus.CREATED, httpStatus.NO_CONTENT],
  287. statusCode)) {
  288. var data = {statusCode: statusCode, path: path, content: content};
  289. errors.setRequest(xhrdav.DavFs.Request.buildRequestErrors(data));
  290. }
  291. args.push(errors);
  292. args.push(errors.hasRequest() ? false : true);
  293. return args;
  294. };
  295.  
  296. /**
  297. * Error Handler
  298. *
  299. * Errors object structure:
  300. * {status: <HTTP status code>, path: <request path>,
  301. * summary: <Request error summary message>,
  302. * message: <Request error detail message>}
  303. *
  304. * @private
  305. * @param {string} path HTTP Request path.
  306. * @param {number} statusCode HTTP Status code.
  307. * @param {Object} content Response body data.
  308. * @param {Object} headers Response headers.
  309. * @return {Array.<xhrdav.Errors, string=>} Errors, new Location.
  310. * @see xhrdav.Errors
  311. */
  312. xhrdav.DavFs.Request.prototype.simpleErrorHandler_ = function(
  313. path, statusCode, content, headers) {
  314. var config = xhrdav.Conf.getInstance();
  315. var httpStatus = xhrdav.HttpStatus;
  316. var errors = new xhrdav.Errors();
  317.  
  318. var args = [];
  319. if (!goog.array.contains(
  320. [httpStatus.OK, httpStatus.CREATED, httpStatus.NO_CONTENT],
  321. statusCode)) {
  322. var data = {statusCode: statusCode, path: path, content: content};
  323. errors.setRequest(xhrdav.DavFs.Request.buildRequestErrors(data));
  324. }
  325. args.push(errors);
  326. if (statusCode == httpStatus.CREATED) {
  327. args.push(headers['Location']);
  328. }
  329. return args;
  330. };
  331.  
  332. /**
  333. * Building request errors
  334. *
  335. * @param {{statusCode: number, path: string, content: string=}} data
  336. * Response data.
  337. * @return {{status: number, path: string, html: string,
  338. * summary: string=, message: string=}} errors Map data.
  339. */
  340. xhrdav.DavFs.Request.buildRequestErrors = function(data) {
  341. var httpStatusText = xhrdav.HttpStatus.text;
  342. var errMap = {};
  343.  
  344. var errorHtmlDom = !goog.string.isEmptySafe(data.content) ?
  345. goog.dom.htmlToDocumentFragment(data.content) : null;
  346. var summary = null, description = null;
  347. if (goog.isDefAndNotNull(errorHtmlDom)) {
  348. summary = goog.dom.getElementsByTagNameAndClass(
  349. 'title', null, errorHtmlDom)[0];
  350. description = goog.dom.getElementsByTagNameAndClass(
  351. 'p', null, errorHtmlDom)[0];
  352. }
  353. goog.object.extend(errMap, {
  354. summary: goog.isDefAndNotNull(summary) ?
  355. goog.dom.getTextContent(summary) :
  356. data.statusCode + ' ' + httpStatusText[data.statusCode],
  357. message: goog.isDefAndNotNull(description) ?
  358. goog.dom.getTextContent(description) :
  359. data.statusCode + ' ' + httpStatusText[data.statusCode]
  360. });
  361.  
  362. goog.object.extend(errMap, {
  363. status: data.statusCode,
  364. path: data.path,
  365. html: data.content});
  366.  
  367. return errMap;
  368. };
  369.  
  370. /**
  371. * Build Directory tree from multistatus
  372. *
  373. * @param {Object} content multistatus response data.
  374. * @param {Object=} opt_helper Returning resource controller.
  375. * @return {(xhrdav.Resource|xhrdav.ResourceController|Object)}
  376. * converted object for WebDAV resources.
  377. * @see xhrdav.ResourceBuilder.createCollection
  378. */
  379. xhrdav.DavFs.Request.prototype.getListDirFromMultistatus = function(
  380. content, opt_helper) {
  381. if (!goog.isDefAndNotNull(opt_helper)) opt_helper = {};
  382. var builder = xhrdav.ResourceBuilder.createCollection(content);
  383. var resources;
  384. if (!!opt_helper.hasCtrl) {
  385. resources = builder.getResources();
  386. if (goog.isDefAndNotNull(resources.root)) resources.root.setRequest(this);
  387. if (!goog.array.isEmpty(resources.childs)) {
  388. goog.array.forEach(resources.childs, function(child) {
  389. child.setRequest(this);
  390. }, this);
  391. }
  392. } else {
  393. resources = builder.serialize(opt_helper.asModel);
  394. }
  395. return resources;
  396. };
  397.  
  398. /**
  399. * Update(move, copy) request handler.
  400. *
  401. * @private
  402. * @param {string} method Method name of xhrdav.Client instance.
  403. * @param {string} path Update src file path.
  404. * @param {string} dstPath Update destination path.
  405. * @param {Function} handler callback handler function.
  406. * @param {Object=} opt_request Request parameters.
  407. * @param {Object=} context Callback scope.
  408. * @param {Function=} onXhrComplete onXhrComplete callback function.
  409. */
  410. xhrdav.DavFs.Request.prototype.updateRequestHandler_ = function(
  411. method, path, dstPath, handler, opt_request, context, onXhrComplete) {
  412. var api = goog.getObjectByName(method, this.davSite_);
  413.  
  414. api.call(this.davSite_, path, dstPath,
  415. goog.bind(this.responseHandler_, this,
  416. handler, this.simpleErrorHandler_, path, context),
  417. opt_request, onXhrComplete);
  418. };
  419.  
  420. /**
  421. * Propfind request(listDir, getProps) handler.
  422. *
  423. * @private
  424. * @param {string} method Method name of xhrdav.Client instance.
  425. * @param {string} path propfind request path.
  426. * @param {Function} handler callback handler function.
  427. * @param {Object=} opt_request Request parameters.
  428. * @param {Object=} context Callback scope.
  429. * @param {{hasCtrl:boolean, asModel:boolean}=} opt_helper response options.
  430. * @param {Function=} onXhrComplete onXhrComplete callback function.
  431. */
  432. xhrdav.DavFs.Request.prototype.propfindRequestHandler_ = function(
  433. path, handler, opt_request, context, opt_helper, onXhrComplete) {
  434. var dataHandler = goog.bind(this.processMultistatus_, this, opt_helper);
  435. this.davSite_.propfind(path,
  436. goog.bind(this.responseHandler_, this, handler, dataHandler, path, context),
  437. opt_request, onXhrComplete);
  438. };
  439.  
  440. /**
  441. * Processing Reponse Multi-Status Data
  442. *
  443. * @private
  444. * @param {string} path HTTP Request path.
  445. * @param {number} statusCode HTTP Status code.
  446. * @param {Object} content Response body data.
  447. * @param {Object} headers Response headers.
  448. * @return {Array.<xhrdav.Errors, xhrdav.Response>} response contents.
  449. * @see xhrdav.Errors
  450. */
  451. xhrdav.DavFs.Request.prototype.processMultistatus_ = function(
  452. opt_helper, path, statusCode, content, headers) {
  453. var config = xhrdav.Conf.getInstance();
  454. var httpStatus = xhrdav.HttpStatus;
  455. var errors = new xhrdav.Errors();
  456.  
  457. var args = [];
  458. if (statusCode == httpStatus.MULTI_STATUS) {
  459. // TODO: research scope
  460. xhrdav.Conf.logging({'name': 'xhrdav.DavFs.Request#processMultistatus_',
  461. 'scope': this instanceof xhrdav.DavFs.Request}, 'config');
  462. content = this.getListDirFromMultistatus(content, opt_helper);
  463. } else {
  464. var data = {statusCode: statusCode, path: path, content: content};
  465. errors.setRequest(xhrdav.DavFs.Request.buildRequestErrors(data));
  466. }
  467. args.push(errors);
  468. args.push(content);
  469.  
  470. return args;
  471. };
  472.  
  473. /**
  474. * Create Request paramters.
  475. *
  476. * @private
  477. * @param {Object=} opt_headers HTTP Request Headers.
  478. * @param {Object=} opt_params HTTP Query parameters.
  479. * @param {string=} opt_xhrId Xhrmanager Id.
  480. * @return {Object} created request map object.
  481. * @throws {Error} Not found of xhrIo object.
  482. */
  483. xhrdav.DavFs.Request.prototype.createRequestParameters_ = function(
  484. opt_headers, opt_params, opt_xhrId) {
  485. var opt_request = {headers: opt_headers || {}, query: opt_params || {}};
  486.  
  487. if (!goog.isDefAndNotNull(this.xhrIo_)) {
  488. // Nothing to do.
  489. } else if (this.xhrIo_ instanceof goog.net.XhrIo) {
  490. goog.object.extend(opt_request, {xhrIo: this.xhrIo_});
  491. } else if (this.xhrIo_ instanceof goog.net.XhrManager) {
  492. var map;
  493. if (!goog.string.isEmptySafe(opt_xhrId)) {
  494. map = {xhrId: opt_xhrId, xhrMgr: this.xhrIo_};
  495. } else {
  496. map = {xhrId: goog.string.createUniqueString(), xhrMgr: this.xhrIo_};
  497. }
  498. goog.object.extend(opt_request, map);
  499. }
  500.  
  501. return opt_request;
  502. };
  503.  
  504. /**
  505. * listing collection
  506. *
  507. * @param {string} path Listing request path.
  508. * @param {Function} handler callback handler function.
  509. * @param {Object=} opt_headers Request headers options.
  510. * @param {Object=} opt_params Request query paramters.
  511. * @param {Object=} context Callback scope.
  512. * @param {{hasCtrl:boolean=, asModel:boolean=, xhrId:string=}=} opt_helper
  513. * xhr and response options.
  514. * @param {Function=} onXhrComplete onXhrComplete callback function.
  515. * @see #propfindRequestHandler_
  516. */
  517. xhrdav.DavFs.Request.prototype.listDir = function(
  518. path, handler, opt_headers, opt_params, context, opt_helper, onXhrComplete) {
  519. var opt_request = this.createRequestParameters_(
  520. opt_headers, opt_params, opt_helper && opt_helper.xhrId);
  521.  
  522. opt_request.headers['Depth'] = 1; // listing directory
  523. this.propfindRequestHandler_(path, handler, opt_request,
  524. context, opt_helper, onXhrComplete);
  525. };
  526.  
  527. /**
  528. * Get property for a single resource.
  529. *
  530. * @param {string} path property get request path.
  531. * @param {Function} handler callback handler function.
  532. * @param {Object=} opt_headers Request headers options.
  533. * @param {Object=} opt_params Request query paramters.
  534. * @param {Object=} context Callback scope.
  535. * @param {{hasCtrl:boolean=, asModel:boolean=, xhrId:string=}=} opt_helper
  536. * xhr and response options.
  537. * @param {Function=} onXhrComplete onXhrComplete callback function.
  538. * @see #propfindRequestHandler_
  539. */
  540. xhrdav.DavFs.Request.prototype.getProps = function(
  541. path, handler, opt_headers, opt_params, context, opt_helper, onXhrComplete) {
  542. var opt_request = this.createRequestParameters_(
  543. opt_headers, opt_params, opt_helper && opt_helper.xhrId);
  544.  
  545. this.propfindRequestHandler_(
  546. path, handler, opt_request, context, onXhrComplete);
  547. };
  548.  
  549. /**
  550. * Create directory (collection)
  551. *
  552. * @param {string} path Create dierctory path.
  553. * @param {Function} handler callback handler function.
  554. * @param {Object=} opt_headers Request headers options.
  555. * @param {Object=} opt_params Request query paramters.
  556. * @param {Object=} context Callback scope.
  557. * @param {{xhrId:string=}=} opt_helper xhr options.
  558. * @param {Function=} onXhrComplete onXhrComplete callback function.
  559. */
  560. xhrdav.DavFs.Request.prototype.mkDir = function(
  561. path, handler, opt_headers, opt_params, context, opt_helper, onXhrComplete) {
  562. var opt_request = this.createRequestParameters_(
  563. opt_headers, opt_params, opt_helper && opt_helper.xhrId);
  564.  
  565. this.davSite_.mkcol(path,
  566. goog.bind(this.responseHandler_, this,
  567. handler, this.simpleErrorHandler_, path, context),
  568. opt_request, onXhrComplete);
  569. };
  570.  
  571. /**
  572. * Remove resource
  573. *
  574. * @param {string} path Remove resource path.
  575. * @param {Function} handler callback handler function.
  576. * @param {Object=} opt_headers Request headers options.
  577. * @param {Object=} opt_params Request query paramters.
  578. * @param {Object=} context Callback scope.
  579. * @param {{xhrId:string=}=} opt_helper xhr options.
  580. * @param {Function=} onXhrComplete onXhrComplete callback function.
  581. */
  582. xhrdav.DavFs.Request.prototype.remove = function(
  583. path, handler, opt_headers, opt_params, context, opt_helper, onXhrComplete) {
  584. var opt_request = this.createRequestParameters_(
  585. opt_headers, opt_params, opt_helper && opt_helper.xhrId);
  586.  
  587. this.davSite_._delete(path,
  588. goog.bind(this.responseHandler_, this,
  589. handler, this.simpleErrorHandler_, path, context),
  590. opt_request, onXhrComplete);
  591. };
  592.  
  593. /**
  594. * Move resource
  595. *
  596. * @param {string} path Move src resource path.
  597. * @param {string} dstPath Move destination path.
  598. * @param {Function} handler callback handler function.
  599. * @param {Object=} opt_headers Request headers options.
  600. * @param {Object=} opt_params Request query paramters.
  601. * @param {Object=} context Callback scope.
  602. * @param {{xhrId:string=}=} opt_helper xhr options.
  603. * @param {Function=} onXhrComplete onXhrComplete callback function.
  604. */
  605. xhrdav.DavFs.Request.prototype.move = function(
  606. path, dstPath, handler, opt_headers, opt_params, context,
  607. opt_helper, onXhrComplete) {
  608. var opt_request = this.createRequestParameters_(
  609. opt_headers, opt_params, opt_helper && opt_helper.xhrId);
  610.  
  611. this.updateRequestHandler_('move',
  612. path, dstPath, handler, opt_request, context, onXhrComplete);
  613. };
  614.  
  615. /**
  616. * Copy resource
  617. *
  618. * @param {string} path Move src resource path.
  619. * @param {string} dstPath Move destination path.
  620. * @param {Function} handler callback handler function.
  621. * @param {Object=} opt_headers Request headers options.
  622. * @param {Object=} opt_params Request query paramters.
  623. * @param {Object=} context Callback scope.
  624. * @param {{xhrId:string=}=} opt_helper xhr options.
  625. * @param {Function=} onXhrComplete onXhrComplete callback function.
  626. * @see #updateRequestHandler_
  627. */
  628. xhrdav.DavFs.Request.prototype.copy = function(
  629. path, dstPath, handler, opt_headers, opt_params, context,
  630. opt_helper, onXhrComplete) {
  631. var opt_request = this.createRequestParameters_(
  632. opt_headers, opt_params, opt_helper && opt_helper.xhrId);
  633. this.updateRequestHandler_('copy',
  634. path, dstPath, handler, opt_request, context, onXhrComplete);
  635. };
  636.  
  637. /**
  638. * Read data from WebDAV server
  639. *
  640. * @param {string} path read file path.
  641. * @param {Function} handler callback handler function.
  642. * @param {Object=} opt_headers Request headers options.
  643. * @param {Object=} opt_params Request query paramters.
  644. * @param {{xhrId:string=}=} opt_helper xhr options.
  645. * @param {Function=} onXhrComplete onXhrComplete callback function.
  646. */
  647. xhrdav.DavFs.Request.prototype.read = function(
  648. path, handler, opt_headers, opt_params, context, opt_helper, onXhrComplete) {
  649. var opt_request = this.createRequestParameters_(
  650. opt_headers, opt_params, opt_helper && opt_helper.xhrId);
  651.  
  652. path = xhrdav.utils.path.removeLastSlash(path);
  653.  
  654. this.davSite_.get(path,
  655. goog.bind(this.responseHandler_, this,
  656. handler, this.contentReadHandler_, path, context),
  657. opt_request, onXhrComplete);
  658. };
  659.  
  660. /**
  661. * Write data to WebDAV server
  662. *
  663. * @param {string} path upload file path.
  664. * @param {Object} content data string.
  665. * @param {Function} handler callback handler function.
  666. * @param {Object=} opt_headers Request headers options.
  667. * @param {Object=} opt_params Request query paramters.
  668. * @param {Object=} context Callback scope.
  669. * @param {{xhrId:string=}=} opt_helper xhr options.
  670. * @param {Function=} onXhrComplete onXhrComplete callback function.
  671. */
  672. xhrdav.DavFs.Request.prototype.write = function(
  673. path, content, handler, opt_headers, opt_params, context,
  674. opt_helper, onXhrComplete) {
  675. var opt_request = this.createRequestParameters_(
  676. opt_headers, opt_params, opt_helper && opt_helper.xhrId);
  677.  
  678. path = xhrdav.utils.path.removeLastSlash(path);
  679.  
  680. this.davSite_.put(path, content,
  681. goog.bind(this.responseHandler_, this,
  682. handler, this.simpleErrorHandler_, path, context),
  683. opt_request, onXhrComplete);
  684. };
  685.  
  686. /**
  687. * Upload data to WebDAV server
  688. *
  689. * @param {string} path upload file path.
  690. * @param {File} file File object(File API).
  691. * @param {Function} handler callback handler function.
  692. * @param {Object=} opt_headers Request headers options.
  693. * @param {Object=} opt_params Request query paramters.
  694. * @param {Object=} context Callback scope.
  695. * @param {{xhrId:string=}=} opt_helper xhr options.
  696. * @param {Function=} onXhrComplete onXhrComplete callback function.
  697. * @throws {Error} Not a file object.
  698. */
  699. xhrdav.DavFs.Request.prototype.upload = function(
  700. path, file, handler, opt_headers, opt_params, context,
  701. opt_helper, onXhrComplete) {
  702. var opt_request = this.createRequestParameters_(
  703. opt_headers, opt_params, opt_helper && opt_helper.xhrId);
  704.  
  705. path = xhrdav.utils.path.removeLastSlash(path);
  706. if (!(file instanceof File)) {
  707. xhrdav.Conf.logging(
  708. {'DavFs.Request#upload':
  709. 'Argument "file" is not a file object!![path: ' + path + ']'},
  710. 'warning');
  711. xhrdav.Conf.logging(file, 'warning');
  712. }
  713. if (goog.isDefAndNotNull(file)) {
  714. goog.object.extend(opt_request.headers,
  715. {x_file_name: file.name, x_file_size: file.size});
  716. }
  717.  
  718. this.davSite_.put(path, file,
  719. goog.bind(this.responseHandler_, this,
  720. handler, this.simpleErrorHandler_, path, context),
  721. opt_request, onXhrComplete);
  722. };
  723.  
  724. /**
  725. * Resource Exists to WebDAV server
  726. *
  727. * @param {string} path exists resource path.
  728. * @param {Function} handler callback handler function.
  729. * @param {Object=} opt_headers Request headers options.
  730. * @param {Object=} opt_params Request query paramters.
  731. * @param {Object=} context Callback scope.
  732. * @param {{xhrId:string=}=} opt_helper xhr options.
  733. * @param {Function=} onXhrComplete onXhrComplete callback function.
  734. */
  735. xhrdav.DavFs.Request.prototype.exists = function(
  736. path, handler, opt_headers, opt_params, context, opt_helper, onXhrComplete) {
  737. var opt_request = this.createRequestParameters_(
  738. opt_headers, opt_params, opt_helper && opt_helper.xhrId);
  739.  
  740. this.davSite_.head(path,
  741. goog.bind(this.responseHandler_, this,
  742. handler, this.existsHandler_, path, context),
  743. opt_request, onXhrComplete);
  744. };
  745.  
  746. /**
  747. * Create ResourceController.
  748. *
  749. * @param {(xhrdav.Resource|Object)=} resource
  750. * Json/Hash object for WebDAV resource.
  751. * @return {xhrdav.ResourceController}
  752. * createed request resource controller object.
  753. * @see xhrdav.ResourceController
  754. */
  755. xhrdav.DavFs.Request.prototype.createResourceController = function(resource) {
  756. var controller = new xhrdav.ResourceController(resource);
  757. controller.setRequest(this);
  758. return controller;
  759. };
  760.  
  761.  
  762. /* Entry Point for closure compiler */
  763. goog.exportSymbol('xhrdav.DavFs.getInstance', xhrdav.DavFs.getInstance);
  764. goog.exportSymbol('xhrdav.DavFs.DEFAULT_DAV_SITE_NAME',
  765. xhrdav.DavFs.DEFAULT_DAV_SITE_NAME);
  766. goog.exportProperty(xhrdav.DavFs.prototype, 'getXhrManager',
  767. xhrdav.DavFs.prototype.getXhrManager);
  768. goog.exportProperty(xhrdav.DavFs.prototype, 'setXhrManager',
  769. xhrdav.DavFs.prototype.setXhrManager);
  770. goog.exportProperty(xhrdav.DavFs.prototype, 'getConnection',
  771. xhrdav.DavFs.prototype.getConnection);
  772. goog.exportProperty(xhrdav.DavFs.prototype, 'addConnection',
  773. xhrdav.DavFs.prototype.addConnection);
  774. goog.exportProperty(xhrdav.DavFs.prototype, 'getRequest',
  775. xhrdav.DavFs.prototype.getRequest);
  776.  
  777. goog.exportSymbol('xhrdav.DavFs.Request', xhrdav.DavFs.Request);
  778. goog.exportSymbol('xhrdav.DavFs.Request.buildRequestErrors',
  779. xhrdav.DavFs.Request.buildRequestErrors);
  780. goog.exportProperty(xhrdav.DavFs.Request.prototype,
  781. 'getListDirFromMultistatus',
  782. xhrdav.DavFs.Request.prototype.getListDirFromMultistatus);
  783. goog.exportProperty(xhrdav.DavFs.Request.prototype, 'listDir',
  784. xhrdav.DavFs.Request.prototype.listDir);
  785. goog.exportProperty(xhrdav.DavFs.Request.prototype, 'getProps',
  786. xhrdav.DavFs.Request.prototype.getProps);
  787. goog.exportProperty(xhrdav.DavFs.Request.prototype, 'mkDir',
  788. xhrdav.DavFs.Request.prototype.mkDir);
  789. goog.exportProperty(xhrdav.DavFs.Request.prototype, 'remove',
  790. xhrdav.DavFs.Request.prototype.remove);
  791. goog.exportProperty(xhrdav.DavFs.Request.prototype, 'move',
  792. xhrdav.DavFs.Request.prototype.move);
  793. goog.exportProperty(xhrdav.DavFs.Request.prototype, 'copy',
  794. xhrdav.DavFs.Request.prototype.copy);
  795. goog.exportProperty(xhrdav.DavFs.Request.prototype, 'read',
  796. xhrdav.DavFs.Request.prototype.read);
  797. goog.exportProperty(xhrdav.DavFs.Request.prototype, 'write',
  798. xhrdav.DavFs.Request.prototype.write);
  799. goog.exportProperty(xhrdav.DavFs.Request.prototype, 'upload',
  800. xhrdav.DavFs.Request.prototype.upload);
  801. goog.exportProperty(xhrdav.DavFs.Request.prototype, 'exists',
  802. xhrdav.DavFs.Request.prototype.exists);
  803. goog.exportProperty(xhrdav.DavFs.Request.prototype, 'createResourceController',
  804. xhrdav.DavFs.Request.prototype.createResourceController);
  805.