Skip to content

Commit

Permalink
✨ [Feat]: record client 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
g00hyun committed Dec 2, 2024
1 parent ddcba1a commit f742b79
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 11 deletions.
3 changes: 3 additions & 0 deletions apps/media/src/common/clients/record-client.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IRecordClient {
post<T>(path: string, data: any): Promise<T>;
}
27 changes: 27 additions & 0 deletions apps/media/src/common/clients/record.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { ErrorStatus } from '../responses/exceptions/errorStatus';
import { HttpService } from '@nestjs/axios';
import { CustomWsException } from '../responses/exceptions/custom-ws.exception';
import { IRecordClient } from './record-client.interface';

@Injectable()
export class RecordClient implements IRecordClient {
constructor(private readonly httpService: HttpService, private readonly configService: ConfigService) {}

private readonly baseUrl = this.configService.get<string>('RECORD_SERVER_URL');

async post<T>(path: string, data: any): Promise<T> {
try {
const response = await this.httpService.post<T>(`${this.baseUrl}${path}`, data).toPromise();
return response.data;
} catch (error) {
throw this.handleError(error);
}
}

// TODO: Error custom 필요
private handleError(_error: any): Error {
throw new CustomWsException(ErrorStatus.RECORD_SERVER_ERROR);
}
}
6 changes: 6 additions & 0 deletions apps/media/src/common/responses/exceptions/errorStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ export class ErrorStatus {

static readonly API_SERVER_ERROR = new ErrorStatus(500, 'COMMON_5001', 'API 서비스 로직에서 문제가 발생했습니다.');

static readonly RECORD_SERVER_ERROR = new ErrorStatus(
500,
'COMMON_5002',
'RECORD 서비스 로직에서 문제가 발생했습니다.',
);

// User Errors
static readonly USER_NOT_FOUND = new ErrorStatus(404, 'MEMBER_4000', '사용자를 찾을 수 없습니다.');

Expand Down
6 changes: 3 additions & 3 deletions apps/media/src/sfu/sfu.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ConsumerService } from './services/consumer.service';
import { BroadcastModule } from '../broadcast/broadcast.module';
import { ClientService } from './services/client.service';
import { RecordService } from './services/record.service';
import { ApiClient } from 'src/common/clients/api.client';
import { RecordClient } from 'src/common/clients/record.client';

@Module({
imports: [BroadcastModule, HttpModule],
Expand All @@ -25,8 +25,8 @@ import { ApiClient } from 'src/common/clients/api.client';
RecordService,
ClientService,
{
provide: 'API_CLIENT',
useClass: ApiClient,
provide: 'RECORD_CLIENT',
useClass: RecordClient,
},
],
})
Expand Down
12 changes: 4 additions & 8 deletions apps/media/src/sfu/sfu.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import { ClientService } from './services/client.service';
import { RecordService } from './services/record.service';
import { User } from '../types/user';
import { SetVideoQualityDto } from './dto/set-video-quality.dto';
import { IApiClient } from 'src/common/clients/api-client.interface';
// import axios from 'axios';
import { IRecordClient } from 'src/common/clients/record-client.interface';

@Injectable()
export class SfuService {
Expand All @@ -29,17 +28,14 @@ export class SfuService {
private readonly recordService: RecordService,
private readonly clientService: ClientService,
private readonly configService: ConfigService,
@Inject('API_CLIENT')
private readonly apiClient: IApiClient,
@Inject('RECORD_CLIENT')
private readonly recordClient: IRecordClient,
) {}

async createRoom(clientId: string, user: User) {
const room = await this.roomService.createRoom();
// await axios.post(`${this.configService.get('RECORD_SERVER_URL')}/thumbnail`, {
// roomId: room.id,
// });

this.apiClient.post(`${this.configService.get('RECORD_SERVER_URL')}/thumbnail`, { roomId: room.id });
await this.recordClient.post('/thumbnail', { roomId: room.id });

const thumbnail = `${this.configService.get('PUBLIC_RECORD_SERVER_URL')}/statics/thumbnails/${room.id}.jpg`;
await this.broadcasterService.createBroadcast(
Expand Down
7 changes: 7 additions & 0 deletions apps/record/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,17 @@ if (!fs.existsSync(recordsDirPath)) {
}

app.post('/thumbnail', (req, res) => {
console.log('Received thumbnail request:', req.body);
const { roomId } = req.body;
try {
console.log('Default thumbnail path:', defaultThumbnailPath);
if (!fs.existsSync(defaultThumbnailPath)) {
console.error('Default thumbnail not found');
res.status(404).send({ error: 'Default thumbnail not found' });
}
const newThumbnailPath = path.join(thumbnailsDirPath, `${roomId}.jpg`);
fs.copyFileSync(defaultThumbnailPath, newThumbnailPath);
console.log('Thumbnail created:', newThumbnailPath);
res.send({ success: true });
} catch (error) {
res.status(500).send({ success: false, error: 'Failed to create thumbnail' });
Expand Down

0 comments on commit f742b79

Please sign in to comment.