fix(core): update

This commit is contained in:
2024-04-24 18:02:21 +02:00
parent a301d67bda
commit b538f72004
10 changed files with 219 additions and 44 deletions

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartexpose',
version: '1.0.2',
version: '1.0.3',
description: 'a package to expose things to the internet'
}

View File

@ -36,7 +36,7 @@ export abstract class ExposeProvider {
id: string;
status: 'deleted' | 'failed' | 'notfound';
}[]>
public abstract wipeFileById(idArg: string): Promise<{
public abstract deleteFileById(idArg: string): Promise<{
id: string;
status: 'deleted' | 'failed' | 'notfound';
}>

View File

@ -1,12 +1,14 @@
import { ExposeProvider } from './classes.exposeprovider.js';
import type { SmartExpose } from './classes.smartexpose.js';
import * as plugins from './plugins.js';
export interface IWebdavExposeProviderOptions {
webdavCredentials: plugins.smartwebdav.IWebdavClientOptions,
webdavSubPath: string,
webdavCredentials: plugins.smartwebdav.IWebdavClientOptions;
webdavSubPath: string;
}
export class WebDavExposeProvider extends ExposeProvider {
public smartExposeRef: SmartExpose;
public webdavClient: plugins.smartwebdav.WebdavClient;
public options: IWebdavExposeProviderOptions;
@ -15,40 +17,99 @@ export class WebDavExposeProvider extends ExposeProvider {
this.options = optionsArg;
}
public async houseKeeping (): Promise<void> {
public async houseKeeping(): Promise<void> {
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')) {
}
}
}
public async ensureBaseDir (): Promise<void> {
public async getFilePathById(idArg: string): Promise<string> {
return plugins.smartpath.join(this.options.webdavSubPath, idArg);
}
public async ensureBaseDir(): Promise<void> {
await this.webdavClient.ensureDirectory(this.options.webdavSubPath);
}
public async start (): Promise<void> {
public async start(): Promise<void> {
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 * * * * *')
}
public async stop (): Promise<void> {
public async stop(): Promise<void> {
// 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: {
smartFile: plugins.smartfile.SmartFile;
deleteAfterMillis?: number;
privateUrl?: boolean;
}): 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)
})`);
const webdavFilePath = await this.getFilePathById(fileId);
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 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...`);
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',
};
}
public async wipeAll (): Promise<{ id: string; status: 'deleted' | 'failed' | 'notfound'; }[]> {
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',
};
});
}
public async wipeFileById (idArg: string): Promise<{ id: string; status: 'deleted' | 'failed' | 'notfound'; }> {
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',
};
}
}
}

View File

@ -5,6 +5,7 @@ import * as plugins from './plugins.js';
export interface ISmartExposeOptions {
deleteAfterMillis?: number,
privateUrl?: boolean,
exposedBaseUrl: string,
}
export class SmartExpose {
@ -12,7 +13,6 @@ export class SmartExpose {
public static createWithWebdav(optionsArg: {
webdavCredentials: plugins.smartwebdav.IWebdavClientOptions,
webdavSubPath: string,
exposedBaseUrl: string,
exposeOptions: ISmartExposeOptions,
}) {
const provider = new WebDavExposeProvider({
@ -20,14 +20,32 @@ export class SmartExpose {
webdavSubPath: optionsArg.webdavSubPath,
});
const smartexposeInstance = new SmartExpose(provider, optionsArg.exposeOptions);
provider.smartExposeRef = smartexposeInstance;
return smartexposeInstance;
}
// INSTANCE
public taskmanager: plugins.taskbuffer.TaskManager;
public provider: ExposeProvider;
public options: ISmartExposeOptions;
constructor(provider: ExposeProvider, optionsArg: ISmartExposeOptions) {
this.provider = provider;
this.options = optionsArg;
}
public async start() {
this.taskmanager = new plugins.taskbuffer.TaskManager();
await this.provider.start();
this.taskmanager.start();
}
public async stop() {
await this.provider.stop();
this.taskmanager.stop();
}
public async exposeFile(optionsArg: Parameters<ExposeProvider['exposeFile']>[0]) {
return this.provider.exposeFile(optionsArg);
}
}

View File

@ -1,12 +1,24 @@
// @push.rocks scope
import * as smartdelay from '@push.rocks/smartdelay';
import * as smartexpect from '@push.rocks/smartexpect';
import * as smartfile from '@push.rocks/smartfile';
import * as smartformat from '@push.rocks/smartformat';
import * as smartjson from '@push.rocks/smartjson';
import * as smartpath from '@push.rocks/smartpath';
import * as smartrequest from '@push.rocks/smartrequest';
import * as smartunique from '@push.rocks/smartunique';
import * as smartwebdav from '@push.rocks/smartwebdav';
import * as taskbuffer from '@push.rocks/taskbuffer';
export {
smartdelay,
smartexpect,
smartfile,
smartformat,
smartjson,
smartpath,
smartrequest,
smartunique,
smartwebdav,
taskbuffer,
}