Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/ProxyImage: 이미지 프록시 stream으로 개선 #48

Open
wants to merge 1 commit into
base: stage
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/core/intercepters/response.intercepter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export interface Response<T> {
@Injectable()
export class ResponseInterceptor<T> implements NestInterceptor<T, Response<T>> {
intercept(_: ExecutionContext, next: CallHandler): Observable<Response<T>> {
const request = _.switchToHttp().getRequest();
if (request.path.startsWith('/proxy')) {
return next.handle();
}

return next.handle().pipe(
map((data: T) => {
return { message: 'success', data };
Expand Down
15 changes: 12 additions & 3 deletions src/proxy/proxy.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Controller, Get, Query, Response } from '@nestjs/common';
import { Controller, Get, Param, Query, Res } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';

import { Response as Res } from 'express';
import { Response } from 'express';

import { ProxyService } from 'src/proxy/proxy.service';

Expand All @@ -10,9 +10,18 @@ import { ProxyService } from 'src/proxy/proxy.service';
export class ProxyController {
constructor(private readonly proxyService: ProxyService) {}

@Get(':host/:path(*)')
async getProxyImage(
@Res({ passthrough: true }) res: Response,
@Param('host') host: string,
@Param('path') path: string,
) {
return await this.proxyService.proxyImage(host, path, res);
}

// TODO : 한정된 도메인에 대해서만 proxy 하도록 조건 추가 필요
@Get()
async getKakaoImage(@Query('url') url: string, @Response() res: Res) {
async getKakaoImage(@Query('url') url: string, @Res() res: Response) {
const result = await this.proxyService.fetchKakaoImage(url);

res.setHeader('Content-Type', result.contentType);
Expand Down
24 changes: 23 additions & 1 deletion src/proxy/proxy.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';
import { Injectable, StreamableFile } from '@nestjs/common';

import { Response } from 'express';
import { map } from 'rxjs/operators';
import { isReadable } from 'stream';

@Injectable()
export class ProxyService {
Expand All @@ -19,4 +21,24 @@ export class ProxyService {
)
.toPromise();
}

async proxyImage(host: string, path: string, res: Response) {
const { data, headers, status } = await this.httpService.axiosRef.request({
url: `https://${host}/${path}`,
method: 'GET',
responseType: 'stream',
validateStatus: () => true,
decompress: false,
});

for (const key in headers) {
res.setHeader(key, headers[key]);
}
res.status(status);
if (isReadable(data)) {
return new StreamableFile(data);
} else {
throw new Error('Stream is not readable');
}
}
}
Loading