From d82d9e4cfaa7fac6640c2502d55df0d44481dcbe Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Fri, 26 Apr 2024 13:51:45 +0200 Subject: [PATCH] fix(core): update --- ts/00_commitinfo_data.ts | 2 +- ts/classes.exposeprovider.ts | 10 +++++ ts/classes.exposeprovider.webdav.ts | 61 ++++++++++++++++++++--------- ts/classes.smartexpose.ts | 4 ++ 4 files changed, 58 insertions(+), 19 deletions(-) diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 2cf34d3..3f4b7e5 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartexpose', - version: '1.0.5', + version: '1.0.6', description: 'a package to expose things to the internet' } diff --git a/ts/classes.exposeprovider.ts b/ts/classes.exposeprovider.ts index eda7c3d..c18c6a3 100644 --- a/ts/classes.exposeprovider.ts +++ b/ts/classes.exposeprovider.ts @@ -28,6 +28,16 @@ export abstract class ExposeProvider { status: 'created' | 'updated'; }>; + public abstract exposeFileArray(optionsArg: { + smartFiles: plugins.smartfile.SmartFile[], + deleteAfterMillis?: number, + privateUrl?: boolean, + }): Promise<{ + url: string; + id: string; + status: 'created' | 'updated'; + }[]>; + public abstract start(): Promise; public abstract stop(): Promise; diff --git a/ts/classes.exposeprovider.webdav.ts b/ts/classes.exposeprovider.webdav.ts index 59915a4..1df247f 100644 --- a/ts/classes.exposeprovider.webdav.ts +++ b/ts/classes.exposeprovider.webdav.ts @@ -39,39 +39,50 @@ export class WebDavExposeProvider extends ExposeProvider { this.webdavClient = new plugins.smartwebdav.WebdavClient(this.options.webdavCredentials); await this.ensureBaseDir(); await this.houseKeeping(); - this.smartExposeRef.taskmanager.addAndScheduleTask(new plugins.taskbuffer.Task({ - name: 'webdavHousekeeping', - taskFunction: async () => { - await this.houseKeeping(); - }, - }), '0 * * * * *') + this.smartExposeRef.taskmanager.addAndScheduleTask( + new plugins.taskbuffer.Task({ + name: 'webdavHousekeeping', + taskFunction: async () => { + await this.houseKeeping(); + }, + }), + '0 * * * * *' + ); } public async stop(): Promise { // Nothing to do here } - public async exposeFile(optionsArg: { - smartFile: plugins.smartfile.SmartFile; - deleteAfterMillis?: number; - privateUrl?: boolean; - }): Promise<{ url: string; id: string; status: 'created' | 'updated' }> { + public async exposeFile( + optionsArg: Parameters[0] + ): Promise<{ url: string; id: string; status: 'created' | 'updated' }> { await this.ensureBaseDir(); const fileId = plugins.smartunique.shortId(30); - console.log(`Expsing file under id: ${fileId}. (${ - plugins.smartformat.prettyBytes(optionsArg.smartFile.contents.length) - })`); + console.log( + `Expsing file under id: ${fileId}. (${plugins.smartformat.prettyBytes( + optionsArg.smartFile.contents.length + )})` + ); const webdavFilePath = await this.getFilePathById(fileId); - const fileToUpload = await plugins.smartfile.SmartFile.fromBuffer(webdavFilePath, optionsArg.smartFile.contents); + const fileToUpload = await plugins.smartfile.SmartFile.fromBuffer( + webdavFilePath, + optionsArg.smartFile.contents + ); await this.webdavClient.uploadSmartFileArray([fileToUpload]); console.log(`checking file presence: ${webdavFilePath}`); const existsOnWebdav = await this.webdavClient.wdClient.exists(webdavFilePath); - const publicUrl = plugins.smartpath.join(this.smartExposeRef.options.exposedBaseUrl, webdavFilePath); - console.log(`cehcking for file at ${publicUrl}`) + const publicUrl = plugins.smartpath.join( + this.smartExposeRef.options.exposedBaseUrl, + webdavFilePath + ); + console.log(`cehcking for file at ${publicUrl}`); const response = await plugins.smartrequest.getBinary(publicUrl); plugins.smartexpect.expect(response.body).toEqual(fileToUpload.contents); if (optionsArg.deleteAfterMillis) { - console.log(`Scheduling deletion of file with id: ${fileId} in ${optionsArg.deleteAfterMillis}ms...`); + console.log( + `Scheduling deletion of file with id: ${fileId} in ${optionsArg.deleteAfterMillis}ms...` + ); plugins.smartdelay.delayFor(optionsArg.deleteAfterMillis).then(() => { console.log(`Deleting file with id: ${fileId}...`); this.deleteFileById(fileId); @@ -85,6 +96,20 @@ export class WebDavExposeProvider extends ExposeProvider { }; } + public async exposeFileArray(optionsArg: Parameters[0]) { + const returnArray = []; + for (const smartFile of optionsArg.smartFiles) { + returnArray.push( + await this.exposeFile({ + smartFile, + deleteAfterMillis: optionsArg.deleteAfterMillis, + privateUrl: optionsArg.privateUrl, + }) + ); + } + return returnArray; + } + 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); diff --git a/ts/classes.smartexpose.ts b/ts/classes.smartexpose.ts index b17a91d..b9bab1b 100644 --- a/ts/classes.smartexpose.ts +++ b/ts/classes.smartexpose.ts @@ -42,4 +42,8 @@ export class SmartExpose { public async exposeFile(optionsArg: Parameters[0]) { return this.provider.exposeFile(optionsArg); } + + public async exposeFileArray(optionsArg: Parameters[0]) { + return this.provider.exposeFileArray(optionsArg); + } }