Skip to content

Commit 9bb23d1

Browse files
committed
fix: update storage
1 parent 2033a32 commit 9bb23d1

5 files changed

Lines changed: 78 additions & 9 deletions

File tree

src/config/storage.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Storage from '~/lib/storage'
2+
import { GoogleCloudStorageParams } from '~/lib/storage/types'
3+
import { env } from './env'
4+
5+
export const storage = Storage.create({
6+
storageType: 'gcs',
7+
params: {
8+
access_key: env.STORAGE_ACCESS_KEY,
9+
bucket: env.STORAGE_BUCKET_NAME,
10+
expires: env.STORAGE_SIGN_EXPIRED,
11+
filepath: env.STORAGE_FILEPATH,
12+
} as GoogleCloudStorageParams,
13+
})

src/lib/storage/gcs.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,16 @@ export default class GoogleCloudStorage {
4444
})
4545
}
4646

47+
/**
48+
* Generate keyfile
49+
*/
4750
private _generateKeyfile(values: string[]) {
4851
return values.join('/')
4952
}
5053

54+
/**
55+
* Get expires object
56+
*/
5157
public expiresObject() {
5258
const getExpired = this._expires.replace(/[^0-9]/g, '')
5359

@@ -57,6 +63,9 @@ export default class GoogleCloudStorage {
5763
return { expiresIn, expiryDate }
5864
}
5965

66+
/**
67+
* Initialize storage
68+
*/
6069
async initialize() {
6170
const msgType = `${green('storage - google cloud storage')}`
6271
const bucketName = this._bucket
@@ -79,6 +88,9 @@ export default class GoogleCloudStorage {
7988
}
8089
}
8190

91+
/**
92+
* Create bucket
93+
*/
8294
private async _createBucket() {
8395
const msgType = `${green('storage - google cloud storage')}`
8496
const bucketName = this._bucket
@@ -97,6 +109,9 @@ export default class GoogleCloudStorage {
97109
}
98110
}
99111

112+
/**
113+
* Upload file
114+
*/
100115
async uploadFile({ directory, file }: UploadFileParams) {
101116
const keyfile = this._generateKeyfile([directory, file.filename])
102117

@@ -112,12 +127,15 @@ export default class GoogleCloudStorage {
112127
}
113128

114129
const data = await this.client.bucket(this._bucket).upload(file.path, options)
115-
const signedUrl = await this.presignedURL(keyfile)
130+
const signedUrl = await this.presignedUrl(keyfile)
116131

117132
return { data: data[1], signedUrl }
118133
}
119134

120-
async presignedURL(keyfile: string) {
135+
/**
136+
* Generate presigned URL
137+
*/
138+
async presignedUrl(keyfile: string) {
121139
const msgType = `${green('storage - google cloud storage')}`
122140
const bucketName = this._bucket
123141

src/lib/storage/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ import MinIOStorage from './minio'
33
import S3Storage from './s3'
44
import { GoogleCloudStorageParams, MinIOStorageParams, S3StorageParams } from './types'
55

6-
export type StorageType = 's3' | 'minio' | 'gcs'
6+
type StorageType = 's3' | 'minio' | 'gcs'
77

8-
export type StorageParams = {
8+
type StorageParams = {
99
storageType: StorageType
1010
params: S3StorageParams | MinIOStorageParams | GoogleCloudStorageParams
1111
}
1212

13+
type StorageInstance = S3Storage | MinIOStorage | GoogleCloudStorage
14+
1315
export default class Storage {
14-
constructor({ storageType, params }: StorageParams) {
16+
static create({ storageType, params }: StorageParams): StorageInstance {
1517
switch (storageType) {
1618
case 's3':
1719
// @ts-expect-error

src/lib/storage/minio.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,16 @@ export default class MinIOStorage {
3535
})
3636
}
3737

38+
/**
39+
* Generate keyfile
40+
*/
3841
private _generateKeyfile(values: string[]) {
3942
return values.join('/')
4043
}
4144

45+
/**
46+
* Get expires object
47+
*/
4248
public expiresObject() {
4349
const getExpired = this._expires.replace(/[^0-9]/g, '')
4450

@@ -48,6 +54,9 @@ export default class MinIOStorage {
4854
return { expiresIn, expiryDate }
4955
}
5056

57+
/**
58+
* Initialize storage
59+
*/
5160
async initialize() {
5261
const msgType = `${green('storage - minio')}`
5362
const bucketName = this._bucket
@@ -62,6 +71,9 @@ export default class MinIOStorage {
6271
}
6372
}
6473

74+
/**
75+
* Create bucket
76+
*/
6577
private async _createBucket() {
6678
const msgType = `${green('storage - minio')}`
6779
const bucketName = this._bucket
@@ -79,6 +91,9 @@ export default class MinIOStorage {
7991
}
8092
}
8193

94+
/**
95+
* Upload file
96+
*/
8297
async uploadFile({ directory, file }: UploadFileParams) {
8398
const keyfile = this._generateKeyfile([directory, file.filename])
8499

@@ -89,12 +104,15 @@ export default class MinIOStorage {
89104
}
90105

91106
const data = await this.client.fPutObject(this._bucket, keyfile, file.path, options)
92-
const signedUrl = await this.presignedURL(keyfile)
107+
const signedUrl = await this.presignedUrl(keyfile)
93108

94109
return { data, signedUrl }
95110
}
96111

97-
async presignedURL(keyfile: string) {
112+
/**
113+
* Generate presigned URL
114+
*/
115+
async presignedUrl(keyfile: string) {
98116
const msgType = `${green('storage - minio')}`
99117
const bucketName = this._bucket
100118

src/lib/storage/s3.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@ export default class S3Storage {
3131
})
3232
}
3333

34+
/**
35+
* Generate keyfile
36+
*/
3437
private _generateKeyfile(values: string[]) {
3538
return values.join('/')
3639
}
3740

41+
/**
42+
* Get expires object
43+
*/
3844
public expiresObject() {
3945
const getExpired = this._expires.replace(/[^0-9]/g, '')
4046

@@ -44,6 +50,9 @@ export default class S3Storage {
4450
return { expiresIn, expiryDate }
4551
}
4652

53+
/**
54+
* Initialize storage
55+
*/
4756
async initialize() {
4857
const msgType = `${green('storage - aws s3')}`
4958
const bucketName = this._bucket
@@ -63,6 +72,9 @@ export default class S3Storage {
6372
}
6473
}
6574

75+
/**
76+
* Create bucket
77+
*/
6678
private async _createBucket() {
6779
const msgType = `${green('storage - aws s3')}`
6880
const bucketName = this._bucket
@@ -81,6 +93,9 @@ export default class S3Storage {
8193
}
8294
}
8395

96+
/**
97+
* Upload file
98+
*/
8499
async uploadFile({ directory, file }: UploadFileParams) {
85100
const keyfile = this._generateKeyfile([directory, file.filename])
86101

@@ -94,12 +109,15 @@ export default class S3Storage {
94109
})
95110

96111
const data = await this.client.send(command)
97-
const signedUrl = await this.presignedURL(keyfile)
112+
const signedUrl = await this.presignedUrl(keyfile)
98113

99114
return { data, signedUrl }
100115
}
101116

102-
async presignedURL(keyfile: string) {
117+
/**
118+
* Generate presigned URL
119+
*/
120+
async presignedUrl(keyfile: string) {
103121
const msgType = `${green('storage - aws s3')}`
104122
const bucketName = this._bucket
105123

0 commit comments

Comments
 (0)