@file-storage/core
Advanced tools
Comparing version 1.3.4 to 1.3.5
@@ -15,3 +15,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Storage = void 0; | ||
exports.Storage = exports.StorageClass = void 0; | ||
const common_1 = require("@file-storage/common"); | ||
@@ -26,3 +26,2 @@ const local_1 = __importDefault(require("@file-storage/local")); | ||
}; | ||
let availableDisks = [defaultDiskConfig]; | ||
const drivers = [ | ||
@@ -36,6 +35,5 @@ local_1.default, | ||
].filter((item) => !!item); | ||
const plugins = []; | ||
function handleDiskConfigs(diskConfigs) { | ||
const seen = new Set(); | ||
availableDisks = []; | ||
const availableDisks = []; | ||
const hasDuplicatesName = () => diskConfigs.some((diskConfig) => seen.size === seen.add(diskConfig.name).size); | ||
@@ -45,46 +43,19 @@ if (hasDuplicatesName()) { | ||
} | ||
if (diskConfigs.length === 0) { | ||
availableDisks.push(defaultDiskConfig); | ||
if (diskConfigs.length > 0) { | ||
availableDisks.push(...diskConfigs); | ||
} | ||
else { | ||
availableDisks.push(...diskConfigs); | ||
availableDisks.push(defaultDiskConfig); | ||
} | ||
return availableDisks; | ||
} | ||
function handlePluginConfigs(pluginCls = []) { | ||
plugins.push(...pluginCls); | ||
} | ||
function driverNotLoaded(driver) { | ||
return !drivers.find((item) => item.name === driver.name); | ||
} | ||
function addDriversFromAvailableDisks() { | ||
availableDisks.forEach((disk) => { | ||
if (typeof disk.driver !== 'string' && driverNotLoaded(disk.driver)) { | ||
drivers.push(disk.driver); | ||
} | ||
}); | ||
} | ||
function getDisk(diskName) { | ||
const diskConfig = availableDisks.find((item) => item.name === diskName); | ||
if (!diskConfig) { | ||
throw new Error(`Given disk is not defined: ${diskName}`); | ||
} | ||
const driver = typeof diskConfig.driver !== 'string' | ||
? diskConfig.driver | ||
: drivers.find((item) => item['driverName'] === diskConfig.driver); | ||
if (!driver) { | ||
// Throw error missing built-in driver package. | ||
if (Object.values(common_1.DriverName).includes(diskConfig.driver)) { | ||
throw new Error(`Please install \`@file-storage/${diskConfig.driver}\` for ${diskConfig.driver} driver`); | ||
} | ||
const name = typeof diskConfig.driver !== 'string' ? diskConfig.driver.name : diskConfig.driver; | ||
throw new Error(`Driver '${name}' is not declared.`); | ||
} | ||
return new driver(diskConfig); | ||
} | ||
class StorageClass { | ||
constructor(config = true) { | ||
constructor() { | ||
this.availableDisks = [defaultDiskConfig]; | ||
this.plugins = []; | ||
this.uniqueFileName = false; | ||
if (config) { | ||
this.config(); | ||
} | ||
this.config(); | ||
} | ||
@@ -94,2 +65,7 @@ get name() { | ||
} | ||
getDriversFromAvailableDisks() { | ||
return this.availableDisks | ||
.filter((disk) => typeof disk.driver !== 'string' && driverNotLoaded(disk.driver)) | ||
.map((disk) => disk.driver); | ||
} | ||
/** | ||
@@ -99,16 +75,22 @@ * Config for storage methods supported in the application. | ||
config(options = {}) { | ||
const { diskConfigs = [], uniqueFileName = false } = options; | ||
const { diskConfigs, uniqueFileName } = options; | ||
let { defaultDiskName } = options; | ||
handleDiskConfigs(diskConfigs); | ||
handlePluginConfigs(options.plugins); | ||
addDriversFromAvailableDisks(); | ||
if (typeof diskConfigs !== 'undefined') { | ||
this.availableDisks = handleDiskConfigs(diskConfigs); | ||
} | ||
drivers.push(...this.getDriversFromAvailableDisks()); | ||
if (options.plugins && options.plugins.length > 0) { | ||
this.plugins = options.plugins; | ||
} | ||
if (!defaultDiskName) { | ||
if (availableDisks.length > 1) { | ||
if (this.availableDisks.length > 1) { | ||
throw new Error('Please specify a default disk name.'); | ||
} | ||
defaultDiskName = availableDisks[0].name; | ||
defaultDiskName = this.availableDisks[0].name; | ||
} | ||
this.uniqueFileName = uniqueFileName; | ||
this.defaultDisk = getDisk(defaultDiskName); | ||
this.pluginInstances = plugins.map((pluginClass) => { | ||
if (typeof uniqueFileName !== 'undefined') { | ||
this.uniqueFileName = uniqueFileName; | ||
} | ||
this.defaultDisk = this.getDisk(defaultDiskName); | ||
this.pluginInstances = this.plugins.map((pluginClass) => { | ||
const plugin = new pluginClass(); | ||
@@ -119,16 +101,33 @@ plugin.init(this.defaultDisk); | ||
} | ||
disk(diskName, asStorage = false) { | ||
if (!diskName) { | ||
return this.defaultDisk; | ||
getDisk(diskName) { | ||
const diskConfig = this.availableDisks.find((item) => item.name === diskName); | ||
if (!diskConfig) { | ||
throw new Error(`Given disk is not defined: ${diskName}`); | ||
} | ||
if (asStorage) { | ||
const storage = new StorageClass(false); | ||
storage.config({ | ||
diskConfigs: availableDisks, | ||
defaultDiskName: diskName, | ||
}); | ||
return storage; | ||
const driver = typeof diskConfig.driver !== 'string' | ||
? diskConfig.driver | ||
: drivers.find((item) => item['driverName'] === diskConfig.driver); | ||
if (!driver) { | ||
// Throw error missing built-in driver package. | ||
if (Object.values(common_1.DriverName).includes(diskConfig.driver)) { | ||
throw new Error(`Please install \`@file-storage/${diskConfig.driver}\` for ${diskConfig.driver} driver`); | ||
} | ||
const name = typeof diskConfig.driver !== 'string' ? diskConfig.driver.name : diskConfig.driver; | ||
throw new Error(`Driver '${name}' is not declared.`); | ||
} | ||
return getDisk(diskName); | ||
return new driver(diskConfig); | ||
} | ||
/** | ||
* Get current disk instance. | ||
*/ | ||
instance() { | ||
return this.defaultDisk; | ||
} | ||
disk(diskName, options) { | ||
options = typeof diskName === 'string' ? options : diskName; | ||
const storage = Object.assign(new StorageClass(), this); | ||
const defaultDiskName = typeof diskName === 'string' ? diskName : options.defaultDiskName || this.name; | ||
storage.config(Object.assign(Object.assign({}, options), { defaultDiskName })); | ||
return storage; | ||
} | ||
url(path) { | ||
@@ -161,3 +160,3 @@ return this.defaultDisk.url(path); | ||
result = Object.assign({}, result, putData); | ||
for (const plugin of this.pluginInstances) { | ||
for (const plugin of this.pluginInstances || []) { | ||
if (plugin.afterPutKey && plugin.afterPut) { | ||
@@ -196,2 +195,3 @@ const afterPutData = yield plugin.afterPut(result.path); | ||
} | ||
exports.StorageClass = StorageClass; | ||
/** | ||
@@ -198,0 +198,0 @@ * `Storage` provides a filesystem abstraction, simple way to uses drivers for working with local filesystems, Amazon S3,... |
@@ -10,3 +10,2 @@ import { DriverName, requireDefaultModule, getFileName, } from '@file-storage/common'; | ||
}; | ||
let availableDisks = [defaultDiskConfig]; | ||
const drivers = [ | ||
@@ -20,6 +19,5 @@ LocalDriver, | ||
].filter((item) => !!item); | ||
const plugins = []; | ||
function handleDiskConfigs(diskConfigs) { | ||
const seen = new Set(); | ||
availableDisks = []; | ||
const availableDisks = []; | ||
const hasDuplicatesName = () => diskConfigs.some((diskConfig) => seen.size === seen.add(diskConfig.name).size); | ||
@@ -29,46 +27,19 @@ if (hasDuplicatesName()) { | ||
} | ||
if (diskConfigs.length === 0) { | ||
availableDisks.push(defaultDiskConfig); | ||
if (diskConfigs.length > 0) { | ||
availableDisks.push(...diskConfigs); | ||
} | ||
else { | ||
availableDisks.push(...diskConfigs); | ||
availableDisks.push(defaultDiskConfig); | ||
} | ||
return availableDisks; | ||
} | ||
function handlePluginConfigs(pluginCls = []) { | ||
plugins.push(...pluginCls); | ||
} | ||
function driverNotLoaded(driver) { | ||
return !drivers.find((item) => item.name === driver.name); | ||
} | ||
function addDriversFromAvailableDisks() { | ||
availableDisks.forEach((disk) => { | ||
if (typeof disk.driver !== 'string' && driverNotLoaded(disk.driver)) { | ||
drivers.push(disk.driver); | ||
} | ||
}); | ||
} | ||
function getDisk(diskName) { | ||
const diskConfig = availableDisks.find((item) => item.name === diskName); | ||
if (!diskConfig) { | ||
throw new Error(`Given disk is not defined: ${diskName}`); | ||
} | ||
const driver = typeof diskConfig.driver !== 'string' | ||
? diskConfig.driver | ||
: drivers.find((item) => item['driverName'] === diskConfig.driver); | ||
if (!driver) { | ||
// Throw error missing built-in driver package. | ||
if (Object.values(DriverName).includes(diskConfig.driver)) { | ||
throw new Error(`Please install \`@file-storage/${diskConfig.driver}\` for ${diskConfig.driver} driver`); | ||
} | ||
const name = typeof diskConfig.driver !== 'string' ? diskConfig.driver.name : diskConfig.driver; | ||
throw new Error(`Driver '${name}' is not declared.`); | ||
} | ||
return new driver(diskConfig); | ||
} | ||
class StorageClass { | ||
constructor(config = true) { | ||
export class StorageClass { | ||
constructor() { | ||
this.availableDisks = [defaultDiskConfig]; | ||
this.plugins = []; | ||
this.uniqueFileName = false; | ||
if (config) { | ||
this.config(); | ||
} | ||
this.config(); | ||
} | ||
@@ -78,2 +49,7 @@ get name() { | ||
} | ||
getDriversFromAvailableDisks() { | ||
return this.availableDisks | ||
.filter((disk) => typeof disk.driver !== 'string' && driverNotLoaded(disk.driver)) | ||
.map((disk) => disk.driver); | ||
} | ||
/** | ||
@@ -83,16 +59,22 @@ * Config for storage methods supported in the application. | ||
config(options = {}) { | ||
const { diskConfigs = [], uniqueFileName = false } = options; | ||
const { diskConfigs, uniqueFileName } = options; | ||
let { defaultDiskName } = options; | ||
handleDiskConfigs(diskConfigs); | ||
handlePluginConfigs(options.plugins); | ||
addDriversFromAvailableDisks(); | ||
if (typeof diskConfigs !== 'undefined') { | ||
this.availableDisks = handleDiskConfigs(diskConfigs); | ||
} | ||
drivers.push(...this.getDriversFromAvailableDisks()); | ||
if (options.plugins && options.plugins.length > 0) { | ||
this.plugins = options.plugins; | ||
} | ||
if (!defaultDiskName) { | ||
if (availableDisks.length > 1) { | ||
if (this.availableDisks.length > 1) { | ||
throw new Error('Please specify a default disk name.'); | ||
} | ||
defaultDiskName = availableDisks[0].name; | ||
defaultDiskName = this.availableDisks[0].name; | ||
} | ||
this.uniqueFileName = uniqueFileName; | ||
this.defaultDisk = getDisk(defaultDiskName); | ||
this.pluginInstances = plugins.map((pluginClass) => { | ||
if (typeof uniqueFileName !== 'undefined') { | ||
this.uniqueFileName = uniqueFileName; | ||
} | ||
this.defaultDisk = this.getDisk(defaultDiskName); | ||
this.pluginInstances = this.plugins.map((pluginClass) => { | ||
const plugin = new pluginClass(); | ||
@@ -103,16 +85,36 @@ plugin.init(this.defaultDisk); | ||
} | ||
disk(diskName, asStorage = false) { | ||
if (!diskName) { | ||
return this.defaultDisk; | ||
getDisk(diskName) { | ||
const diskConfig = this.availableDisks.find((item) => item.name === diskName); | ||
if (!diskConfig) { | ||
throw new Error(`Given disk is not defined: ${diskName}`); | ||
} | ||
if (asStorage) { | ||
const storage = new StorageClass(false); | ||
storage.config({ | ||
diskConfigs: availableDisks, | ||
defaultDiskName: diskName, | ||
}); | ||
return storage; | ||
const driver = typeof diskConfig.driver !== 'string' | ||
? diskConfig.driver | ||
: drivers.find((item) => item['driverName'] === diskConfig.driver); | ||
if (!driver) { | ||
// Throw error missing built-in driver package. | ||
if (Object.values(DriverName).includes(diskConfig.driver)) { | ||
throw new Error(`Please install \`@file-storage/${diskConfig.driver}\` for ${diskConfig.driver} driver`); | ||
} | ||
const name = typeof diskConfig.driver !== 'string' ? diskConfig.driver.name : diskConfig.driver; | ||
throw new Error(`Driver '${name}' is not declared.`); | ||
} | ||
return getDisk(diskName); | ||
return new driver(diskConfig); | ||
} | ||
/** | ||
* Get current disk instance. | ||
*/ | ||
instance() { | ||
return this.defaultDisk; | ||
} | ||
disk(diskName, options) { | ||
options = typeof diskName === 'string' ? options : diskName; | ||
const storage = Object.assign(new StorageClass(), this); | ||
const defaultDiskName = typeof diskName === 'string' ? diskName : options.defaultDiskName || this.name; | ||
storage.config({ | ||
...options, | ||
defaultDiskName, | ||
}); | ||
return storage; | ||
} | ||
url(path) { | ||
@@ -144,3 +146,3 @@ return this.defaultDisk.url(path); | ||
result = Object.assign({}, result, putData); | ||
for (const plugin of this.pluginInstances) { | ||
for (const plugin of this.pluginInstances || []) { | ||
if (plugin.afterPutKey && plugin.afterPut) { | ||
@@ -147,0 +149,0 @@ const afterPutData = await plugin.afterPut(result.path); |
@@ -5,3 +5,5 @@ /// <reference types="node" /> | ||
import { StorageConfiguration } from './types'; | ||
declare class StorageClass { | ||
export declare class StorageClass { | ||
private availableDisks; | ||
private plugins; | ||
/** | ||
@@ -16,4 +18,5 @@ * Get default disk instance. | ||
private uniqueFileName; | ||
constructor(config?: boolean); | ||
constructor(); | ||
get name(): string; | ||
private getDriversFromAvailableDisks; | ||
/** | ||
@@ -23,4 +26,9 @@ * Config for storage methods supported in the application. | ||
config<U extends DiskConfig>(options?: StorageConfiguration<U>): void; | ||
private getDisk; | ||
/** | ||
* Get disk instance by diskName. | ||
* Get current disk instance. | ||
*/ | ||
instance<U extends Driver>(): U; | ||
/** | ||
* Get StorageClass instance by diskName. | ||
* | ||
@@ -30,4 +38,5 @@ * @param diskName Disk name. | ||
*/ | ||
disk<U extends Driver>(diskName?: string): U; | ||
disk(diskName: string, asStorage: true): StorageClass; | ||
disk(diskName: string): StorageClass; | ||
disk<U extends DiskConfig>(options: StorageConfiguration<U>): StorageClass; | ||
disk<U extends DiskConfig>(diskName: string, options: StorageConfiguration<U>): StorageClass; | ||
url(path: string): string; | ||
@@ -54,2 +63,1 @@ exists(path: string): Promise<boolean>; | ||
export declare const Storage: StorageClass; | ||
export {}; |
{ | ||
"name": "@file-storage/core", | ||
"version": "1.3.4", | ||
"version": "1.3.5", | ||
"description": "> TODO: description", | ||
@@ -34,11 +34,11 @@ "author": "Dang Nguyen <haidang009@gmail.com>", | ||
"dependencies": { | ||
"@file-storage/local": "^1.3.4", | ||
"@file-storage/local": "^1.3.5", | ||
"uuid": "^8.3.2" | ||
}, | ||
"devDependencies": { | ||
"@file-storage/common": "^1.3.4", | ||
"@file-storage/common": "^1.3.5", | ||
"rimraf": "~3.0.2", | ||
"typescript": "~4.3.5" | ||
}, | ||
"gitHead": "0fff4d5d6961f2809e64cbad3733660700fe1353" | ||
"gitHead": "748206a0f1eac99680a3faa8d077eb5eefe6c677" | ||
} |
@@ -79,10 +79,2 @@ ``` | ||
## Obtain disk instance: | ||
To interact with a specific disk instead of the default, use `disk` method to get that instance: | ||
```javascript | ||
Storage.disk('local').get('/path/to/local/my-image.png'); | ||
``` | ||
## Unique file name | ||
@@ -102,2 +94,13 @@ | ||
## Obtain specific disk: | ||
To interact with a specific disk instead of the default, use `disk` method: | ||
```typescript | ||
Storage.disk('local').get('/path/to/local/my-image.png'); | ||
// To adjust the configuration on the fly, you can specify the settings in the second argument: | ||
Storage.disk('local', { uniqueFileName: false }).put(...); | ||
``` | ||
## Create your custom driver | ||
@@ -149,3 +152,3 @@ | ||
Add provide it to Storage config: | ||
And provide it to Storage config: | ||
@@ -161,8 +164,2 @@ ```typescript | ||
**NOTE**: `Image manipulation` only available on Storage facade, If you obtain a specific disk instance, set the second parameter to `true` to obtain a storage instance insteads: | ||
```typescript | ||
Storage.disk('your-disk', true); // Storage instance. | ||
``` | ||
#### Image manipulation customize | ||
@@ -199,2 +196,3 @@ | ||
- [ ] Replace `request` module with another module as it was deprecated. | ||
- [ ] Remove deprecated: BuiltInDiskConfig, DriverName. | ||
@@ -201,0 +199,0 @@ ## License |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25604
483
197
Updated@file-storage/local@^1.3.5