-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
389 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Artur Bier | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "@bunt/fs", | ||
"version": "0.1.0", | ||
"keywords": [ | ||
"typescript" | ||
], | ||
"author": { | ||
"name": "Artur Bier", | ||
"email": "[email protected]" | ||
}, | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist/" | ||
], | ||
"description": "FileSystem library", | ||
"repository": "[email protected]:izatop/bunt.git", | ||
"scripts": { | ||
"clean": "yarn build:clean", | ||
"watch": "yarn build:watch", | ||
"build": "yarn build:clean && tsc", | ||
"build:clean": "rimraf dist tsconfig.tsbuildinfo", | ||
"build:watch": "yarn build --watch" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"@bunt/unit": "^0.14.8", | ||
"@bunt/util": "^0.14.8", | ||
"@types/minio": "^7.0.7", | ||
"minio": "^7.0.18" | ||
}, | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import {FileStorage} from "../FileStorage"; | ||
|
||
export interface IBucketOptions { | ||
region?: string; | ||
} | ||
|
||
export class FsBucket { | ||
public readonly name: string; | ||
public readonly region?: string; | ||
protected ready = false; | ||
readonly #fs: FileStorage; | ||
|
||
constructor(fs: FileStorage, name: string, options: IBucketOptions = {}) { | ||
this.#fs = fs; | ||
this.name = name; | ||
this.region = options.region; | ||
} | ||
|
||
public setPolicy(policy: string): Promise<void> { | ||
return this.#fs.getDriver().setBucketPolicy(this.name, policy); | ||
} | ||
|
||
public getPolicy(): Promise<string> { | ||
return this.#fs.getDriver().getBucketPolicy(this.name); | ||
} | ||
|
||
public async write(id: string, file: string, metadata: Record<any, any>): Promise<string> { | ||
if (!this.ready) { | ||
await this.save(); | ||
} | ||
|
||
const driver = this.#fs.getDriver(); | ||
return driver.write(this.name, id, file, metadata); | ||
} | ||
|
||
public async save(): Promise<void> { | ||
const driver = this.#fs.getDriver(); | ||
await driver.createBucket(this.name, this.region, true); | ||
this.ready = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {FileStorage} from "../FileStorage"; | ||
import {FsBucket, IBucketOptions} from "./FsBucket"; | ||
|
||
export class FsBucketList { | ||
readonly #fs: FileStorage; | ||
readonly #buckets = new Map<string, FsBucket>(); | ||
|
||
constructor(fs: FileStorage) { | ||
this.#fs = fs; | ||
} | ||
|
||
public ensure(name: string, options: IBucketOptions = {}): FsBucket { | ||
const bucket = this.#buckets.get(name) || this.factory(name, options); | ||
if (!this.#buckets.has(name)) { | ||
this.#buckets.set(name, bucket); | ||
} | ||
|
||
return bucket; | ||
} | ||
|
||
protected factory(name: string, options: IBucketOptions = {}): FsBucket { | ||
return new FsBucket(this.#fs, name, options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export abstract class FsDriverAbstract { | ||
public abstract createBucket(name: string, region?: string, checkExists?: boolean): Promise<void>; | ||
|
||
public abstract write(bucket: string, name: string, file: string, metadata: Record<any, any>): Promise<string>; | ||
|
||
public abstract setBucketPolicy(bucket: string, policy: string): Promise<void>; | ||
|
||
public abstract getBucketPolicy(bucket: string): Promise<string>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import {assert} from "@bunt/util"; | ||
import {Client} from "minio"; | ||
import {parse} from "url"; | ||
import {FsDriverAbstract} from "./FsDriverAbstract"; | ||
import {MinIOBucketPolicy} from "./MinIOBucketPolicy"; | ||
|
||
const DEFAULT_REGION = "default"; | ||
|
||
export class MinIO extends FsDriverAbstract { | ||
readonly #client: Client; | ||
readonly #policy = new MinIOBucketPolicy(); | ||
|
||
constructor(dsn: string) { | ||
super(); | ||
const {auth, hostname: endPoint, port, protocol} = parse(dsn); | ||
assert(auth, "Authorization is required"); | ||
assert(endPoint, "Endpoint is required"); | ||
assert(port, "Port is required"); | ||
|
||
const [accessKey, secretKey] = auth?.split(":"); | ||
this.#client = new Client({ | ||
useSSL: protocol?.startsWith("https"), | ||
port: parseInt(port), | ||
endPoint, | ||
accessKey, | ||
secretKey, | ||
}); | ||
} | ||
|
||
public async setBucketPolicy(bucket: string, policy: string): Promise<void> { | ||
await this.#client.setBucketPolicy(bucket, this.#policy.getPolicy(bucket, policy)); | ||
} | ||
|
||
public getBucketPolicy(bucket: string): Promise<string> { | ||
return this.#client.getBucketPolicy(bucket); | ||
} | ||
|
||
public async createBucket(name: string, region?: string, checkExists = true): Promise<void> { | ||
if (checkExists && await this.#client.bucketExists(name)) { | ||
return; | ||
} | ||
|
||
await this.#client.makeBucket(name, region ?? DEFAULT_REGION); | ||
} | ||
|
||
public write(bucket: string, name: string, file: string, metadata: Record<any, any>): Promise<string> { | ||
return this.#client.fPutObject(bucket, name, file, metadata); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {assert} from "@bunt/util"; | ||
|
||
export enum MinIOBucketPolicyEnum { | ||
READONLY = "public-readonly", | ||
} | ||
|
||
export class MinIOBucketPolicy { | ||
readonly #policies = new Map<MinIOBucketPolicyEnum, (bucket: string) => string>(); | ||
|
||
public constructor() { | ||
this.#policies.set(MinIOBucketPolicyEnum.READONLY, this.publicReadOnlyPolicy); | ||
} | ||
|
||
public getPolicy(bucket: string, maybe: string): string { | ||
const policy = this.#policies.get(maybe as MinIOBucketPolicyEnum); | ||
assert(policy, `Unknown bucket policy ${maybe}`); | ||
return policy(bucket); | ||
} | ||
|
||
public publicReadOnlyPolicy(bucket: string): string { | ||
return JSON.stringify({ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Sid": "PublicRead", | ||
"Effect": "Allow", | ||
"Principal": "*", | ||
"Action": ["s3:GetObject"], | ||
"Resource": [`arn:aws:s3:::${bucket}/*`], | ||
}, | ||
], | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {FsBucket, IBucketOptions} from "./Bucket/FsBucket"; | ||
import {FsBucketList} from "./Bucket/FsBucketList"; | ||
import {FsDriverAbstract} from "./Driver/FsDriverAbstract"; | ||
|
||
export class FileStorage { | ||
readonly #driver: FsDriverAbstract; | ||
readonly #buckets: FsBucketList; | ||
|
||
constructor(driver: FsDriverAbstract) { | ||
this.#driver = driver; | ||
this.#buckets = new FsBucketList(this); | ||
} | ||
|
||
public getBucket(name: string, options: IBucketOptions = {}): FsBucket { | ||
return this.#buckets.ensure(name, options); | ||
} | ||
|
||
public getDriver(): FsDriverAbstract { | ||
return this.#driver; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {Service} from "@bunt/unit"; | ||
import {FsDriverAbstract} from "./Driver/FsDriverAbstract"; | ||
import {FileStorage} from "./FileStorage"; | ||
|
||
export class FileStorageService extends Service<FileStorage> { | ||
readonly #driver: FsDriverAbstract; | ||
|
||
constructor(driver: FsDriverAbstract) { | ||
super(); | ||
this.#driver = driver; | ||
} | ||
|
||
public async resolve(): Promise<FileStorage> { | ||
return new FileStorage(this.#driver); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export * from "./FileStorage"; | ||
export * from "./FileStorageService"; | ||
export * from "./Bucket/FsBucket"; | ||
export * from "./Bucket/FsBucketList"; | ||
export * from "./Driver/FsDriverAbstract"; | ||
export * from "./Driver/MinIOBucketPolicy"; | ||
export * from "./Driver/MinIO"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./Storage"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"extends": "../../tsconfig.lib.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "dist" | ||
}, | ||
"references": [ | ||
{ | ||
"path": "../unit" | ||
}, | ||
{ | ||
"path": "../util" | ||
} | ||
], | ||
"include": [ | ||
"src" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.