Skip to content

Commit beb6752

Browse files
committed
add storage module
1 parent 240adb8 commit beb6752

File tree

8 files changed

+403
-10
lines changed

8 files changed

+403
-10
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ lerna-debug.log*
3636

3737

3838
# Adding for this project
39-
.env
39+
.env
40+
41+
src/service-account.json

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"nest": "nest"
2222
},
2323
"dependencies": {
24+
"@google-cloud/storage": "^6.12.0",
2425
"@nestjs-modules/mailer": "^1.9.1",
2526
"@nestjs/cache-manager": "^2.1.0",
2627
"@nestjs/common": "^10.0.0",

src/apps/app.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Module } from '@nestjs/common';
2-
import { ConfigModule, ConfigService } from '@nestjs/config';
2+
import { ConfigModule } from '@nestjs/config';
33
import { JwtModule } from '@nestjs/jwt';
44
import { TypeOrmModule } from '@nestjs/typeorm';
55
import { defaultTypeOrmConfig } from 'src/config/typeorm.config';
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { join } from "path";
2+
import { Bucket, StorageOptions, Storage as GCS } from "@google-cloud/storage";
3+
import { ProviderStorage } from "./provider.abstract";
4+
5+
export class GoogleCloudStorage implements ProviderStorage<Bucket> {
6+
7+
private _instance: Bucket = null;
8+
9+
constructor(
10+
public storageOption: StorageOptions,
11+
public config: {
12+
bucketName: string,
13+
prefix: string
14+
}
15+
) {
16+
17+
}
18+
19+
instance(): Bucket {
20+
if(this._instance == null) {
21+
this._instance = new GCS(this.storageOption).bucket('tradeapp-f8e74.appspot.com');
22+
}
23+
24+
return this._instance;
25+
}
26+
27+
async put(destination: string, buffer: Buffer, mimetype?: string) {
28+
29+
const file = this.instance().file(destination);
30+
31+
await file.save(buffer, {
32+
gzip: true,
33+
});
34+
35+
return file;
36+
}
37+
38+
async move(origin:string, destination: string) {
39+
console.log(`${this.config.prefix}/${origin}`)
40+
console.log(`${this.config.prefix}/${destination}`)
41+
42+
await this.instance().file(`${this.config.prefix}/${origin}`).move(`${this.config.prefix}/${destination}`);
43+
44+
return destination;
45+
}
46+
47+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export abstract class ProviderStorage<Instance> {
2+
abstract put(destination: string, buffer: Buffer, mimetype?: string): Promise<any>;
3+
abstract move(origin:string, destination: string): Promise<any>
4+
abstract instance(): Instance | any
5+
}

src/modules/storage/storage.module.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Module } from '@nestjs/common';
2+
import { ConfigModule } from '@nestjs/config';
3+
import { StorageService } from './storage.service';
4+
5+
@Module({
6+
imports: [
7+
ConfigModule
8+
],
9+
providers: [StorageService],
10+
exports: [StorageService]
11+
})
12+
export class StorageModule {}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { ConfigService } from '@nestjs/config';
3+
import { join } from 'path';
4+
import { GoogleCloudStorage } from './providers/gcs.provider';
5+
import { ProviderStorage } from './providers/provider.abstract';
6+
7+
@Injectable()
8+
export class StorageService {
9+
10+
private environment = 'development';
11+
12+
private providerInstance: ProviderStorage<any>;
13+
14+
private domainURL: string;
15+
private provider: string;
16+
private prefixToReplace: any;
17+
18+
constructor(
19+
private configService: ConfigService,
20+
) {
21+
this.environment = this.configService.get('STORAGE_UPLOAD_PREFIX');
22+
23+
this.prefixToReplace = {
24+
'gcs': 'gcs://',
25+
's3': 's3://',
26+
'cloudinary': 'cld://'
27+
}
28+
}
29+
30+
setProvider(provider: 'gcs') {
31+
this.provider = provider;
32+
33+
switch(provider) {
34+
35+
case 'gcs': {
36+
this.domainURL = this.configService.get('GCS_DOMAIN_URL') ?? 'http://___domain.com';
37+
this.providerInstance = new GoogleCloudStorage({
38+
keyFile: join(__dirname, '..', '..', 'service-account.json'),
39+
projectId: this.configService.get('GCS_PROJECT_ID') ?? 'intervest-io',
40+
}, { bucketName: this.configService.get('GCS_BUCKET_NAME') ??'intervest-io.appspot.com', prefix: this.environment })
41+
}
42+
43+
default: {
44+
throw Error('Storage Provider not available.')
45+
}
46+
}
47+
48+
return this;
49+
}
50+
51+
async put(destination: string, buffer: Buffer, mimetype?: string) {
52+
53+
return await this.providerInstance.put(destination, buffer, mimetype)
54+
55+
}
56+
57+
async move(origin:string, destination: string) {
58+
59+
return await this.providerInstance.move(origin, destination);
60+
}
61+
62+
url(path: string) {
63+
const currentPath = path.replace(this.prefixToReplace[this.provider], `${this.domainURL}/`);
64+
65+
return currentPath;
66+
}
67+
68+
}

0 commit comments

Comments
 (0)