@file-storage/core
Advanced tools
Comparing version 1.1.4 to 1.2.0
@@ -15,9 +15,5 @@ /// <reference types="node" /> | ||
private uniqueFileName; | ||
constructor(diskName?: string); | ||
constructor(config?: boolean); | ||
get name(): string; | ||
/** | ||
* Initialize a storage. | ||
*/ | ||
private initialize; | ||
/** | ||
* Config for storage methods supported in the application. | ||
@@ -24,0 +20,0 @@ */ |
@@ -16,3 +16,2 @@ "use strict"; | ||
const path_1 = require("path"); | ||
let configableDefaultDiskName = 'local'; | ||
const defaultDiskConfig = { | ||
@@ -22,3 +21,2 @@ driver: common_1.DriverName.LOCAL, | ||
root: 'storage', | ||
isDefault: true, | ||
}; | ||
@@ -36,8 +34,3 @@ let availableDisks = [defaultDiskConfig]; | ||
availableDisks = []; | ||
const isMoreThanOneDefault = () => diskConfigs.filter((diskConfig) => !!diskConfig.isDefault).length > 1; | ||
const hasDuplicatesName = () => diskConfigs.some((diskConfig) => seen.size === seen.add(diskConfig.name).size); | ||
const isNoDefaultAssigned = () => typeof availableDisks.find((diskConfig) => !!diskConfig.isDefault) === 'undefined'; | ||
if (isMoreThanOneDefault()) { | ||
throw new Error('Not allows more than one default disk.'); | ||
} | ||
if (hasDuplicatesName()) { | ||
@@ -49,13 +42,5 @@ throw new Error('Duplicated disk name.'); | ||
} | ||
availableDisks.push(...diskConfigs); | ||
if (isNoDefaultAssigned()) { | ||
availableDisks[0].isDefault = true; | ||
else { | ||
availableDisks.push(...diskConfigs); | ||
} | ||
// Set default disk. | ||
for (const diskConfig of availableDisks) { | ||
if (diskConfig.isDefault) { | ||
configableDefaultDiskName = diskConfig.name; | ||
break; | ||
} | ||
} | ||
} | ||
@@ -67,3 +52,3 @@ function addCustomDriver(customDrivers = []) { | ||
} | ||
function getDisk(diskName = configableDefaultDiskName) { | ||
function getDisk(diskName) { | ||
const diskConfig = availableDisks.find((item) => item.name === diskName); | ||
@@ -86,5 +71,7 @@ if (!diskConfig) { | ||
class StorageClass { | ||
constructor(diskName = configableDefaultDiskName) { | ||
constructor(config = true) { | ||
this.uniqueFileName = false; | ||
this.initialize(diskName); | ||
if (config) { | ||
this.config(); | ||
} | ||
} | ||
@@ -95,6 +82,17 @@ get name() { | ||
/** | ||
* Initialize a storage. | ||
* Config for storage methods supported in the application. | ||
*/ | ||
initialize(diskName = configableDefaultDiskName) { | ||
this.defaultDisk = getDisk(diskName); | ||
config(options = {}) { | ||
const { diskConfigs = [], customDrivers = [], uniqueFileName = false } = options; | ||
let { defaultDiskName } = options; | ||
handleDiskConfigs(diskConfigs); | ||
addCustomDriver(customDrivers); | ||
if (!defaultDiskName) { | ||
if (availableDisks.length > 1) { | ||
throw new Error('Please specify a default disk name.'); | ||
} | ||
defaultDiskName = availableDisks[0].name; | ||
} | ||
this.uniqueFileName = uniqueFileName; | ||
this.defaultDisk = getDisk(defaultDiskName); | ||
this.pluginInstances = plugins.map((pluginClass) => { | ||
@@ -106,12 +104,2 @@ const plugin = new pluginClass(); | ||
} | ||
/** | ||
* Config for storage methods supported in the application. | ||
*/ | ||
config(options = {}) { | ||
const { diskConfigs = [], customDrivers = [], uniqueFileName = false } = options; | ||
addCustomDriver(customDrivers); | ||
handleDiskConfigs(diskConfigs); | ||
this.uniqueFileName = uniqueFileName; | ||
this.initialize(configableDefaultDiskName); | ||
} | ||
disk(diskName, asStorage = false) { | ||
@@ -121,3 +109,11 @@ if (!diskName) { | ||
} | ||
return asStorage ? new StorageClass(diskName) : getDisk(diskName); | ||
if (asStorage) { | ||
const storage = new StorageClass(false); | ||
storage.config({ | ||
diskConfigs: availableDisks, | ||
defaultDiskName: diskName, | ||
}); | ||
return storage; | ||
} | ||
return getDisk(diskName); | ||
} | ||
@@ -145,3 +141,5 @@ url(path) { | ||
if (this.uniqueFileName) { | ||
result.path = path_1.dirname(path) + '/' + uuid_1.v4() + '.' + common_1.getExt(path); | ||
const parsedPath = path_1.parse(path); | ||
parsedPath.base = uuid_1.v4() + parsedPath.ext; | ||
result.path = path_1.format(parsedPath); | ||
} | ||
@@ -148,0 +146,0 @@ const putData = yield this.defaultDisk.put(data, result.path); |
@@ -5,2 +5,6 @@ import { Class, DiskConfig, Driver, FtpDiskConfig, LocalDiskConfig, S3DiskConfig, SftpDiskConfig } from '@file-storage/common'; | ||
/** | ||
* Default disk name. | ||
*/ | ||
defaultDiskName?: string; | ||
/** | ||
* List of disks available in your application. | ||
@@ -7,0 +11,0 @@ */ |
{ | ||
"name": "@file-storage/core", | ||
"version": "1.1.4", | ||
"version": "1.2.0", | ||
"description": "> TODO: description", | ||
@@ -30,11 +30,11 @@ "author": "Dang Nguyen <haidang009@gmail.com>", | ||
"dependencies": { | ||
"@file-storage/local": "^1.1.2", | ||
"@file-storage/local": "^1.2.0", | ||
"uuid": "^8.3.2" | ||
}, | ||
"devDependencies": { | ||
"@file-storage/common": "^1.1.2", | ||
"@file-storage/common": "^1.2.0", | ||
"rimraf": "~3.0.2", | ||
"typescript": "~4.3.5" | ||
}, | ||
"gitHead": "291ff2b4c911fc966aad489b394d231927c2f4a7" | ||
"gitHead": "e31fa799b710b85e0c3029b08dfba2a5e8b3a8db" | ||
} |
@@ -50,2 +50,4 @@ ``` | ||
Storage.config({ | ||
// Default disk that you can access directly via Storage facade. | ||
defaultDiskName: 'mys3', | ||
diskConfigs: [ | ||
@@ -61,3 +63,2 @@ { | ||
bucketName: 'mybucket', | ||
isDefault: true, // Default disk that you can access directly via Storage facade. | ||
// Uncomment if you want specify credentials manually. | ||
@@ -134,3 +135,2 @@ // region: 'ap-southeast-1', | ||
name: 'myCustomDisk', | ||
isDefault: true, | ||
... | ||
@@ -137,0 +137,0 @@ } |
16302
260
Updated@file-storage/local@^1.2.0