-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPGO-108] Upload image on register (#22)
* update user schema * add file proto * init file module * update auth proto * upload profile image when register * fix * fix failed test * fix failed test * fix
- Loading branch information
1 parent
3917cfe
commit aeb4ef9
Showing
15 changed files
with
274 additions
and
52 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
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
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
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
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
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
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
2 changes: 2 additions & 0 deletions
2
src/database/migrations/20231106224059_add_photo_file_name_column/migration.sql
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,2 @@ | ||
-- AlterTable | ||
ALTER TABLE "User" ADD COLUMN "photoFileName" TEXT; |
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
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,25 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
import { ClientsModule, Transport } from '@nestjs/microservices'; | ||
import { join } from 'path'; | ||
import { FileService } from './file.service'; | ||
|
||
@Module({ | ||
imports: [ | ||
ConfigModule.forRoot(), | ||
ClientsModule.register([ | ||
{ | ||
name: 'FILE_PACKAGE', | ||
transport: Transport.GRPC, | ||
options: { | ||
package: 'file', | ||
protoPath: join(__dirname, '../proto/file.proto'), | ||
url: process.env.FILE_GRPC_URL, | ||
}, | ||
}, | ||
]), | ||
], | ||
providers: [FileService], | ||
exports: [FileService], | ||
}) | ||
export class FileModule {} |
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,60 @@ | ||
/* eslint-disable */ | ||
import { GrpcMethod, GrpcStreamMethod } from "@nestjs/microservices"; | ||
import { Observable } from "rxjs"; | ||
|
||
export const protobufPackage = "file"; | ||
|
||
export interface UploadFileRequest { | ||
filename: string; | ||
data: Uint8Array; | ||
userId: string; | ||
} | ||
|
||
export interface UploadFileResponse { | ||
url: string; | ||
filename: string; | ||
} | ||
|
||
export interface GetSignedURLRequest { | ||
filename: string; | ||
userId: string; | ||
} | ||
|
||
export interface GetSignedURLResponse { | ||
url: string; | ||
} | ||
|
||
export const FILE_PACKAGE_NAME = "file"; | ||
|
||
export interface FileServiceClient { | ||
uploadFile(request: UploadFileRequest): Observable<UploadFileResponse>; | ||
|
||
getSignedUrl(request: GetSignedURLRequest): Observable<GetSignedURLResponse>; | ||
} | ||
|
||
export interface FileServiceController { | ||
uploadFile( | ||
request: UploadFileRequest, | ||
): Promise<UploadFileResponse> | Observable<UploadFileResponse> | UploadFileResponse; | ||
|
||
getSignedUrl( | ||
request: GetSignedURLRequest, | ||
): Promise<GetSignedURLResponse> | Observable<GetSignedURLResponse> | GetSignedURLResponse; | ||
} | ||
|
||
export function FileServiceControllerMethods() { | ||
return function (constructor: Function) { | ||
const grpcMethods: string[] = ["uploadFile", "getSignedUrl"]; | ||
for (const method of grpcMethods) { | ||
const descriptor: any = Reflect.getOwnPropertyDescriptor(constructor.prototype, method); | ||
GrpcMethod("FileService", method)(constructor.prototype[method], method, descriptor); | ||
} | ||
const grpcStreamMethods: string[] = []; | ||
for (const method of grpcStreamMethods) { | ||
const descriptor: any = Reflect.getOwnPropertyDescriptor(constructor.prototype, method); | ||
GrpcStreamMethod("FileService", method)(constructor.prototype[method], method, descriptor); | ||
} | ||
}; | ||
} | ||
|
||
export const FILE_SERVICE_NAME = "FileService"; |
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,28 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { FileService } from './file.service'; | ||
|
||
describe('FileService', () => { | ||
let service: FileService; | ||
|
||
const mockFileClient = { | ||
getService: jest.fn(), | ||
}; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
FileService, | ||
{ | ||
provide: 'FILE_PACKAGE', | ||
useValue: mockFileClient, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<FileService>(FileService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
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,29 @@ | ||
import { Inject, Injectable, Logger } from '@nestjs/common'; | ||
import { ClientGrpc } from '@nestjs/microservices'; | ||
import { catchError, firstValueFrom } from 'rxjs'; | ||
import { | ||
FileServiceClient, | ||
UploadFileRequest, | ||
UploadFileResponse, | ||
} from './file.pb'; | ||
|
||
@Injectable() | ||
export class FileService { | ||
private fileClient: FileServiceClient; | ||
private readonly logger = new Logger(FileService.name); | ||
|
||
constructor(@Inject('FILE_PACKAGE') private client: ClientGrpc) { | ||
this.fileClient = this.client.getService<FileServiceClient>('FileService'); | ||
} | ||
|
||
async uploadFile(request: UploadFileRequest): Promise<UploadFileResponse> { | ||
return await firstValueFrom( | ||
this.fileClient.uploadFile(request).pipe( | ||
catchError((error) => { | ||
this.logger.error(error); | ||
throw error; | ||
}), | ||
), | ||
); | ||
} | ||
} |
Oops, something went wrong.