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 getDirectoryAsSmartfileArray(pathArg: string): Promise { const directoryListing = await this.listDirectory(pathArg); const smartfileArray: plugins.smartfile.SmartFile[] = []; for (const file of directoryListing) { const fileContents = (await this.wdClient.getFileContents(file.filename, { format: 'binary', })) as Buffer; smartfileArray.push(await plugins.smartfile.SmartFile.fromBuffer(file.filename, fileContents)); } return smartfileArray; } public async ensureDirectory(path: string): Promise { 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 { 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); } }