Promise.resolve(document.save()) .then( () => { return Promise.reject(new Error(field +' required validation failed')); }).catch((err) => { debug(err); should.exist(err); Object.keys(err.errors).length.should.be.exactly(errorsLength ? errorsLength : 1); err.errors.should.have.property(field); })
/** * Disables an app via the Provisioning API * @param {string} appName name of the app to be disabled * @returns {Promise.<status>} boolean: true if successful * @returns {Promise.<error>} string: error message, if any. */ disableApp (appName) { return this.helpers._makeOCSrequest('DELETE', this.helpers.OCS_SERVICE_CLOUD, 'apps/' + encodeURIComponent(appName)) .then(data => { const statusCode = parseInt(this.helpers._checkOCSstatusCode(data.data)) if (statusCode === 999) { return Promise.reject('Provisioning API has been disabled at your instance') } return Promise.resolve(true) }) }
_sendDavReport (body) { if (!this.helpers.getAuthorization()) { return Promise.reject('Please specify an authorization first.') } return this.helpers.getCurrentUserAsync().then(user => { const path = '/files/' + user.id + '/' const headers = this.helpers.buildHeaders() headers['Content-Type'] = 'application/xml; charset=utf-8' return this.davClient.request('REPORT', this.helpers._buildFullWebDAVPathV2(path), headers, body).then(result => { if (result.status !== 207) { return Promise.reject(this.helpers.buildHttpErrorFromDavResponse(result.status, result.body)) } else { return Promise.resolve(this.helpers._parseBody(result.body, 2)) } }) }) }
/** * Returns a list of versions for the given file id * @param {string|number} fileId file id * @returns {Promise.<fileInfo>} Array[objects]: each object is an instance of class fileInfo * @returns {Promise.<error>} string: error message, if any. */ listVersions (fileId) { const path = '/meta/' + fileId + '/v' return this.davClient.propFind(this.helpers._buildFullWebDAVPathV2(path), [], 1, { Authorization: this.helpers.getAuthorization() }).then(result => { if (result.status !== 207) { return Promise.reject(this.helpers.buildHttpErrorFromDavResponse(result.status, result.body)) } else { return Promise.resolve(this.helpers._parseBody(result.body).splice(1)) } }) }
/** * Moves a remote file or directory * @param {string} source initial path of file/folder - including the token aka root folder * @param {string} target path where to move file/folder finally - including the token aka root folder * @param {string|null} password * @return {Promise.<status>} boolean: whether the operation was successful * @return {Promise.<error>} string: error message, if any. */ move (source, target, password = null) { const headers = this.helpers.buildHeaders(false) const sourceUrl = this.getFileUrl(source) const targetUrl = this.getFileUrl(target) if (password) { headers.authorization = 'Basic ' + Buffer.from('public:' + password).toString('base64') } headers.Destination = targetUrl return this.davClient.request('MOVE', sourceUrl, headers).then(result => { if ([200, 201, 204, 207].indexOf(result.status) > -1) { return Promise.resolve(true) } return Promise.reject(this.helpers.buildHttpErrorFromDavResponse(result.status, result.body)) }) }
Promise.resolve(document.save()) .then( () => { return Promise.reject(new Error(field +' unique validation failed')); }).catch((err) => { should.exist(err); })
/** * Deletes a remote file or directory * @param {string} path path of the file/folder at OC instance * @returns {Promise.<status>} boolean: whether the operation was successful * @returns {Promise.<error>} string: error message, if any. */ delete (path) { if (!this.helpers.getAuthorization()) { return Promise.reject('Please specify an authorization first.') } return this.davClient.request('DELETE', this.helpers._buildFullWebDAVPath(path), this.helpers.buildHeaders()).then(result => { if ([200, 201, 204, 207].indexOf(result.status) > -1) { return Promise.resolve(true) } else { return Promise.reject(this.helpers.buildHttpErrorFromDavResponse(result.status, result.body)) } }) }
/** * Creates a remote directory * @param {string} path path of the folder to be created at OC instance * @returns {Promise.<status>} boolean: whether the operation was successful * @returns {Promise.<error>} string: error message, if any. */ mkdir (path) { if (path[path.length - 1] !== '/') { path += '/' } if (!this.helpers.getAuthorization()) { return Promise.reject('Please specify an authorization first.') } return this.davClient.request('MKCOL', this.helpers._buildFullWebDAVPath(path), this.helpers.buildHeaders()).then(result => { if ([200, 201, 204, 207].indexOf(result.status) > -1) { return Promise.resolve(true) } return Promise.reject(this.helpers.buildHttpErrorFromDavResponse(result.status, result.body)) }) }
/** * Creates a remote directory in a public folder * @param {string} token public share token - may contain the path as well * @param {string|null} path path of the folder to be created at OC instance * @param {string|null} password * @return {Promise.<status>} boolean: whether the operation was successful * @return {Promise.<error>} string: error message, if any. */ createFolder (token, path = null, password = null) { const headers = this.helpers.buildHeaders(false) const url = this.getFileUrl(token, path) if (password) { headers.authorization = 'Basic ' + Buffer.from('public:' + password).toString('base64') } return this.davClient.request('MKCOL', url, headers).then(result => { if ([200, 201, 204, 207].indexOf(result.status) > -1) { return Promise.resolve(true) } return Promise.reject(this.helpers.buildHttpErrorFromDavResponse(result.status, result.body)) }) }
/** * Returns the content of a version of a remote file version * @param {string} fileId id of the remote file at OC instance * @param {string} versionId id of the version of the remote file at OC instance */ getFileVersionContents (fileId, versionId) { const path = '/meta/' + fileId + '/v/' + versionId return this.helpers._get(this.helpers._buildFullWebDAVPathV2(path)).then(data => { const response = data.response const body = data.body if (response.statusCode === 200) { return Promise.resolve(body) } else { return Promise.reject(this.helpers.buildHttpErrorFromDavResponse(response.status, body)) } }) }
/** * Moves a remote file or directory * @param {string} source initial path of file/folder * @param {string} target path where to move file/folder finally * @returns {Promise.<status>} boolean: whether the operation was successful * @returns {Promise.<error>} string: error message, if any. */ move (source, target) { if (!this.helpers.getAuthorization()) { return Promise.reject('Please specify an authorization first.') } const headers = this.helpers.buildHeaders() headers.Destination = this.helpers._buildFullWebDAVPath(target) return this.davClient.request('MOVE', this.helpers._buildFullWebDAVPath(source), headers).then(result => { if ([200, 201, 204, 207].indexOf(result.status) > -1) { return Promise.resolve(true) } return Promise.reject(this.helpers.buildHttpErrorFromDavResponse(result.status, result.body)) }) }
/** * Enables an app via the Provisioning API * @param {string} appName name of the app to be enabled * @returns {Promise.<status>} boolean: true if successful * @returns {Promise.<error>} string: error message, if any. */ enableApp (appName) { return this.helpers._makeOCSrequest('POST', this.helpers.OCS_SERVICE_CLOUD, 'apps/' + encodeURIComponent(appName)) .then(data => { if (!data.body) { return Promise.reject('No app found by the name "' + appName + '"') } const statusCode = parseInt(this.helpers._checkOCSstatusCode(data.data)) if (statusCode === 999) { return Promise.reject('Provisioning API has been disabled at your instance') } return Promise.resolve(true) }) }
/** * Queries the server for the real path of the authenticated user for a given fileId * * @param {number} fileId * @return {*|Promise<string>} */ getPathForFileId (fileId) { const path = '/meta/' + fileId return this.davClient.propFind(this.helpers._buildFullWebDAVPathV2(path), [ '{http://owncloud.org/ns}meta-path-for-user' ], 0, { Authorization: this.helpers.getAuthorization() }).then(result => { if (result.status !== 207) { return Promise.reject(this.helpers.buildHttpErrorFromDavResponse(result.status, result.body)) } const file = this.helpers._parseBody(result.body) return Promise.resolve(file[0].getProperty('{http://owncloud.org/ns}meta-path-for-user')) }) }
/** * Mark a remote file or directory as favorite * @param {string} path path of file/folder * @param {boolean} value Add or remove the favorite marker of a file/folder * @returns {Promise.<status>} boolean: whether the operation was successful * @returns {Promise.<error>} string: error message, if any. */ favorite (path, value = true) { if (!this.helpers.getAuthorization()) { return Promise.reject('Please specify an authorization first.') } return this.davClient.propPatch(this.helpers._buildFullWebDAVPath(path), { '{http://owncloud.org/ns}favorite': value ? 'true' : 'false' }, this.helpers.buildHeaders()).then(result => { if ([200, 201, 204, 207].indexOf(result.status) > -1) { return Promise.resolve(true) } return Promise.reject(this.helpers.buildHttpErrorFromDavResponse(result.status, result.body)) }) }
/** * Copies a remote file or directory * @param {string} source initial path of file/folder * @param {string} target path where to copy file/folder finally * @returns {Promise.<status>} boolean: whether the operation was successful * @returns {Promise.<error>} string: error message, if any. */ copy (source, target) { if (!this.helpers.getAuthorization()) { return Promise.reject('Please specify an authorization first.') } const headers = this.helpers.buildHeaders() headers.Destination = this.helpers._buildFullWebDAVPath(target) return this.davClient.request('COPY', this.helpers._buildFullWebDAVPath(source), headers).then(result => { if ([200, 201, 204, 207].indexOf(result.status) > -1) { return Promise.resolve(true) } return Promise.reject(this.helpers.buildHttpErrorFromDavResponse(result.status, result.body)) }) }