|
|
|
@ -1,3 +1,4 @@
|
|
|
|
|
import type { isFile } from '@push.rocks/smartpath/dist_ts/smartpath.check.js';
|
|
|
|
|
import * as plugins from './plugins.js';
|
|
|
|
|
|
|
|
|
|
export interface IWebdavClientOptions {
|
|
|
|
@ -34,6 +35,18 @@ export class WebdavClient {
|
|
|
|
|
return result as plugins.webdav.FileStat[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async ensureDirectory(path: string): Promise<void> {
|
|
|
|
|
console.log(`Ensuring directory at ${path}`);
|
|
|
|
|
const pathLevels = plugins.smartpath.get.pathLevels(path);
|
|
|
|
@ -100,7 +113,27 @@ export class WebdavClient {
|
|
|
|
|
await this.wdClient.moveFile(sourcePathArg, targetPathArg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async deleteDirectory(pathArg: string) {
|
|
|
|
|
public async deleteFile(pathArg: string, checkNotDirectory = true) {
|
|
|
|
|
if (!(await this.wdClient.exists(pathArg))) {
|
|
|
|
|
throw new Error(`Path does not exist`);
|
|
|
|
|
}
|
|
|
|
|
if (checkNotDirectory && (await this.isDirecory(pathArg))) {
|
|
|
|
|
throw new Error(`Path is a directory, not a file`);
|
|
|
|
|
}
|
|
|
|
|
await this.wdClient.deleteFile(pathArg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async deleteDirectory(pathArg: string) {
|
|
|
|
|
await this.deleteFile(pathArg, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async isFile(pathArg: string) {
|
|
|
|
|
const stat = (await this.wdClient.stat(pathArg)) as plugins.webdav.FileStat;
|
|
|
|
|
return stat.type === 'file';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async isDirecory(pathArg: string) {
|
|
|
|
|
const stat = (await this.wdClient.stat(pathArg)) as plugins.webdav.FileStat;
|
|
|
|
|
return stat.type === 'directory';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|