fix(core): update
This commit is contained in:
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartwebdav',
|
||||
version: '1.0.3',
|
||||
description: 'A TypeScript library for easy interaction with WebDAV servers, including file and directory management.'
|
||||
}
|
102
ts/classes.webdavclient.ts
Normal file
102
ts/classes.webdavclient.ts
Normal file
@ -0,0 +1,102 @@
|
||||
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, {
|
||||
authType: optionsArg.authType,
|
||||
...(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);
|
||||
}
|
||||
}
|
4
ts/index.ts
Normal file
4
ts/index.ts
Normal file
@ -0,0 +1,4 @@
|
||||
import * as plugins from './plugins.js';
|
||||
export * from './classes.webdavclient.js';
|
||||
|
||||
export const authType = plugins.webdav.AuthType;
|
15
ts/plugins.ts
Normal file
15
ts/plugins.ts
Normal file
@ -0,0 +1,15 @@
|
||||
// @push.rocks scope
|
||||
import * as smartfile from '@push.rocks/smartfile';
|
||||
import * as smartpath from '@push.rocks/smartpath'
|
||||
|
||||
export {
|
||||
smartfile,
|
||||
smartpath,
|
||||
}
|
||||
|
||||
// third party scope
|
||||
import * as webdav from 'webdav';
|
||||
|
||||
export {
|
||||
webdav,
|
||||
}
|
Reference in New Issue
Block a user