Skip to content

Commit

Permalink
Merge pull request #6 from SecJS/feat/len-subscribe-configs
Browse files Browse the repository at this point in the history
feat: subscribe disks configurations in runtime
  • Loading branch information
jlenon7 authored Jan 5, 2022
2 parents 82fdd9f + ec1c230 commit 516842e
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 34 deletions.
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ export default {
disks: {
local: {
driver: 'local',
root: Path.storage('app'),
root: Path.noBuild().storage('app'),
url: `${Env('APP_URL', '')}/storage`,
},
public: {
driver: 'local',
root: Path.storage('app/public'),
root: Path.noBuild().storage('app/public'),
url: `${Env('APP_URL', '')}/storage/public`,
},
s3: {
Expand Down Expand Up @@ -146,6 +146,28 @@ await storage.copy('folder/DASdsakdjas912831jhdasnm.txt', 'folder/test/copy.txt'
await storage.move('folder/DASdsakdjas912831jhdasnm.txt', 'folder/test/move.txt')
```

### Subscribing configs of disks in runtime

> You can subscribe the disks configs in runtime using addConfig, removeConfig and resetConfig methods
```ts
// File created on storage/newAppFolder/file.txt
storage
.addConfig('root', Path.noBuild().storage('newAppFolder'))
.put('file.txt', Buffer.from('Hello World'))

// Will use the default: storage/app/file.txt
storage
.removeConfig('root')
.put('file.txt', Buffer.from('Hello World'))

// resetConfig removes all the configs from the Storage instance
// Will use the default: storage/app/file.txt
storage
.resetConfigs()
.put('file.txt', Buffer.from('Hello World'))
```

### Using S3 disk

> You can use s3 disk to make all this actions inside your s3 bucket
Expand Down
8 changes: 4 additions & 4 deletions config/filesystem.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ export default {
disks: {
local: {
driver: 'local',
root: Path.storage('app'),
root: Path.noBuild().storage('app'),
url: `${Env('APP_URL', '')}/storage`,
},
public: {
driver: 'local',
root: Path.storage('app/public'),
root: Path.noBuild().storage('app/public'),
url: `${Env('APP_URL', '')}/storage/public`,
},
s3: {
Expand All @@ -43,7 +43,7 @@ export default {
secret: Env('AWS_SECRET', ''),
region: Env('AWS_REGION', ''),
bucket: Env('AWS_BUCKET', ''),
endpoint: Env('AWS_ENDPOINT', '')
}
endpoint: Env('AWS_ENDPOINT', ''),
},
},
}
4 changes: 2 additions & 2 deletions config/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default {
secret: Env('AWS_SECRET', ''),
region: Env('AWS_REGION', ''),
bucket: Env('AWS_BUCKET', ''),
endpoint: Env('AWS_ENDPOINT', '')
}
endpoint: Env('AWS_ENDPOINT', ''),
},
},
}
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@secjs/storage",
"version": "1.0.0",
"version": "1.0.1",
"description": "Handle your application files in Node.js",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand All @@ -23,10 +23,10 @@
"@secjs/config": "1.0.7",
"@secjs/env": "1.2.5",
"@secjs/exceptions": "1.0.4",
"@secjs/logger": "^1.2.2",
"@secjs/utils": "1.4.3",
"@secjs/logger": "1.2.2",
"@secjs/utils": "1.5.8",
"@types/jest": "27.0.1",
"@types/mime-types": "^2.1.1",
"@types/mime-types": "2.1.1",
"@types/node": "14.17.0",
"@typescript-eslint/eslint-plugin": "4.31.0",
"@typescript-eslint/parser": "4.31.0",
Expand Down
6 changes: 3 additions & 3 deletions src/Drivers/LocalDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export class LocalDriver implements DriverContract {
private readonly _url: string
private readonly _root: string

constructor(disk: string) {
this._url = Config.get(`filesystem.disks.${disk}.url`)
this._root = Config.get(`filesystem.disks.${disk}.root`)
constructor(disk: string, configs: any = {}) {
this._url = configs.url || Config.get(`filesystem.disks.${disk}.url`)
this._root = configs.root || Config.get(`filesystem.disks.${disk}.root`)
}

private concat(filePath: string) {
Expand Down
12 changes: 6 additions & 6 deletions src/Drivers/S3Driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ export class S3Driver implements DriverContract {
private readonly _bucket: string
private readonly _endpoint: string

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

this._key = s3Config.key
this._region = s3Config.region
this._secret = s3Config.secret
this._bucket = s3Config.bucket
this._endpoint = s3Config.endpoint
this._key = configs.key || s3Config.key
this._region = configs.region || s3Config.region
this._secret = configs.secret || s3Config.secret
this._bucket = configs.bucket || s3Config.bucket
this._endpoint = configs.endpoint || s3Config.endpoint

this.s3Client = new S3({
region: this._region,
Expand Down
52 changes: 49 additions & 3 deletions src/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ import { Drivers } from './Drivers/Drivers'
import { DriverContract } from './Contracts/DriverContract'

export class Storage {
private configs: any = {}
private _tempDriver: DriverContract | null = null
private _defaultDriver: DriverContract | null = null

static build(name: string, driver: new (disk: string) => DriverContract) {
static build(
name: string,
driver: new (disk: string, configs?: any) => DriverContract,
) {
if (Drivers[name])
throw new InternalServerException(`Driver ${name} already exists`)

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

resetConfigs(): Storage {
this.configs = {}

const defaultDisk = Config.get('filesystem.default')
const diskConfig = Config.get(`filesystem.disks.${defaultDisk}`)

this._defaultDriver = new Drivers[diskConfig.driver](
defaultDisk,
this.configs,
)

return this
}

removeConfig(key: string): Storage {
delete this.configs[key]

const defaultDisk = Config.get('filesystem.default')
const diskConfig = Config.get(`filesystem.disks.${defaultDisk}`)

this._defaultDriver = new Drivers[diskConfig.driver](
defaultDisk,
this.configs,
)

return this
}

addConfig(key: string, value: any): Storage {
this.configs[key] = value

const defaultDisk = Config.get('filesystem.default')
const diskConfig = Config.get(`filesystem.disks.${defaultDisk}`)

this._defaultDriver = new Drivers[diskConfig.driver](
defaultDisk,
this.configs,
)

return this
}

changeDefaultDisk(disk: string): Storage {
const diskConfig = Config.get(`filesystem.disks.${disk}`)

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

this._defaultDriver = new Drivers[diskConfig.driver](disk)
this._defaultDriver = new Drivers[diskConfig.driver](disk, this.configs)

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

this._tempDriver = new Drivers[diskConfig.driver](disk)
this._tempDriver = new Drivers[diskConfig.driver](disk, this.configs)

return this
}
Expand Down
14 changes: 14 additions & 0 deletions tests/storage-local.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,20 @@ describe('\n Storage Local Class', () => {
expect(existsSync(copyPath)).toBe(true)
})

it('should add, remove and reset new configs to drivers', async () => {
storage.addConfig('root', Path.storage('newApp/local'))

await storage.put('local.txt', bigContent)

expect(existsSync(Path.storage('newApp/local/local.txt'))).toBe(true)

await storage.removeConfig('root').put('local.txt', bigContent)

expect(existsSync(Path.storage('app/local/local.txt'))).toBe(true)

await promises.rmdir(Path.storage('newApp'), { recursive: true })
})

afterEach(async () => {
await promises.rmdir(Path.storage('app/local'), { recursive: true })
})
Expand Down

0 comments on commit 516842e

Please sign in to comment.