-
Notifications
You must be signed in to change notification settings - Fork 0
WebDavService Examples
Dmytro Katashev edited this page Aug 13, 2024
·
2 revisions
This example demonstrates how to upload a file to a WebDAV server using the WebDavService class.
var WebDavService = require('*/cartridge/scripts/webservice/WebDavService');
// Define a custom WebDAV service
var FileUploadService = WebDavService.extend({
SERVICE_CONFIGURATIONS: {
default: 'webdav.upload.service',
},
/**
* Upload a file to the WebDAV server.
*
* @param {string} localFilePath - The local path of the file to upload.
* @param {string} remoteFilePath - The destination path on the WebDAV server.
* @returns {dw.svc.Result} - The WebDAV service result.
*/
uploadFile: function(localFilePath, remoteFilePath) {
var File = require('dw/io/File');
var localFile = new File(localFilePath);
return this.fetch({
operation: 'put',
args: [remoteFilePath, localFile]
});
}
});
// Perform the WebDAV upload
var result = FileUploadService.uploadFile('IMPEX/local/path/file.txt', '/remote/path/file.txt');This example demonstrates how to download a file from a WebDAV server.
var WebDavService = require('*/cartridge/scripts/webservice/WebDavService');
// Define a custom WebDAV service
var FileDownloadService = WebDavService.extend({
SERVICE_CONFIGURATIONS: {
default: 'webdav.download.service',
},
/**
* Download a file from the WebDAV server.
*
* @param {string} remoteFilePath - The path of the file to download.
* @param {string} localFilePath - The path where the file should be saved locally.
* @returns {dw.svc.Result} - The WebDAV service result.
*/
downloadFile: function(remoteFilePath, localFilePath) {
var File = require('dw/io/File');
var localFile = new File(localFilePath);
return this.fetch({
operation: 'getBinary',
args: [remoteFilePath, localFile]
});
}
});
// Perform the WebDAV download
var result = FileDownloadService.downloadFile('/remote/path/file.txt', 'IMPEX/local/path/file.txt');This example shows how to list the contents of a directory on a WebDAV server.
var WebDavService = require('*/cartridge/scripts/webservice/WebDavService');
// Define a custom WebDAV service
var DirectoryListingService = WebDavService.extend({
SERVICE_CONFIGURATIONS: {
default: 'webdav.list.service',
},
/**
* List the contents of a directory on the WebDAV server.
*
* @param {string} remoteDirectoryPath - The path of the directory to list.
* @returns {dw.svc.Result} - The WebDAV service result.
*/
listDirectory: function(remoteDirectoryPath) {
return this.fetch({
operation: 'propfind',
args: [remoteDirectoryPath]
});
}
});
// Perform the directory listing
var result = DirectoryListingService.listDirectory('/remote/path/directory');This example demonstrates how to create a directory on a WebDAV server.
var WebDavService = require('*/cartridge/scripts/webservice/WebDavService');
// Define a custom WebDAV service
var DirectoryCreationService = WebDavService.extend({
SERVICE_CONFIGURATIONS: {
default: 'webdav.mkdir.service',
},
/**
* Create a directory on the WebDAV server.
*
* @param {string} remoteDirectoryPath - The path of the directory to create.
* @returns {dw.svc.Result} - The WebDAV service result.
*/
createDirectory: function(remoteDirectoryPath) {
return this.fetch({
operation: 'mkcol',
args: [remoteDirectoryPath]
});
}
});
// Create the directory on WebDAV
var result = DirectoryCreationService.createDirectory('/remote/path/newdirectory');This example demonstrates how to delete a file from a WebDAV server.
var WebDavService = require('*/cartridge/scripts/webservice/WebDavService');
// Define a custom WebDAV service
var FileDeletionService = WebDavService.extend({
SERVICE_CONFIGURATIONS: {
default: 'webdav.delete.service',
},
/**
* Delete a file from the WebDAV server.
*
* @param {string} remoteFilePath - The path of the file to delete.
* @returns {dw.svc.Result} - The WebDAV service result.
*/
deleteFile: function(remoteFilePath) {
return this.fetch({
operation: 'del',
args: [remoteFilePath]
});
}
});
// Delete the file from WebDAV
var result = FileDeletionService.deleteFile('/remote/path/file.txt');This example demonstrates how to perform multiple operations within a single service call, such as creating a directory, uploading a file, and listing the contents.
var WebDavService = require('*/cartridge/scripts/webservice/WebDavService');
// Define a custom WebDAV service
var MultiOperationService = WebDavService.extend({
SERVICE_CONFIGURATIONS: {
default: 'webdav.multioperation.service',
},
/**
* Manage files on the WebDAV server by performing multiple operations.
*
* @param {string} remoteDirectoryPath - The path of the directory to manage.
* @param {string} localFilePath - The local path of the file to upload.
* @param {string} remoteFileName - The name for the uploaded file on the WebDAV server.
* @returns {dw.svc.Result} - The WebDAV service result.
*/
manageFiles: function(remoteDirectoryPath, localFilePath, remoteFileName) {
return this.fetch({
onExecute: function(svc) {
var client = svc.client;
// Create a new directory on WebDAV
if (client.mkcol(remoteDirectoryPath)) {
// Upload a file to the newly created directory
client.put(localFilePath, remoteDirectoryPath + '/' + remoteFileName);
}
// List the contents of the directory after the operations
return client.propfind(remoteDirectoryPath);
}
});
}
});
// Perform the WebDAV operations
var result = MultiOperationService.manageFiles('/remote/path/newdirectory', 'IMPEX/local/path/file.txt', 'uploadedfile.txt');