2015-07-13 15:31:47 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3
|
|
|
|
* or later.
|
|
|
|
*
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* global dav */
|
|
|
|
|
|
|
|
(function(OC, FileInfo) {
|
|
|
|
/**
|
|
|
|
* @class OC.Files.Client
|
|
|
|
* @classdesc Client to access files on the server
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {String} options.host host name
|
|
|
|
* @param {int} [options.port] port
|
|
|
|
* @param {boolean} [options.useHTTPS] whether to use https
|
|
|
|
* @param {String} [options.root] root path
|
|
|
|
* @param {String} [options.userName] user name
|
|
|
|
* @param {String} [options.password] password
|
|
|
|
*
|
|
|
|
* @since 8.2
|
|
|
|
*/
|
|
|
|
var Client = function(options) {
|
|
|
|
this._root = options.root;
|
|
|
|
if (this._root.charAt(this._root.length - 1) === '/') {
|
|
|
|
this._root = this._root.substr(0, this._root.length - 1);
|
|
|
|
}
|
|
|
|
|
2016-11-21 14:03:45 +00:00
|
|
|
var url = Client.PROTOCOL_HTTP + '://';
|
2015-07-13 15:31:47 +00:00
|
|
|
if (options.useHTTPS) {
|
2016-11-21 14:03:45 +00:00
|
|
|
url = Client.PROTOCOL_HTTPS + '://';
|
2015-07-13 15:31:47 +00:00
|
|
|
}
|
|
|
|
|
2015-11-17 12:37:13 +00:00
|
|
|
url += options.host + this._root;
|
2016-10-07 14:27:54 +00:00
|
|
|
this._host = options.host;
|
2016-02-16 12:16:52 +00:00
|
|
|
this._defaultHeaders = options.defaultHeaders || {
|
|
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
|
|
'requesttoken': OC.requestToken
|
|
|
|
};
|
2015-07-13 15:31:47 +00:00
|
|
|
this._baseUrl = url;
|
2015-12-14 16:42:13 +00:00
|
|
|
|
|
|
|
var clientOptions = {
|
2015-07-13 15:31:47 +00:00
|
|
|
baseUrl: this._baseUrl,
|
|
|
|
xmlNamespaces: {
|
|
|
|
'DAV:': 'd',
|
2016-07-26 11:40:44 +00:00
|
|
|
'http://owncloud.org/ns': 'oc',
|
2018-03-08 18:52:37 +00:00
|
|
|
'http://nextcloud.org/ns': 'nc',
|
|
|
|
'http://open-collaboration-services.org/ns': 'ocs'
|
2015-07-13 15:31:47 +00:00
|
|
|
}
|
2015-12-14 16:42:13 +00:00
|
|
|
};
|
|
|
|
if (options.userName) {
|
|
|
|
clientOptions.userName = options.userName;
|
|
|
|
}
|
|
|
|
if (options.password) {
|
|
|
|
clientOptions.password = options.password;
|
|
|
|
}
|
|
|
|
this._client = new dav.Client(clientOptions);
|
2015-07-13 15:31:47 +00:00
|
|
|
this._client.xhrProvider = _.bind(this._xhrProvider, this);
|
2018-09-11 18:41:49 +00:00
|
|
|
this._fileInfoParsers = [];
|
2015-07-13 15:31:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Client.NS_OWNCLOUD = 'http://owncloud.org/ns';
|
2016-07-26 11:40:44 +00:00
|
|
|
Client.NS_NEXTCLOUD = 'http://nextcloud.org/ns';
|
2015-07-13 15:31:47 +00:00
|
|
|
Client.NS_DAV = 'DAV:';
|
2018-03-08 18:52:37 +00:00
|
|
|
Client.NS_OCS = 'http://open-collaboration-services.org/ns';
|
2016-11-21 14:03:45 +00:00
|
|
|
|
|
|
|
Client.PROPERTY_GETLASTMODIFIED = '{' + Client.NS_DAV + '}getlastmodified';
|
|
|
|
Client.PROPERTY_GETETAG = '{' + Client.NS_DAV + '}getetag';
|
|
|
|
Client.PROPERTY_GETCONTENTTYPE = '{' + Client.NS_DAV + '}getcontenttype';
|
|
|
|
Client.PROPERTY_RESOURCETYPE = '{' + Client.NS_DAV + '}resourcetype';
|
|
|
|
Client.PROPERTY_INTERNAL_FILEID = '{' + Client.NS_OWNCLOUD + '}fileid';
|
|
|
|
Client.PROPERTY_PERMISSIONS = '{' + Client.NS_OWNCLOUD + '}permissions';
|
|
|
|
Client.PROPERTY_SIZE = '{' + Client.NS_OWNCLOUD + '}size';
|
|
|
|
Client.PROPERTY_GETCONTENTLENGTH = '{' + Client.NS_DAV + '}getcontentlength';
|
2017-09-19 10:53:35 +00:00
|
|
|
Client.PROPERTY_ISENCRYPTED = '{' + Client.NS_DAV + '}is-encrypted';
|
2018-03-08 18:52:37 +00:00
|
|
|
Client.PROPERTY_SHARE_PERMISSIONS = '{' + Client.NS_OCS + '}share-permissions';
|
2016-11-21 14:03:45 +00:00
|
|
|
|
|
|
|
Client.PROTOCOL_HTTP = 'http';
|
|
|
|
Client.PROTOCOL_HTTPS = 'https';
|
|
|
|
|
2015-07-13 15:31:47 +00:00
|
|
|
Client._PROPFIND_PROPERTIES = [
|
|
|
|
/**
|
|
|
|
* Modified time
|
|
|
|
*/
|
|
|
|
[Client.NS_DAV, 'getlastmodified'],
|
|
|
|
/**
|
|
|
|
* Etag
|
|
|
|
*/
|
|
|
|
[Client.NS_DAV, 'getetag'],
|
|
|
|
/**
|
|
|
|
* Mime type
|
|
|
|
*/
|
|
|
|
[Client.NS_DAV, 'getcontenttype'],
|
|
|
|
/**
|
|
|
|
* Resource type "collection" for folders, empty otherwise
|
|
|
|
*/
|
|
|
|
[Client.NS_DAV, 'resourcetype'],
|
|
|
|
/**
|
2015-11-18 14:18:00 +00:00
|
|
|
* File id
|
2015-07-13 15:31:47 +00:00
|
|
|
*/
|
2015-11-18 14:18:00 +00:00
|
|
|
[Client.NS_OWNCLOUD, 'fileid'],
|
2015-07-13 15:31:47 +00:00
|
|
|
/**
|
|
|
|
* Letter-coded permissions
|
|
|
|
*/
|
|
|
|
[Client.NS_OWNCLOUD, 'permissions'],
|
|
|
|
//[Client.NS_OWNCLOUD, 'downloadURL'],
|
|
|
|
/**
|
|
|
|
* Folder sizes
|
|
|
|
*/
|
|
|
|
[Client.NS_OWNCLOUD, 'size'],
|
|
|
|
/**
|
|
|
|
* File sizes
|
|
|
|
*/
|
2016-07-26 11:40:44 +00:00
|
|
|
[Client.NS_DAV, 'getcontentlength'],
|
|
|
|
/**
|
|
|
|
* Preview availability
|
|
|
|
*/
|
2017-04-26 12:53:11 +00:00
|
|
|
[Client.NS_NEXTCLOUD, 'has-preview'],
|
|
|
|
/**
|
|
|
|
* Mount type
|
|
|
|
*/
|
|
|
|
[Client.NS_NEXTCLOUD, 'mount-type'],
|
2017-09-19 10:53:35 +00:00
|
|
|
/**
|
|
|
|
* Encryption state
|
|
|
|
*/
|
|
|
|
[Client.NS_NEXTCLOUD, 'is-encrypted'],
|
2018-03-08 18:52:37 +00:00
|
|
|
/**
|
|
|
|
* Share permissions
|
|
|
|
*/
|
|
|
|
[Client.NS_OCS, 'share-permissions']
|
2015-07-13 15:31:47 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @memberof OC.Files
|
|
|
|
*/
|
|
|
|
Client.prototype = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Root path of the Webdav endpoint
|
|
|
|
*
|
|
|
|
* @type string
|
|
|
|
*/
|
|
|
|
_root: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Client from the library
|
|
|
|
*
|
2015-11-02 13:17:49 +00:00
|
|
|
* @type dav.Client
|
2015-07-13 15:31:47 +00:00
|
|
|
*/
|
|
|
|
_client: null,
|
|
|
|
|
2015-11-02 13:17:49 +00:00
|
|
|
/**
|
|
|
|
* Array of file info parsing functions.
|
|
|
|
*
|
|
|
|
* @type Array<OC.Files.Client~parseFileInfo>
|
|
|
|
*/
|
|
|
|
_fileInfoParsers: [],
|
|
|
|
|
2015-07-13 15:31:47 +00:00
|
|
|
/**
|
|
|
|
* Returns the configured XHR provider for davclient
|
|
|
|
* @return {XMLHttpRequest}
|
|
|
|
*/
|
|
|
|
_xhrProvider: function() {
|
|
|
|
var headers = this._defaultHeaders;
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
var oldOpen = xhr.open;
|
|
|
|
// override open() method to add headers
|
|
|
|
xhr.open = function() {
|
|
|
|
var result = oldOpen.apply(this, arguments);
|
|
|
|
_.each(headers, function(value, key) {
|
|
|
|
xhr.setRequestHeader(key, value);
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
};
|
2015-12-10 17:08:40 +00:00
|
|
|
|
|
|
|
OC.registerXHRForErrorProcessing(xhr);
|
2015-07-13 15:31:47 +00:00
|
|
|
return xhr;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepends the base url to the given path sections
|
|
|
|
*
|
|
|
|
* @param {...String} path sections
|
|
|
|
*
|
|
|
|
* @return {String} base url + joined path, any leading or trailing slash
|
|
|
|
* will be kept
|
|
|
|
*/
|
|
|
|
_buildUrl: function() {
|
|
|
|
var path = this._buildPath.apply(this, arguments);
|
|
|
|
if (path.charAt([path.length - 1]) === '/') {
|
|
|
|
path = path.substr(0, path.length - 1);
|
|
|
|
}
|
|
|
|
if (path.charAt(0) === '/') {
|
|
|
|
path = path.substr(1);
|
|
|
|
}
|
|
|
|
return this._baseUrl + '/' + path;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append the path to the root and also encode path
|
|
|
|
* sections
|
|
|
|
*
|
|
|
|
* @param {...String} path sections
|
|
|
|
*
|
|
|
|
* @return {String} joined path, any leading or trailing slash
|
|
|
|
* will be kept
|
|
|
|
*/
|
|
|
|
_buildPath: function() {
|
|
|
|
var path = OC.joinPaths.apply(this, arguments);
|
|
|
|
var sections = path.split('/');
|
|
|
|
var i;
|
|
|
|
for (i = 0; i < sections.length; i++) {
|
|
|
|
sections[i] = encodeURIComponent(sections[i]);
|
|
|
|
}
|
|
|
|
path = sections.join('/');
|
|
|
|
return path;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse headers string into a map
|
|
|
|
*
|
|
|
|
* @param {string} headersString headers list as string
|
|
|
|
*
|
|
|
|
* @return {Object.<String,Array>} map of header name to header contents
|
|
|
|
*/
|
|
|
|
_parseHeaders: function(headersString) {
|
|
|
|
var headerRows = headersString.split('\n');
|
|
|
|
var headers = {};
|
|
|
|
for (var i = 0; i < headerRows.length; i++) {
|
|
|
|
var sepPos = headerRows[i].indexOf(':');
|
|
|
|
if (sepPos < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var headerName = headerRows[i].substr(0, sepPos);
|
|
|
|
var headerValue = headerRows[i].substr(sepPos + 2);
|
|
|
|
|
|
|
|
if (!headers[headerName]) {
|
|
|
|
// make it an array
|
|
|
|
headers[headerName] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
headers[headerName].push(headerValue);
|
|
|
|
}
|
|
|
|
return headers;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses the etag response which is in double quotes.
|
|
|
|
*
|
|
|
|
* @param {string} etag etag value in double quotes
|
|
|
|
*
|
|
|
|
* @return {string} etag without double quotes
|
|
|
|
*/
|
|
|
|
_parseEtag: function(etag) {
|
|
|
|
if (etag.charAt(0) === '"') {
|
|
|
|
return etag.split('"')[1];
|
|
|
|
}
|
|
|
|
return etag;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse Webdav result
|
|
|
|
*
|
|
|
|
* @param {Object} response XML object
|
|
|
|
*
|
|
|
|
* @return {Array.<FileInfo>} array of file info
|
|
|
|
*/
|
|
|
|
_parseFileInfo: function(response) {
|
2018-09-10 20:37:01 +00:00
|
|
|
var path = decodeURIComponent(response.href);
|
2015-07-13 15:31:47 +00:00
|
|
|
if (path.substr(0, this._root.length) === this._root) {
|
|
|
|
path = path.substr(this._root.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path.charAt(path.length - 1) === '/') {
|
|
|
|
path = path.substr(0, path.length - 1);
|
|
|
|
}
|
|
|
|
|
2016-02-08 10:43:42 +00:00
|
|
|
if (response.propStat.length === 0 || response.propStat[0].status !== 'HTTP/1.1 200 OK') {
|
2015-07-13 15:31:47 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var props = response.propStat[0].properties;
|
|
|
|
|
|
|
|
var data = {
|
2016-11-21 14:03:45 +00:00
|
|
|
id: props[Client.PROPERTY_INTERNAL_FILEID],
|
2015-07-13 15:31:47 +00:00
|
|
|
path: OC.dirname(path) || '/',
|
|
|
|
name: OC.basename(path),
|
2016-11-21 14:03:45 +00:00
|
|
|
mtime: (new Date(props[Client.PROPERTY_GETLASTMODIFIED])).getTime()
|
2015-07-13 15:31:47 +00:00
|
|
|
};
|
|
|
|
|
2016-11-21 14:03:45 +00:00
|
|
|
var etagProp = props[Client.PROPERTY_GETETAG];
|
2015-07-13 15:31:47 +00:00
|
|
|
if (!_.isUndefined(etagProp)) {
|
|
|
|
data.etag = this._parseEtag(etagProp);
|
|
|
|
}
|
|
|
|
|
2016-11-21 14:03:45 +00:00
|
|
|
var sizeProp = props[Client.PROPERTY_GETCONTENTLENGTH];
|
2015-07-13 15:31:47 +00:00
|
|
|
if (!_.isUndefined(sizeProp)) {
|
|
|
|
data.size = parseInt(sizeProp, 10);
|
|
|
|
}
|
|
|
|
|
2016-11-21 14:03:45 +00:00
|
|
|
sizeProp = props[Client.PROPERTY_SIZE];
|
2015-07-13 15:31:47 +00:00
|
|
|
if (!_.isUndefined(sizeProp)) {
|
|
|
|
data.size = parseInt(sizeProp, 10);
|
|
|
|
}
|
|
|
|
|
2016-07-26 11:40:44 +00:00
|
|
|
var hasPreviewProp = props['{' + Client.NS_NEXTCLOUD + '}has-preview'];
|
|
|
|
if (!_.isUndefined(hasPreviewProp)) {
|
|
|
|
data.hasPreview = hasPreviewProp === 'true';
|
|
|
|
} else {
|
|
|
|
data.hasPreview = true;
|
|
|
|
}
|
|
|
|
|
2017-09-19 10:53:35 +00:00
|
|
|
var isEncryptedProp = props['{' + Client.NS_NEXTCLOUD + '}is-encrypted'];
|
|
|
|
if (!_.isUndefined(isEncryptedProp)) {
|
|
|
|
data.isEncrypted = isEncryptedProp === '1';
|
|
|
|
} else {
|
|
|
|
data.isEncrypted = false;
|
|
|
|
}
|
|
|
|
|
2016-11-21 14:03:45 +00:00
|
|
|
var contentType = props[Client.PROPERTY_GETCONTENTTYPE];
|
2015-07-13 15:31:47 +00:00
|
|
|
if (!_.isUndefined(contentType)) {
|
|
|
|
data.mimetype = contentType;
|
|
|
|
}
|
|
|
|
|
2016-11-21 14:03:45 +00:00
|
|
|
var resType = props[Client.PROPERTY_RESOURCETYPE];
|
2015-07-13 15:31:47 +00:00
|
|
|
if (!data.mimetype && resType) {
|
|
|
|
var xmlvalue = resType[0];
|
|
|
|
if (xmlvalue.namespaceURI === Client.NS_DAV && xmlvalue.nodeName.split(':')[1] === 'collection') {
|
|
|
|
data.mimetype = 'httpd/unix-directory';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 16:08:00 +00:00
|
|
|
data.permissions = OC.PERMISSION_NONE;
|
2016-11-21 14:03:45 +00:00
|
|
|
var permissionProp = props[Client.PROPERTY_PERMISSIONS];
|
2015-07-13 15:31:47 +00:00
|
|
|
if (!_.isUndefined(permissionProp)) {
|
|
|
|
var permString = permissionProp || '';
|
|
|
|
data.mountType = null;
|
|
|
|
for (var i = 0; i < permString.length; i++) {
|
|
|
|
var c = permString.charAt(i);
|
|
|
|
switch (c) {
|
|
|
|
// FIXME: twisted permissions
|
|
|
|
case 'C':
|
|
|
|
case 'K':
|
|
|
|
data.permissions |= OC.PERMISSION_CREATE;
|
|
|
|
break;
|
2017-11-02 16:08:00 +00:00
|
|
|
case 'G':
|
|
|
|
data.permissions |= OC.PERMISSION_READ;
|
|
|
|
break;
|
2015-07-13 15:31:47 +00:00
|
|
|
case 'W':
|
2017-07-03 13:37:26 +00:00
|
|
|
case 'N':
|
|
|
|
case 'V':
|
2015-07-13 15:31:47 +00:00
|
|
|
data.permissions |= OC.PERMISSION_UPDATE;
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
data.permissions |= OC.PERMISSION_DELETE;
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
data.permissions |= OC.PERMISSION_SHARE;
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
if (!data.mountType) {
|
|
|
|
// TODO: how to identify external-root ?
|
|
|
|
data.mountType = 'external';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
// TODO: how to identify shared-root ?
|
|
|
|
data.mountType = 'shared';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-08 18:52:37 +00:00
|
|
|
var sharePermissionsProp = props[Client.PROPERTY_SHARE_PERMISSIONS];
|
|
|
|
if (!_.isUndefined(sharePermissionsProp)) {
|
|
|
|
data.sharePermissions = parseInt(sharePermissionsProp);
|
|
|
|
}
|
|
|
|
|
2017-04-26 12:53:11 +00:00
|
|
|
var mounTypeProp = props['{' + Client.NS_NEXTCLOUD + '}mount-type'];
|
|
|
|
if (!_.isUndefined(mounTypeProp)) {
|
|
|
|
data.mountType = mounTypeProp;
|
|
|
|
}
|
|
|
|
|
2015-11-02 13:17:49 +00:00
|
|
|
// extend the parsed data using the custom parsers
|
|
|
|
_.each(this._fileInfoParsers, function(parserFunction) {
|
2018-09-03 16:30:42 +00:00
|
|
|
_.extend(data, parserFunction(response, data) || {});
|
2015-11-02 13:17:49 +00:00
|
|
|
});
|
|
|
|
|
2015-07-13 15:31:47 +00:00
|
|
|
return new FileInfo(data);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse Webdav multistatus
|
|
|
|
*
|
|
|
|
* @param {Array} responses
|
|
|
|
*/
|
|
|
|
_parseResult: function(responses) {
|
|
|
|
var self = this;
|
|
|
|
return _.map(responses, function(response) {
|
|
|
|
return self._parseFileInfo(response);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the given status code means success
|
|
|
|
*
|
|
|
|
* @param {int} status status code
|
|
|
|
*
|
|
|
|
* @return true if status code is between 200 and 299 included
|
|
|
|
*/
|
|
|
|
_isSuccessStatus: function(status) {
|
|
|
|
return status >= 200 && status <= 299;
|
|
|
|
},
|
|
|
|
|
2017-08-08 14:37:14 +00:00
|
|
|
/**
|
|
|
|
* Parse the Sabre exception out of the given response, if any
|
|
|
|
*
|
|
|
|
* @param {Object} response object
|
|
|
|
* @return {Object} array of parsed message and exception (only the first one)
|
|
|
|
*/
|
|
|
|
_getSabreException: function(response) {
|
|
|
|
var result = {};
|
|
|
|
var xml = response.xhr.responseXML;
|
|
|
|
var messages = xml.getElementsByTagNameNS('http://sabredav.org/ns', 'message');
|
|
|
|
var exceptions = xml.getElementsByTagNameNS('http://sabredav.org/ns', 'exception');
|
|
|
|
if (messages.length) {
|
|
|
|
result.message = messages[0].textContent;
|
|
|
|
}
|
|
|
|
if (exceptions.length) {
|
|
|
|
result.exception = exceptions[0].textContent;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2015-07-13 15:31:47 +00:00
|
|
|
/**
|
|
|
|
* Returns the default PROPFIND properties to use during a call.
|
|
|
|
*
|
|
|
|
* @return {Array.<Object>} array of properties
|
|
|
|
*/
|
2015-11-02 13:17:49 +00:00
|
|
|
getPropfindProperties: function() {
|
2015-07-13 15:31:47 +00:00
|
|
|
if (!this._propfindProperties) {
|
|
|
|
this._propfindProperties = _.map(Client._PROPFIND_PROPERTIES, function(propDef) {
|
|
|
|
return '{' + propDef[0] + '}' + propDef[1];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this._propfindProperties;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lists the contents of a directory
|
|
|
|
*
|
|
|
|
* @param {String} path path to retrieve
|
|
|
|
* @param {Object} [options] options
|
|
|
|
* @param {boolean} [options.includeParent=false] set to true to keep
|
|
|
|
* the parent folder in the result list
|
2015-11-02 13:17:49 +00:00
|
|
|
* @param {Array} [options.properties] list of Webdav properties to retrieve
|
2015-07-13 15:31:47 +00:00
|
|
|
*
|
|
|
|
* @return {Promise} promise
|
|
|
|
*/
|
|
|
|
getFolderContents: function(path, options) {
|
|
|
|
if (!path) {
|
|
|
|
path = '';
|
|
|
|
}
|
2015-11-02 13:17:49 +00:00
|
|
|
options = options || {};
|
2015-07-13 15:31:47 +00:00
|
|
|
var self = this;
|
|
|
|
var deferred = $.Deferred();
|
|
|
|
var promise = deferred.promise();
|
2015-11-02 13:17:49 +00:00
|
|
|
var properties;
|
|
|
|
if (_.isUndefined(options.properties)) {
|
|
|
|
properties = this.getPropfindProperties();
|
|
|
|
} else {
|
|
|
|
properties = options.properties;
|
|
|
|
}
|
2015-07-13 15:31:47 +00:00
|
|
|
|
|
|
|
this._client.propFind(
|
|
|
|
this._buildUrl(path),
|
2015-11-02 13:17:49 +00:00
|
|
|
properties,
|
2015-07-13 15:31:47 +00:00
|
|
|
1
|
|
|
|
).then(function(result) {
|
|
|
|
if (self._isSuccessStatus(result.status)) {
|
|
|
|
var results = self._parseResult(result.body);
|
|
|
|
if (!options || !options.includeParent) {
|
|
|
|
// remove root dir, the first entry
|
|
|
|
results.shift();
|
|
|
|
}
|
|
|
|
deferred.resolve(result.status, results);
|
|
|
|
} else {
|
2017-08-08 14:37:14 +00:00
|
|
|
result = _.extend(result, self._getSabreException(result));
|
|
|
|
deferred.reject(result.status, result);
|
2015-07-13 15:31:47 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return promise;
|
|
|
|
},
|
|
|
|
|
2016-02-08 10:43:42 +00:00
|
|
|
/**
|
|
|
|
* Fetches a flat list of files filtered by a given filter criteria.
|
|
|
|
* (currently only system tags is supported)
|
|
|
|
*
|
|
|
|
* @param {Object} filter filter criteria
|
|
|
|
* @param {Object} [filter.systemTagIds] list of system tag ids to filter by
|
2016-09-12 15:09:46 +00:00
|
|
|
* @param {bool} [filter.favorite] set it to filter by favorites
|
2016-02-08 10:43:42 +00:00
|
|
|
* @param {Object} [options] options
|
|
|
|
* @param {Array} [options.properties] list of Webdav properties to retrieve
|
|
|
|
*
|
|
|
|
* @return {Promise} promise
|
|
|
|
*/
|
|
|
|
getFilteredFiles: function(filter, options) {
|
|
|
|
options = options || {};
|
|
|
|
var self = this;
|
|
|
|
var deferred = $.Deferred();
|
|
|
|
var promise = deferred.promise();
|
|
|
|
var properties;
|
|
|
|
if (_.isUndefined(options.properties)) {
|
|
|
|
properties = this.getPropfindProperties();
|
|
|
|
} else {
|
|
|
|
properties = options.properties;
|
|
|
|
}
|
|
|
|
|
2016-09-12 15:09:46 +00:00
|
|
|
if (!filter || (!filter.systemTagIds && _.isUndefined(filter.favorite))) {
|
2016-02-08 10:43:42 +00:00
|
|
|
throw 'Missing filter argument';
|
|
|
|
}
|
|
|
|
|
|
|
|
// root element with namespaces
|
|
|
|
var body = '<oc:filter-files ';
|
|
|
|
var namespace;
|
|
|
|
for (namespace in this._client.xmlNamespaces) {
|
|
|
|
body += ' xmlns:' + this._client.xmlNamespaces[namespace] + '="' + namespace + '"';
|
|
|
|
}
|
|
|
|
body += '>\n';
|
|
|
|
|
|
|
|
// properties query
|
|
|
|
body += ' <' + this._client.xmlNamespaces['DAV:'] + ':prop>\n';
|
|
|
|
_.each(properties, function(prop) {
|
|
|
|
var property = self._client.parseClarkNotation(prop);
|
|
|
|
body += ' <' + self._client.xmlNamespaces[property.namespace] + ':' + property.name + ' />\n';
|
|
|
|
});
|
|
|
|
|
|
|
|
body += ' </' + this._client.xmlNamespaces['DAV:'] + ':prop>\n';
|
|
|
|
|
|
|
|
// rules block
|
|
|
|
body += ' <oc:filter-rules>\n';
|
|
|
|
_.each(filter.systemTagIds, function(systemTagIds) {
|
|
|
|
body += ' <oc:systemtag>' + escapeHTML(systemTagIds) + '</oc:systemtag>\n';
|
|
|
|
});
|
2016-09-12 15:09:46 +00:00
|
|
|
if (filter.favorite) {
|
|
|
|
body += ' <oc:favorite>' + (filter.favorite ? '1': '0') + '</oc:favorite>\n';
|
|
|
|
}
|
2016-02-08 10:43:42 +00:00
|
|
|
body += ' </oc:filter-rules>\n';
|
|
|
|
|
|
|
|
// end of root
|
|
|
|
body += '</oc:filter-files>\n';
|
|
|
|
|
|
|
|
this._client.request(
|
|
|
|
'REPORT',
|
|
|
|
this._buildUrl(),
|
2016-02-16 16:28:35 +00:00
|
|
|
{},
|
2016-02-08 10:43:42 +00:00
|
|
|
body
|
|
|
|
).then(function(result) {
|
|
|
|
if (self._isSuccessStatus(result.status)) {
|
|
|
|
var results = self._parseResult(result.body);
|
|
|
|
deferred.resolve(result.status, results);
|
|
|
|
} else {
|
2017-08-08 14:37:14 +00:00
|
|
|
result = _.extend(result, self._getSabreException(result));
|
|
|
|
deferred.reject(result.status, result);
|
2016-02-08 10:43:42 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return promise;
|
|
|
|
},
|
|
|
|
|
2015-07-13 15:31:47 +00:00
|
|
|
/**
|
|
|
|
* Returns the file info of a given path.
|
|
|
|
*
|
|
|
|
* @param {String} path path
|
2015-11-02 13:17:49 +00:00
|
|
|
* @param {Array} [options.properties] list of Webdav properties to retrieve
|
2015-07-13 15:31:47 +00:00
|
|
|
*
|
|
|
|
* @return {Promise} promise
|
|
|
|
*/
|
2015-11-02 13:17:49 +00:00
|
|
|
getFileInfo: function(path, options) {
|
2015-07-13 15:31:47 +00:00
|
|
|
if (!path) {
|
|
|
|
path = '';
|
|
|
|
}
|
2015-11-02 13:17:49 +00:00
|
|
|
options = options || {};
|
2015-07-13 15:31:47 +00:00
|
|
|
var self = this;
|
|
|
|
var deferred = $.Deferred();
|
|
|
|
var promise = deferred.promise();
|
2015-11-02 13:17:49 +00:00
|
|
|
var properties;
|
|
|
|
if (_.isUndefined(options.properties)) {
|
|
|
|
properties = this.getPropfindProperties();
|
|
|
|
} else {
|
|
|
|
properties = options.properties;
|
|
|
|
}
|
2015-07-13 15:31:47 +00:00
|
|
|
|
|
|
|
// TODO: headers
|
|
|
|
this._client.propFind(
|
|
|
|
this._buildUrl(path),
|
2015-11-02 13:17:49 +00:00
|
|
|
properties,
|
2015-07-13 15:31:47 +00:00
|
|
|
0
|
|
|
|
).then(
|
|
|
|
function(result) {
|
|
|
|
if (self._isSuccessStatus(result.status)) {
|
|
|
|
deferred.resolve(result.status, self._parseResult([result.body])[0]);
|
|
|
|
} else {
|
2017-08-08 14:37:14 +00:00
|
|
|
result = _.extend(result, self._getSabreException(result));
|
|
|
|
deferred.reject(result.status, result);
|
2015-07-13 15:31:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the contents of the given file.
|
|
|
|
*
|
|
|
|
* @param {String} path path to file
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
getFileContents: function(path) {
|
|
|
|
if (!path) {
|
|
|
|
throw 'Missing argument "path"';
|
|
|
|
}
|
|
|
|
var self = this;
|
|
|
|
var deferred = $.Deferred();
|
|
|
|
var promise = deferred.promise();
|
|
|
|
|
|
|
|
this._client.request(
|
|
|
|
'GET',
|
2016-02-16 16:28:35 +00:00
|
|
|
this._buildUrl(path)
|
2015-07-13 15:31:47 +00:00
|
|
|
).then(
|
|
|
|
function(result) {
|
|
|
|
if (self._isSuccessStatus(result.status)) {
|
|
|
|
deferred.resolve(result.status, result.body);
|
|
|
|
} else {
|
2017-08-08 14:37:14 +00:00
|
|
|
result = _.extend(result, self._getSabreException(result));
|
|
|
|
deferred.reject(result.status, result);
|
2015-07-13 15:31:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Puts the given data into the given file.
|
|
|
|
*
|
|
|
|
* @param {String} path path to file
|
|
|
|
* @param {String} body file body
|
|
|
|
* @param {Object} [options]
|
|
|
|
* @param {String} [options.contentType='text/plain'] content type
|
|
|
|
* @param {bool} [options.overwrite=true] whether to overwrite an existing file
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
putFileContents: function(path, body, options) {
|
|
|
|
if (!path) {
|
|
|
|
throw 'Missing argument "path"';
|
|
|
|
}
|
|
|
|
var self = this;
|
|
|
|
var deferred = $.Deferred();
|
|
|
|
var promise = deferred.promise();
|
|
|
|
options = options || {};
|
2016-02-16 16:28:35 +00:00
|
|
|
var headers = {};
|
2016-02-04 14:38:54 +00:00
|
|
|
var contentType = 'text/plain;charset=utf-8';
|
2015-07-13 15:31:47 +00:00
|
|
|
if (options.contentType) {
|
|
|
|
contentType = options.contentType;
|
|
|
|
}
|
|
|
|
|
|
|
|
headers['Content-Type'] = contentType;
|
|
|
|
|
|
|
|
if (_.isUndefined(options.overwrite) || options.overwrite) {
|
|
|
|
// will trigger 412 precondition failed if a file already exists
|
|
|
|
headers['If-None-Match'] = '*';
|
|
|
|
}
|
|
|
|
|
|
|
|
this._client.request(
|
|
|
|
'PUT',
|
|
|
|
this._buildUrl(path),
|
|
|
|
headers,
|
|
|
|
body || ''
|
|
|
|
).then(
|
|
|
|
function(result) {
|
|
|
|
if (self._isSuccessStatus(result.status)) {
|
|
|
|
deferred.resolve(result.status);
|
|
|
|
} else {
|
2017-08-08 14:37:14 +00:00
|
|
|
result = _.extend(result, self._getSabreException(result));
|
|
|
|
deferred.reject(result.status, result);
|
2015-07-13 15:31:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
_simpleCall: function(method, path) {
|
|
|
|
if (!path) {
|
|
|
|
throw 'Missing argument "path"';
|
|
|
|
}
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
var deferred = $.Deferred();
|
|
|
|
var promise = deferred.promise();
|
|
|
|
|
|
|
|
this._client.request(
|
|
|
|
method,
|
2016-02-16 16:28:35 +00:00
|
|
|
this._buildUrl(path)
|
2015-07-13 15:31:47 +00:00
|
|
|
).then(
|
|
|
|
function(result) {
|
|
|
|
if (self._isSuccessStatus(result.status)) {
|
|
|
|
deferred.resolve(result.status);
|
|
|
|
} else {
|
2017-08-08 14:37:14 +00:00
|
|
|
result = _.extend(result, self._getSabreException(result));
|
|
|
|
deferred.reject(result.status, result);
|
2015-07-13 15:31:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a directory
|
|
|
|
*
|
|
|
|
* @param {String} path path to create
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
createDirectory: function(path) {
|
|
|
|
return this._simpleCall('MKCOL', path);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a file or directory
|
|
|
|
*
|
|
|
|
* @param {String} path path to delete
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
remove: function(path) {
|
|
|
|
return this._simpleCall('DELETE', path);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Moves path to another path
|
|
|
|
*
|
|
|
|
* @param {String} path path to move
|
|
|
|
* @param {String} destinationPath destination path
|
|
|
|
* @param {boolean} [allowOverwrite=false] true to allow overwriting,
|
|
|
|
* false otherwise
|
2016-10-07 14:27:54 +00:00
|
|
|
* @param {Object} [headers=null] additional headers
|
2015-07-13 15:31:47 +00:00
|
|
|
*
|
|
|
|
* @return {Promise} promise
|
|
|
|
*/
|
2016-10-07 14:27:54 +00:00
|
|
|
move: function(path, destinationPath, allowOverwrite, headers) {
|
2015-07-13 15:31:47 +00:00
|
|
|
if (!path) {
|
|
|
|
throw 'Missing argument "path"';
|
|
|
|
}
|
|
|
|
if (!destinationPath) {
|
|
|
|
throw 'Missing argument "destinationPath"';
|
|
|
|
}
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
var deferred = $.Deferred();
|
|
|
|
var promise = deferred.promise();
|
2016-10-07 14:27:54 +00:00
|
|
|
headers = _.extend({}, headers, {
|
2016-02-16 16:28:35 +00:00
|
|
|
'Destination' : this._buildUrl(destinationPath)
|
2016-10-07 14:27:54 +00:00
|
|
|
});
|
2015-07-13 15:31:47 +00:00
|
|
|
|
|
|
|
if (!allowOverwrite) {
|
2017-05-01 03:45:41 +00:00
|
|
|
headers.Overwrite = 'F';
|
2015-07-13 15:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this._client.request(
|
|
|
|
'MOVE',
|
|
|
|
this._buildUrl(path),
|
2017-08-27 13:28:26 +00:00
|
|
|
headers
|
|
|
|
).then(
|
2017-08-08 14:37:14 +00:00
|
|
|
function(result) {
|
|
|
|
if (self._isSuccessStatus(result.status)) {
|
|
|
|
deferred.resolve(result.status);
|
2017-08-27 13:28:26 +00:00
|
|
|
} else {
|
2017-08-08 14:37:14 +00:00
|
|
|
result = _.extend(result, self._getSabreException(result));
|
|
|
|
deferred.reject(result.status, result);
|
2017-08-27 13:28:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies path to another path
|
|
|
|
*
|
|
|
|
* @param {String} path path to copy
|
|
|
|
* @param {String} destinationPath destination path
|
|
|
|
* @param {boolean} [allowOverwrite=false] true to allow overwriting,
|
|
|
|
* false otherwise
|
|
|
|
*
|
|
|
|
* @return {Promise} promise
|
|
|
|
*/
|
|
|
|
copy: function (path, destinationPath, allowOverwrite) {
|
|
|
|
if (!path) {
|
|
|
|
throw 'Missing argument "path"';
|
|
|
|
}
|
|
|
|
if (!destinationPath) {
|
|
|
|
throw 'Missing argument "destinationPath"';
|
|
|
|
}
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
var deferred = $.Deferred();
|
|
|
|
var promise = deferred.promise();
|
|
|
|
var headers = {
|
|
|
|
'Destination' : this._buildUrl(destinationPath)
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!allowOverwrite) {
|
|
|
|
headers.Overwrite = 'F';
|
|
|
|
}
|
|
|
|
|
|
|
|
this._client.request(
|
|
|
|
'COPY',
|
|
|
|
this._buildUrl(path),
|
2015-07-13 15:31:47 +00:00
|
|
|
headers
|
|
|
|
).then(
|
|
|
|
function(response) {
|
|
|
|
if (self._isSuccessStatus(response.status)) {
|
|
|
|
deferred.resolve(response.status);
|
|
|
|
} else {
|
|
|
|
deferred.reject(response.status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return promise;
|
2015-11-02 13:17:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a file info parser function
|
|
|
|
*
|
2018-10-24 14:24:16 +00:00
|
|
|
* @param {OC.Files.Client~parseFileInfo} parserFunction
|
2015-11-02 13:17:49 +00:00
|
|
|
*/
|
|
|
|
addFileInfoParser: function(parserFunction) {
|
|
|
|
this._fileInfoParsers.push(parserFunction);
|
2016-09-03 16:06:35 +00:00
|
|
|
},
|
2015-07-13 15:31:47 +00:00
|
|
|
|
2016-09-03 16:06:35 +00:00
|
|
|
/**
|
|
|
|
* Returns the dav.Client instance used internally
|
|
|
|
*
|
2016-11-15 17:51:52 +00:00
|
|
|
* @since 11.0.0
|
2016-09-03 16:06:35 +00:00
|
|
|
* @return {dav.Client}
|
|
|
|
*/
|
|
|
|
getClient: function() {
|
|
|
|
return this._client;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the user name
|
|
|
|
*
|
2016-11-15 17:51:52 +00:00
|
|
|
* @since 11.0.0
|
2016-09-03 16:06:35 +00:00
|
|
|
* @return {String} userName
|
|
|
|
*/
|
|
|
|
getUserName: function() {
|
|
|
|
return this._client.userName;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-11-21 14:03:45 +00:00
|
|
|
* Returns the password
|
2016-09-03 16:06:35 +00:00
|
|
|
*
|
2016-11-15 17:51:52 +00:00
|
|
|
* @since 11.0.0
|
2016-09-03 16:06:35 +00:00
|
|
|
* @return {String} password
|
|
|
|
*/
|
|
|
|
getPassword: function() {
|
|
|
|
return this._client.password;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the base URL
|
|
|
|
*
|
2016-11-15 17:51:52 +00:00
|
|
|
* @since 11.0.0
|
2016-09-03 16:06:35 +00:00
|
|
|
* @return {String} base URL
|
|
|
|
*/
|
|
|
|
getBaseUrl: function() {
|
|
|
|
return this._client.baseUrl;
|
2016-10-07 14:27:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the host
|
|
|
|
*
|
|
|
|
* @since 13.0.0
|
|
|
|
* @return {String} base URL
|
|
|
|
*/
|
|
|
|
getHost: function() {
|
|
|
|
return this._host;
|
2016-09-03 16:06:35 +00:00
|
|
|
}
|
2015-07-13 15:31:47 +00:00
|
|
|
};
|
|
|
|
|
2015-11-02 13:17:49 +00:00
|
|
|
/**
|
|
|
|
* File info parser function
|
|
|
|
*
|
|
|
|
* This function receives a list of Webdav properties as input and
|
|
|
|
* should return a hash array of parsed properties, if applicable.
|
|
|
|
*
|
|
|
|
* @callback OC.Files.Client~parseFileInfo
|
|
|
|
* @param {Object} XML Webdav properties
|
|
|
|
* @return {Array} array of parsed property values
|
|
|
|
*/
|
|
|
|
|
2015-07-13 15:31:47 +00:00
|
|
|
if (!OC.Files) {
|
|
|
|
/**
|
|
|
|
* @namespace OC.Files
|
|
|
|
*
|
|
|
|
* @since 8.2
|
|
|
|
*/
|
|
|
|
OC.Files = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the default instance of the files client
|
|
|
|
*
|
|
|
|
* @return {OC.Files.Client} default client
|
|
|
|
*
|
|
|
|
* @since 8.2
|
|
|
|
*/
|
|
|
|
OC.Files.getClient = function() {
|
|
|
|
if (OC.Files._defaultClient) {
|
|
|
|
return OC.Files._defaultClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
var client = new OC.Files.Client({
|
|
|
|
host: OC.getHost(),
|
|
|
|
port: OC.getPort(),
|
2018-05-08 21:02:19 +00:00
|
|
|
root: OC.linkToRemoteBase('dav') + '/files/' + OC.getCurrentUser().uid,
|
2015-07-13 15:31:47 +00:00
|
|
|
useHTTPS: OC.getProtocol() === 'https'
|
|
|
|
});
|
|
|
|
OC.Files._defaultClient = client;
|
|
|
|
return client;
|
|
|
|
};
|
|
|
|
|
|
|
|
OC.Files.Client = Client;
|
|
|
|
})(OC, OC.Files.FileInfo);
|