@file-storage/core
Advanced tools
Comparing version 1.1.2 to 1.1.3
/// <reference types="node" /> | ||
import { Stream } from 'stream'; | ||
import { DiskConfig, Driver } from '@file-storage/common'; | ||
import { DiskConfig, Driver, PutResult } from '@file-storage/common'; | ||
import { BuitInDiskConfig, StorageConfiguration } from './types'; | ||
/** | ||
* `Storage` provides a filesystem abstraction, simple way to uses drivers for working with local filesystems, Amazon S3,... | ||
*/ | ||
declare class StorageClass implements Driver { | ||
@@ -12,3 +9,3 @@ /** | ||
*/ | ||
defaultDisk: Driver; | ||
private defaultDisk; | ||
/** | ||
@@ -18,2 +15,3 @@ * All plugin instances. | ||
private pluginInstances; | ||
private uniqueFileName; | ||
constructor(diskName?: string); | ||
@@ -35,3 +33,3 @@ get name(): string; | ||
*/ | ||
disk<U extends Driver>(diskName: string): U; | ||
disk<U extends Driver>(diskName?: string): U; | ||
disk(diskName: string, asStorage: true): StorageClass; | ||
@@ -42,3 +40,3 @@ url(path: string): string; | ||
lastModified(path: string): Promise<number>; | ||
put(data: Stream | Buffer, path: string): Promise<any>; | ||
put(data: Stream | Buffer, path: string): Promise<PutResult>; | ||
get(path: string): Stream | Promise<Stream>; | ||
@@ -51,3 +49,6 @@ delete(path: string): Promise<any>; | ||
} | ||
/** | ||
* `Storage` provides a filesystem abstraction, simple way to uses drivers for working with local filesystems, Amazon S3,... | ||
*/ | ||
export declare const Storage: StorageClass; | ||
export {}; |
@@ -14,2 +14,4 @@ "use strict"; | ||
const common_1 = require("@file-storage/common"); | ||
const uuid_1 = require("uuid"); | ||
const path_1 = require("path"); | ||
let configableDefaultDiskName = 'local'; | ||
@@ -79,7 +81,5 @@ const defaultDiskConfig = { | ||
} | ||
/** | ||
* `Storage` provides a filesystem abstraction, simple way to uses drivers for working with local filesystems, Amazon S3,... | ||
*/ | ||
class StorageClass { | ||
constructor(diskName = configableDefaultDiskName) { | ||
this.uniqueFileName = false; | ||
this.initialize(diskName); | ||
@@ -105,8 +105,12 @@ } | ||
config(options = {}) { | ||
const { diskConfigs = [], customDrivers = [] } = options; | ||
const { diskConfigs = [], customDrivers = [], uniqueFileName = false } = options; | ||
addCustomDriver(customDrivers); | ||
handleDiskConfigs(diskConfigs); | ||
this.uniqueFileName = uniqueFileName; | ||
this.initialize(configableDefaultDiskName); | ||
} | ||
disk(diskName, asStorage = false) { | ||
if (!diskName) { | ||
return this.defaultDisk; | ||
} | ||
return asStorage ? new StorageClass(diskName) : getDisk(diskName); | ||
@@ -128,8 +132,16 @@ } | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let result = {}; | ||
const putData = yield this.defaultDisk.put(data, path); | ||
let result = { | ||
success: true, | ||
message: 'Uploading success', | ||
name: common_1.getFileName(path), | ||
path, | ||
}; | ||
if (this.uniqueFileName) { | ||
result.path = path_1.dirname(path) + '/' + uuid_1.v4() + '.' + common_1.getExt(path); | ||
} | ||
const putData = yield this.defaultDisk.put(data, result.path); | ||
result = Object.assign({}, result, putData); | ||
for (const plugin of this.pluginInstances) { | ||
if (plugin.afterPutKey && plugin.afterPut) { | ||
const afterPutData = yield plugin.afterPut(path); | ||
const afterPutData = yield plugin.afterPut(result.path); | ||
result[plugin.afterPutKey] = afterPutData; | ||
@@ -160,2 +172,5 @@ } | ||
} | ||
/** | ||
* `Storage` provides a filesystem abstraction, simple way to uses drivers for working with local filesystems, Amazon S3,... | ||
*/ | ||
exports.Storage = new StorageClass(); |
@@ -1,3 +0,3 @@ | ||
import { Class, DiskConfig, Driver, LocalDiskConfig, S3DiskConfig, SftpDiskConfig } from '@file-storage/common'; | ||
export declare type BuitInDiskConfig = LocalDiskConfig | S3DiskConfig | SftpDiskConfig; | ||
import { Class, DiskConfig, Driver, FtpDiskConfig, LocalDiskConfig, S3DiskConfig, SftpDiskConfig } from '@file-storage/common'; | ||
export declare type BuitInDiskConfig = LocalDiskConfig | S3DiskConfig | SftpDiskConfig | FtpDiskConfig; | ||
export interface StorageConfiguration<T extends DiskConfig> { | ||
@@ -12,2 +12,6 @@ /** | ||
customDrivers?: Class<Driver>[]; | ||
/** | ||
* Enable unique file name. | ||
*/ | ||
uniqueFileName?: boolean; | ||
} |
{ | ||
"name": "@file-storage/core", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "> TODO: description", | ||
@@ -26,13 +26,15 @@ "author": "Dang Nguyen <haidang009@gmail.com>", | ||
"test": "jest", | ||
"test:coverage": "jest --coverage", | ||
"test:detectOpenHandles": "jest --detectOpenHandles" | ||
}, | ||
"dependencies": { | ||
"@file-storage/local": "^1.1.1" | ||
"@file-storage/local": "^1.1.2", | ||
"uuid": "^8.3.2" | ||
}, | ||
"devDependencies": { | ||
"@file-storage/common": "^1.1.1", | ||
"@file-storage/common": "^1.1.2", | ||
"rimraf": "~3.0.2", | ||
"typescript": "~4.3.5" | ||
}, | ||
"gitHead": "ee67f33b76b6536599251d52c783a55f77205f6a" | ||
"gitHead": "ecd35677ca09cd5df9e414915d3c4fabc5ec6b5a" | ||
} |
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
16520
262
2
+ Addeduuid@^8.3.2
+ Addeduuid@8.3.2(transitive)
Updated@file-storage/local@^1.1.2