-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ add local storage wrapper/impl
- Loading branch information
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |