Skip to content

Commit 516842e

Browse files
authored
Merge pull request #6 from SecJS/feat/len-subscribe-configs
feat: subscribe disks configurations in runtime
2 parents 82fdd9f + ec1c230 commit 516842e

File tree

9 files changed

+116
-34
lines changed

9 files changed

+116
-34
lines changed

README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ export default {
7676
disks: {
7777
local: {
7878
driver: 'local',
79-
root: Path.storage('app'),
79+
root: Path.noBuild().storage('app'),
8080
url: `${Env('APP_URL', '')}/storage`,
8181
},
8282
public: {
8383
driver: 'local',
84-
root: Path.storage('app/public'),
84+
root: Path.noBuild().storage('app/public'),
8585
url: `${Env('APP_URL', '')}/storage/public`,
8686
},
8787
s3: {
@@ -146,6 +146,28 @@ await storage.copy('folder/DASdsakdjas912831jhdasnm.txt', 'folder/test/copy.txt'
146146
await storage.move('folder/DASdsakdjas912831jhdasnm.txt', 'folder/test/move.txt')
147147
```
148148

149+
### Subscribing configs of disks in runtime
150+
151+
> You can subscribe the disks configs in runtime using addConfig, removeConfig and resetConfig methods
152+
153+
```ts
154+
// File created on storage/newAppFolder/file.txt
155+
storage
156+
.addConfig('root', Path.noBuild().storage('newAppFolder'))
157+
.put('file.txt', Buffer.from('Hello World'))
158+
159+
// Will use the default: storage/app/file.txt
160+
storage
161+
.removeConfig('root')
162+
.put('file.txt', Buffer.from('Hello World'))
163+
164+
// resetConfig removes all the configs from the Storage instance
165+
// Will use the default: storage/app/file.txt
166+
storage
167+
.resetConfigs()
168+
.put('file.txt', Buffer.from('Hello World'))
169+
```
170+
149171
### Using S3 disk
150172

151173
> You can use s3 disk to make all this actions inside your s3 bucket

config/filesystem.default.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ export default {
2929
disks: {
3030
local: {
3131
driver: 'local',
32-
root: Path.storage('app'),
32+
root: Path.noBuild().storage('app'),
3333
url: `${Env('APP_URL', '')}/storage`,
3434
},
3535
public: {
3636
driver: 'local',
37-
root: Path.storage('app/public'),
37+
root: Path.noBuild().storage('app/public'),
3838
url: `${Env('APP_URL', '')}/storage/public`,
3939
},
4040
s3: {
@@ -43,7 +43,7 @@ export default {
4343
secret: Env('AWS_SECRET', ''),
4444
region: Env('AWS_REGION', ''),
4545
bucket: Env('AWS_BUCKET', ''),
46-
endpoint: Env('AWS_ENDPOINT', '')
47-
}
46+
endpoint: Env('AWS_ENDPOINT', ''),
47+
},
4848
},
4949
}

config/filesystem.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export default {
4848
secret: Env('AWS_SECRET', ''),
4949
region: Env('AWS_REGION', ''),
5050
bucket: Env('AWS_BUCKET', ''),
51-
endpoint: Env('AWS_ENDPOINT', '')
52-
}
51+
endpoint: Env('AWS_ENDPOINT', ''),
52+
},
5353
},
5454
}

package-lock.json

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@secjs/storage",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Handle your application files in Node.js",
55
"license": "MIT",
66
"author": "João Lenon <lenon@secjs.com.br>",
@@ -23,10 +23,10 @@
2323
"@secjs/config": "1.0.7",
2424
"@secjs/env": "1.2.5",
2525
"@secjs/exceptions": "1.0.4",
26-
"@secjs/logger": "^1.2.2",
27-
"@secjs/utils": "1.4.3",
26+
"@secjs/logger": "1.2.2",
27+
"@secjs/utils": "1.5.8",
2828
"@types/jest": "27.0.1",
29-
"@types/mime-types": "^2.1.1",
29+
"@types/mime-types": "2.1.1",
3030
"@types/node": "14.17.0",
3131
"@typescript-eslint/eslint-plugin": "4.31.0",
3232
"@typescript-eslint/parser": "4.31.0",

src/Drivers/LocalDriver.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ export class LocalDriver implements DriverContract {
1717
private readonly _url: string
1818
private readonly _root: string
1919

20-
constructor(disk: string) {
21-
this._url = Config.get(`filesystem.disks.${disk}.url`)
22-
this._root = Config.get(`filesystem.disks.${disk}.root`)
20+
constructor(disk: string, configs: any = {}) {
21+
this._url = configs.url || Config.get(`filesystem.disks.${disk}.url`)
22+
this._root = configs.root || Config.get(`filesystem.disks.${disk}.root`)
2323
}
2424

2525
private concat(filePath: string) {

src/Drivers/S3Driver.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ export class S3Driver implements DriverContract {
2323
private readonly _bucket: string
2424
private readonly _endpoint: string
2525

26-
constructor(disk: string) {
26+
constructor(disk: string, configs: any = {}) {
2727
const s3Config = Config.get(`filesystem.disks.${disk}`)
2828

29-
this._key = s3Config.key
30-
this._region = s3Config.region
31-
this._secret = s3Config.secret
32-
this._bucket = s3Config.bucket
33-
this._endpoint = s3Config.endpoint
29+
this._key = configs.key || s3Config.key
30+
this._region = configs.region || s3Config.region
31+
this._secret = configs.secret || s3Config.secret
32+
this._bucket = configs.bucket || s3Config.bucket
33+
this._endpoint = configs.endpoint || s3Config.endpoint
3434

3535
this.s3Client = new S3({
3636
region: this._region,

src/Storage.ts

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ import { Drivers } from './Drivers/Drivers'
2121
import { DriverContract } from './Contracts/DriverContract'
2222

2323
export class Storage {
24+
private configs: any = {}
2425
private _tempDriver: DriverContract | null = null
2526
private _defaultDriver: DriverContract | null = null
2627

27-
static build(name: string, driver: new (disk: string) => DriverContract) {
28+
static build(
29+
name: string,
30+
driver: new (disk: string, configs?: any) => DriverContract,
31+
) {
2832
if (Drivers[name])
2933
throw new InternalServerException(`Driver ${name} already exists`)
3034

@@ -42,6 +46,48 @@ export class Storage {
4246
this._defaultDriver = new Drivers[diskConfig.driver](defaultDisk)
4347
}
4448

49+
resetConfigs(): Storage {
50+
this.configs = {}
51+
52+
const defaultDisk = Config.get('filesystem.default')
53+
const diskConfig = Config.get(`filesystem.disks.${defaultDisk}`)
54+
55+
this._defaultDriver = new Drivers[diskConfig.driver](
56+
defaultDisk,
57+
this.configs,
58+
)
59+
60+
return this
61+
}
62+
63+
removeConfig(key: string): Storage {
64+
delete this.configs[key]
65+
66+
const defaultDisk = Config.get('filesystem.default')
67+
const diskConfig = Config.get(`filesystem.disks.${defaultDisk}`)
68+
69+
this._defaultDriver = new Drivers[diskConfig.driver](
70+
defaultDisk,
71+
this.configs,
72+
)
73+
74+
return this
75+
}
76+
77+
addConfig(key: string, value: any): Storage {
78+
this.configs[key] = value
79+
80+
const defaultDisk = Config.get('filesystem.default')
81+
const diskConfig = Config.get(`filesystem.disks.${defaultDisk}`)
82+
83+
this._defaultDriver = new Drivers[diskConfig.driver](
84+
defaultDisk,
85+
this.configs,
86+
)
87+
88+
return this
89+
}
90+
4591
changeDefaultDisk(disk: string): Storage {
4692
const diskConfig = Config.get(`filesystem.disks.${disk}`)
4793

@@ -55,7 +101,7 @@ export class Storage {
55101
`Driver ${diskConfig.driver} does not exist, use Storage.build method to create a new driver`,
56102
)
57103

58-
this._defaultDriver = new Drivers[diskConfig.driver](disk)
104+
this._defaultDriver = new Drivers[diskConfig.driver](disk, this.configs)
59105

60106
return this
61107
}
@@ -73,7 +119,7 @@ export class Storage {
73119
`Driver ${diskConfig.driver} does not exist, use Storage.build method to create a new driver`,
74120
)
75121

76-
this._tempDriver = new Drivers[diskConfig.driver](disk)
122+
this._tempDriver = new Drivers[diskConfig.driver](disk, this.configs)
77123

78124
return this
79125
}

tests/storage-local.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,20 @@ describe('\n Storage Local Class', () => {
111111
expect(existsSync(copyPath)).toBe(true)
112112
})
113113

114+
it('should add, remove and reset new configs to drivers', async () => {
115+
storage.addConfig('root', Path.storage('newApp/local'))
116+
117+
await storage.put('local.txt', bigContent)
118+
119+
expect(existsSync(Path.storage('newApp/local/local.txt'))).toBe(true)
120+
121+
await storage.removeConfig('root').put('local.txt', bigContent)
122+
123+
expect(existsSync(Path.storage('app/local/local.txt'))).toBe(true)
124+
125+
await promises.rmdir(Path.storage('newApp'), { recursive: true })
126+
})
127+
114128
afterEach(async () => {
115129
await promises.rmdir(Path.storage('app/local'), { recursive: true })
116130
})

0 commit comments

Comments
 (0)