11const StorageSystem = require ( './storage-system' ) ;
22const fse = require ( 'fs-extra' ) ;
3+ const fs = require ( 'fs' ) ;
34
45class 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
4198module . exports = NFSStorageSystem ;
0 commit comments