Skip to content

Commit 0952b40

Browse files
committed
Added NFS support
1 parent e34a996 commit 0952b40

6 files changed

Lines changed: 70 additions & 14 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export AWS_SECRET_ACCESS_KEY=982u289432u48jsdfkjsr3894
4444
export AWS_SESSION_TOKEN (optional)
4545
```
4646

47-
Local NFS Declaration **NOTE NFS still under development i.e it won't function appropriately
47+
Local NFS Declaration
4848
```bash
4949
export MOUNT_POINT=/Users/nitrocode/bucket/
5050
```

lib/GoogleCloudStorageSystem.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class GoogleCloudStorageSystem extends StorageSystem {
4848
}
4949
}
5050

51-
async deleteFile(buckName, filename, destination){
51+
async deleteFile(buckName, filename){
5252
try{
5353
await storage
5454
.bucket(buckName)

lib/NFSStorageSystem.js

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const StorageSystem = require ('./storage-system');
22
const fse = require('fs-extra');
3+
const fs = require('fs');
34

45
class NFSStorageSystem extends StorageSystem {
56
/**
@@ -10,14 +11,13 @@ class NFSStorageSystem extends StorageSystem {
1011
*/
1112
constructor() {
1213
super();
13-
this.nfsMount = '';
14-
this.nfsBucket = '';
14+
this._nfsMount = process.env.NFS_MOUNT_POINT;
1515
}
1616

17-
async upload(source, destination) {
17+
async upload(bucketName, filename, destination) {
1818
try {
19-
const finalDest = `${this.nfsMount}${this.nfsBucket}/${destination}`;
20-
return fse.copy(source, finalDest);
19+
const finalDest = `${this._nfsMount}${bucketName}/${filename}`;
20+
return fse.copy(destination, finalDest);
2121
} catch (e) {
2222
throw new Error(e);
2323
}
@@ -29,13 +29,70 @@ class NFSStorageSystem extends StorageSystem {
2929
* @params filename, destination
3030
* @description Serves as General Download SDK for NFS Storage
3131
*/
32-
async download(filename, destination) {
32+
async download(bucketName, filename, destination) {
3333
try {
34-
return fse.copy(`${this.nfsMount}${this.nfsBucket}/${filename}`, destination);
34+
return fse.copy(`${this._nfsMount}${bucketName}/${filename}`, destination);
3535
} catch (e) {
3636
throw new Error(e);
3737
}
3838
}
39+
40+
async createBucket(bucketName){
41+
try {
42+
if (!fs.existsSync(this._nfsMount+bucketName)){
43+
return fs.mkdirSync(this._nfsMount+bucketName);
44+
}else {
45+
return new Error('Bucket Already Exist')
46+
}
47+
}catch (err) {
48+
throw new Error(err)
49+
}
50+
}
51+
52+
async deleteBucket(bucketName){
53+
try {
54+
if (fs.existsSync(this._nfsMount+bucketName) === true){
55+
return fs.rmdirSync(this._nfsMount+bucketName, { recursive: true });
56+
}else {
57+
return new Error('Bucket Does Not Exist')
58+
}
59+
}catch (err) {
60+
throw new Error(err)
61+
}
62+
}
63+
64+
async listFiles(bucketName){
65+
let files = [];
66+
try {
67+
fs.readdirSync(this._nfsMount+bucketName).forEach(file => {
68+
files.push(file);
69+
});
70+
return files
71+
}catch (err) {
72+
throw new Error(err)
73+
}
74+
}
75+
76+
async listBuckets(){
77+
let files = [];
78+
try {
79+
fs.readdirSync(this._nfsMount).forEach(file => {
80+
files.push(file);
81+
});
82+
return files
83+
}catch (err) {
84+
throw new Error(err)
85+
}
86+
}
87+
88+
async deleteFile(buckName, filename){
89+
try{
90+
return fs.unlinkSync(this._nfsMount+buckName+filename)
91+
}catch (err) {
92+
throw new Error(err)
93+
}
94+
}
95+
3996
}
4097

4198
module.exports = NFSStorageSystem;

lib/storage-system.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ class StorageSystem {
22
upload() {
33
throw new Error('concrete implementation should be used');
44
}
5-
65
download() {
76
throw new Error('concrete implementation should be used');
87
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node_storage_manager",
3-
"version": "1.1.6",
3+
"version": "1.1.7",
44
"description": "A storage manager for nodejs",
55
"main": "index.js",
66
"scripts": {

test/sdk.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ describe('Simple Storage Test', () => {
66

77
//First stage Unit Test
88
it('should run', () => {
9-
let StorageInstance = storageSystem.getInstance('AWS', '');
10-
StorageInstance.download('smc-v1-dev', 'dev_key.key', '/Users/nitrocode/tmp/');
11-
console.log(StorageInstance);
9+
let StorageInstance = storageSystem.getInstance('GCLOUD');
10+
const data = StorageInstance.download('media', 'dev_key.key', '/Users/nitrocode/tmp/');
11+
console.log(data);
1212
});
1313

1414
});

0 commit comments

Comments
 (0)