diff --git a/README.md b/README.md index 8631854..fad8cfa 100644 --- a/README.md +++ b/README.md @@ -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: { @@ -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 diff --git a/config/filesystem.default.ts b/config/filesystem.default.ts index b1fb4b6..095f337 100644 --- a/config/filesystem.default.ts +++ b/config/filesystem.default.ts @@ -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: { @@ -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', ''), + }, }, } diff --git a/config/filesystem.ts b/config/filesystem.ts index 40b2298..1a3209f 100644 --- a/config/filesystem.ts +++ b/config/filesystem.ts @@ -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', ''), + }, }, } diff --git a/package-lock.json b/package-lock.json index 8870977..400a190 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,17 +9,17 @@ "version": "1.0.0", "license": "MIT", "dependencies": { - "aws-sdk": "^2.1037.0", + "aws-sdk": "2.1037.0", "mime-types": "2.1.34" }, "devDependencies": { "@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", @@ -1878,9 +1878,9 @@ } }, "node_modules/@secjs/utils": { - "version": "1.4.3", - "resolved": "https://registry.npmjs.org/@secjs/utils/-/utils-1.4.3.tgz", - "integrity": "sha512-rZMLhn1RSrnSboOSPD76cgPgeBGotfXOlTQxKQq1Di2UWFXQeOU5aKmbiHO98JQHWWiVuveeArKrcYpDeDvScQ==", + "version": "1.5.8", + "resolved": "https://registry.npmjs.org/@secjs/utils/-/utils-1.5.8.tgz", + "integrity": "sha512-PZ9xHS8M1390XP4HKXw0mVOcc/UzYX5By28XppnBJglMbWAZ9zwhB9Ftf+w33nqRoxDmjaOYFa9cMIVF8TWnbA==", "dev": true, "dependencies": { "mime-types": "2.1.34" @@ -12621,9 +12621,9 @@ } }, "@secjs/utils": { - "version": "1.4.3", - "resolved": "https://registry.npmjs.org/@secjs/utils/-/utils-1.4.3.tgz", - "integrity": "sha512-rZMLhn1RSrnSboOSPD76cgPgeBGotfXOlTQxKQq1Di2UWFXQeOU5aKmbiHO98JQHWWiVuveeArKrcYpDeDvScQ==", + "version": "1.5.8", + "resolved": "https://registry.npmjs.org/@secjs/utils/-/utils-1.5.8.tgz", + "integrity": "sha512-PZ9xHS8M1390XP4HKXw0mVOcc/UzYX5By28XppnBJglMbWAZ9zwhB9Ftf+w33nqRoxDmjaOYFa9cMIVF8TWnbA==", "dev": true, "requires": { "mime-types": "2.1.34" diff --git a/package.json b/package.json index 4d2a22e..06c5833 100644 --- a/package.json +++ b/package.json @@ -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 ", @@ -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", diff --git a/src/Drivers/LocalDriver.ts b/src/Drivers/LocalDriver.ts index d7666d4..fd4eb22 100644 --- a/src/Drivers/LocalDriver.ts +++ b/src/Drivers/LocalDriver.ts @@ -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) { diff --git a/src/Drivers/S3Driver.ts b/src/Drivers/S3Driver.ts index b4ce279..5eb3a52 100644 --- a/src/Drivers/S3Driver.ts +++ b/src/Drivers/S3Driver.ts @@ -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, diff --git a/src/Storage.ts b/src/Storage.ts index 3f660e7..dd789a0 100644 --- a/src/Storage.ts +++ b/src/Storage.ts @@ -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`) @@ -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}`) @@ -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 } @@ -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 } diff --git a/tests/storage-local.spec.ts b/tests/storage-local.spec.ts index cd9a7ee..b0b805a 100644 --- a/tests/storage-local.spec.ts +++ b/tests/storage-local.spec.ts @@ -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 }) })