2024-04-20 19:24:55 +00:00
|
|
|
import * as plugins from './plugins.js';
|
|
|
|
|
|
|
|
export interface IWebdavClientOptions {
|
|
|
|
serverUrl: string;
|
2024-04-20 19:41:30 +00:00
|
|
|
authType?: plugins.webdav.AuthType;
|
2024-04-20 19:24:55 +00:00
|
|
|
username?: string;
|
|
|
|
password?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class WebdavClient {
|
|
|
|
wdClient: plugins.webdav.WebDAVClient;
|
|
|
|
constructor(optionsArg: IWebdavClientOptions) {
|
|
|
|
this.wdClient = plugins.webdav.createClient(optionsArg.serverUrl, {
|
2024-04-20 19:41:30 +00:00
|
|
|
...optionsArg.authType ? {
|
|
|
|
authType: optionsArg.authType,
|
|
|
|
} : {
|
|
|
|
authType: plugins.webdav.AuthType.Password,
|
|
|
|
},
|
2024-04-20 19:24:55 +00:00
|
|
|
...(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[];
|
|
|
|
}
|
|
|
|
|
2024-04-22 11:01:11 +00:00
|
|
|
public async getDirectoryAsSmartfileArray(pathArg: string): Promise<plugins.smartfile.SmartFile[]> {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-04-20 19:24:55 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|