smartwebdav/ts/classes.webdavclient.ts
2024-04-20 21:41:30 +02:00

107 lines
3.5 KiB
TypeScript

import * as plugins from './plugins.js';
export interface IWebdavClientOptions {
serverUrl: string;
authType?: plugins.webdav.AuthType;
username?: string;
password?: string;
}
export class WebdavClient {
wdClient: plugins.webdav.WebDAVClient;
constructor(optionsArg: IWebdavClientOptions) {
this.wdClient = plugins.webdav.createClient(optionsArg.serverUrl, {
...optionsArg.authType ? {
authType: optionsArg.authType,
} : {
authType: plugins.webdav.AuthType.Password,
},
...(optionsArg.username
? {
username: optionsArg.username,
}
: {}),
...(optionsArg.password
? {
password: optionsArg.password,
}
: {}),
});
}
public async listDirectory(pathArg: string) {
const result = await this.wdClient.getDirectoryContents('/');
return result as plugins.webdav.FileStat[];
}
public async ensureDirectory(path: string): Promise<void> {
console.log(`Ensuring directory at ${path}`);
const pathLevels = plugins.smartpath.get.pathLevels(path);
let pathTrace = ``;
for (const pathLevel of pathLevels) {
pathTrace += `/${pathLevel}`;
try {
const exists = await this.wdClient.exists(pathTrace);
if (!exists) {
await this.wdClient.createDirectory(pathTrace);
console.log(`Directory created at ${pathTrace}`);
} else {
console.log(`Directory already exists at ${pathTrace}`);
}
} catch (error) {
console.error(`Error ensuring directory: ${error}`);
throw error; // Re-throw to handle it according to your application's logic
}
}
}
public async ensureEmptyDirectory(path: string): Promise<void> {
console.log(`Ensuring empty directory at ${path}`);
try {
const exists = await this.wdClient.exists(path);
if (!exists) {
await this.wdClient.createDirectory(path);
console.log(`Directory created at ${path}`);
} else {
console.log(`Directory already exists at ${path}`);
const directoryListing = await this.listDirectory(path);
for (const file of directoryListing) {
await this.wdClient.deleteFile(file.filename);
}
console.log(`Directory emptied at ${path}`);
}
} catch (error) {
console.error(`Error ensuring directory: ${error}`);
throw error; // Re-throw to handle it according to your application's logic
}
}
private async prepareFileName(fileName: string) {
fileName.startsWith('../') ? (fileName = fileName.slice(1)) : null;
return fileName;
}
public async uploadSmartFileArray(smartfileArrayArg: plugins.smartfile.SmartFile[]) {
for (const smartfileArg of smartfileArrayArg) {
const cleanFileName = await this.prepareFileName(smartfileArg.relative);
await this.ensureDirectory(plugins.smartpath.get.dirname(cleanFileName));
await this.wdClient.putFileContents(cleanFileName, smartfileArg.contents);
}
}
public async deleteSmartfileArray(smartfileArrayArg: plugins.smartfile.SmartFile[]) {
for (const smartfileArg of smartfileArrayArg) {
const cleanFileName = await this.prepareFileName(smartfileArg.relative);
await this.wdClient.deleteFile(cleanFileName);
}
}
public async move(sourcePathArg: string, targetPathArg: string) {
await this.wdClient.moveFile(sourcePathArg, targetPathArg);
}
public async deleteDirectory(pathArg: string) {
await this.wdClient.deleteFile(pathArg);
}
}