Skip to content

Commit

Permalink
feat: ✨ add local storage wrapper/impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Exortions committed Dec 9, 2023
1 parent b94b7d6 commit e408bc5
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/storage/storage-local-provider.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Injectable } from '@nestjs/common';
import { existsSync, mkdir, readFile, writeFile } from 'fs';

const localPath = 'data/';

/**
* Storage provider for local file system
* Uses the file system on the host machine
* to store and retrieve files
*
* @extends StorageProvider
*/
@Injectable()
export class StorageLocalProviderService {
private isInitialized = false;

/**
* Initializes the local storage provider
* Creates the data directory if it does not exist
*/
private initialize(): void {
if (!this.isInitialized && !existsSync(localPath)) {
mkdir(localPath, (err) => {
if (err)
console.error('Failed to initialize local storage provider: ', err);
else this.isInitialized = true;
});
}
}

/**
* Fetches a file from the local storage provider
*
* @param path the path to the file to fetch
* @returns if the file was fetched successfully, and the contents of the file
*/
public async get(path: string): Promise<{ success: boolean; data: string }> {
this.initialize();

return new Promise((resolve, reject) => {
readFile(localPath + path, (err, data) => {
if (err) {
console.error(
'Failed to get file from local storage provider: ',
err,
);
reject({ success: false, data: null });
} else {
resolve({ success: true, data: data.toString() });
}
});
});
}

/**
* Sets a file in the local storage provider
*
* @param path the path to the file to set
* @param value the value to set the file to
* @returns true if the file was set successfully
*/
public async set(path: string, value: any): Promise<boolean> {
this.initialize();

if (!existsSync(localPath + path)) {
return new Promise((resolve, reject) => {
reject('File does not exist');
});
}

return new Promise((resolve, reject) => {
writeFile(localPath + path, value, (err) => {
if (err) {
console.error('Failed to set file in local storage provider: ', err);
return reject(false);
} else resolve(true);
});
});
}
}
11 changes: 11 additions & 0 deletions src/storage/storage.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { StorageProvider } from './storage.service';
import { StorageLocalProviderService } from './storage-local-provider.service';

@Module({
controllers: [],
providers: [
{ provide: StorageProvider, useClass: StorageLocalProviderService },
],
})
export class StorageModule {}
22 changes: 22 additions & 0 deletions src/storage/storage.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Abstract class for fetching files from a
* storage provider.
*/
export abstract class StorageProvider {
/**
* Fetches a file from the storage provider
* @param path the path to the file to fetch
*
* @returns contents of the file
*/
abstract get(path: string): { success: boolean; data: any };

/**
* Sets a file in the storage provider
* @param path the path to the file to set
* @param value the value to set the file to
*
* @returns true if the file was set successfully
*/
abstract set(path: string, value: any): boolean;
}

0 comments on commit e408bc5

Please sign in to comment.