smartexpose/ts/classes.exposeprovider.webdav.ts

142 lines
4.6 KiB
TypeScript
Raw Normal View History

2024-04-22 11:13:00 +00:00
import { ExposeProvider } from './classes.exposeprovider.js';
2024-04-24 16:02:21 +00:00
import type { SmartExpose } from './classes.smartexpose.js';
2024-04-22 11:13:00 +00:00
import * as plugins from './plugins.js';
export interface IWebdavExposeProviderOptions {
2024-04-24 16:02:21 +00:00
webdavCredentials: plugins.smartwebdav.IWebdavClientOptions;
webdavSubPath: string;
2024-04-22 11:13:00 +00:00
}
export class WebDavExposeProvider extends ExposeProvider {
2024-04-24 16:02:21 +00:00
public smartExposeRef: SmartExpose;
2024-04-22 11:13:00 +00:00
public webdavClient: plugins.smartwebdav.WebdavClient;
public options: IWebdavExposeProviderOptions;
2024-04-24 16:14:57 +00:00
constructor(smartexposeRefArg: SmartExpose, optionsArg: IWebdavExposeProviderOptions) {
2024-04-22 11:13:00 +00:00
super();
2024-04-24 16:14:57 +00:00
this.smartExposeRef = smartexposeRefArg;
2024-04-22 11:13:00 +00:00
this.options = optionsArg;
}
2024-04-24 16:02:21 +00:00
public async houseKeeping(): Promise<void> {
2024-04-22 11:13:00 +00:00
const directoryContents = await this.webdavClient.listDirectory(this.options.webdavSubPath);
for (const fileStat of directoryContents) {
// lets read the meta.json file
if (fileStat.filename.endsWith('.json')) {
}
}
}
2024-04-24 16:02:21 +00:00
public async getFilePathById(idArg: string): Promise<string> {
return plugins.smartpath.join(this.options.webdavSubPath, idArg);
}
public async ensureBaseDir(): Promise<void> {
2024-04-22 11:13:00 +00:00
await this.webdavClient.ensureDirectory(this.options.webdavSubPath);
}
2024-04-24 16:02:21 +00:00
public async start(): Promise<void> {
2024-04-22 11:13:00 +00:00
this.webdavClient = new plugins.smartwebdav.WebdavClient(this.options.webdavCredentials);
await this.ensureBaseDir();
await this.houseKeeping();
2024-04-26 11:51:45 +00:00
this.smartExposeRef.taskmanager.addAndScheduleTask(
new plugins.taskbuffer.Task({
name: 'webdavHousekeeping',
taskFunction: async () => {
await this.houseKeeping();
},
}),
'0 * * * * *'
);
2024-04-22 11:13:00 +00:00
}
2024-04-24 16:02:21 +00:00
public async stop(): Promise<void> {
2024-04-22 11:13:00 +00:00
// Nothing to do here
}
2024-04-26 11:51:45 +00:00
public async exposeFile(
optionsArg: Parameters<ExposeProvider['exposeFile']>[0]
): Promise<{ url: string; id: string; status: 'created' | 'updated' }> {
2024-04-22 11:13:00 +00:00
await this.ensureBaseDir();
2024-04-24 16:02:21 +00:00
const fileId = plugins.smartunique.shortId(30);
2024-04-26 11:51:45 +00:00
console.log(
`Expsing file under id: ${fileId}. (${plugins.smartformat.prettyBytes(
optionsArg.smartFile.contents.length
)})`
);
2024-04-24 16:02:21 +00:00
const webdavFilePath = await this.getFilePathById(fileId);
2024-04-26 11:51:45 +00:00
const fileToUpload = await plugins.smartfile.SmartFile.fromBuffer(
webdavFilePath,
optionsArg.smartFile.contents
);
2024-04-24 16:02:21 +00:00
await this.webdavClient.uploadSmartFileArray([fileToUpload]);
console.log(`checking file presence: ${webdavFilePath}`);
const existsOnWebdav = await this.webdavClient.wdClient.exists(webdavFilePath);
2024-04-26 11:51:45 +00:00
const publicUrl = plugins.smartpath.join(
this.smartExposeRef.options.exposedBaseUrl,
webdavFilePath
);
console.log(`cehcking for file at ${publicUrl}`);
2024-04-24 16:02:21 +00:00
const response = await plugins.smartrequest.getBinary(publicUrl);
plugins.smartexpect.expect(response.body).toEqual(fileToUpload.contents);
if (optionsArg.deleteAfterMillis) {
2024-04-26 11:51:45 +00:00
console.log(
`Scheduling deletion of file with id: ${fileId} in ${optionsArg.deleteAfterMillis}ms...`
);
2024-04-24 16:02:21 +00:00
plugins.smartdelay.delayFor(optionsArg.deleteAfterMillis).then(() => {
console.log(`Deleting file with id: ${fileId}...`);
this.deleteFileById(fileId);
console.log(`Deleted file with id: ${fileId}`);
});
}
return {
url: publicUrl,
id: fileId,
status: 'created',
};
2024-04-22 11:13:00 +00:00
}
2024-04-26 11:51:45 +00:00
public async exposeFileArray(optionsArg: Parameters<ExposeProvider['exposeFileArray']>[0]) {
const returnArray = [];
for (const smartFile of optionsArg.smartFiles) {
returnArray.push(
await this.exposeFile({
smartFile,
deleteAfterMillis: optionsArg.deleteAfterMillis,
privateUrl: optionsArg.privateUrl,
})
);
}
return returnArray;
}
2024-04-24 16:02:21 +00:00
public async wipeAll(): Promise<{ id: string; status: 'deleted' | 'failed' | 'notfound' }[]> {
const directoryContents = await this.webdavClient.listDirectory(this.options.webdavSubPath);
await this.webdavClient.ensureEmptyDirectory(this.options.webdavSubPath);
return directoryContents.map((contentArg) => {
return {
id: contentArg.filename,
status: 'deleted',
};
});
2024-04-22 11:13:00 +00:00
}
2024-04-24 16:02:21 +00:00
public async deleteFileById(
idArg: string
): Promise<{ id: string; status: 'deleted' | 'failed' | 'notfound' }> {
const filePath = await this.getFilePathById(idArg);
if (!this.webdavClient.wdClient.exists(filePath)) {
return {
id: idArg,
status: 'notfound',
};
}
await this.webdavClient.deleteFile(filePath);
return {
id: idArg,
status: 'deleted',
};
2024-04-22 11:13:00 +00:00
}
2024-04-24 16:02:21 +00:00
}