Skip to content

Commit

Permalink
[feat] add swagger and generate redirectUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
erik1110 committed Jan 22, 2024
1 parent 3289cef commit 981c3fb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
15 changes: 14 additions & 1 deletion src/features/image/dto/fileUpload.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,17 @@ import { ApiProperty } from "@nestjs/swagger";
export class FileUploadDto {
@ApiProperty({ type: 'string', format: 'binary' })
file: any;
}
}

export class GetImageSuccessDto {
@ApiProperty({ example: true })
status: boolean;

@ApiProperty({ example: '取得圖片網址' })
message: string;

@ApiProperty({
example: "http://localhost:3000/api/v1/url/HSIARDTXC"
})
data: object;
}
38 changes: 31 additions & 7 deletions src/features/image/image.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { Controller, Get, Param, Post, Res, UploadedFile, UseInterceptors } from '@nestjs/common';
import { Controller, Get, HttpStatus, Param, Post, Res, UploadedFile, UseGuards, UseInterceptors } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { ImageService } from './image.service';
import { ApiBody, ApiConsumes } from '@nestjs/swagger';
import { FileUploadDto } from './dto/fileUpload.dto';
import { ApiBearerAuth, ApiBody, ApiConsumes, ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
import { FileUploadDto, GetImageSuccessDto } from './dto/fileUpload.dto';
import { UrlService } from '../url/url.service';
import { AuthGuard } from '@nestjs/passport';
import { ApiErrorDecorator } from 'src/common/decorators/error/error.decorator';
import { getHttpResponse } from 'src/utils/successHandler';

@Controller('image')
@ApiTags('Image - 上傳圖片')
@ApiErrorDecorator(
HttpStatus.INTERNAL_SERVER_ERROR,
'CriticalError',
'系統錯誤,請洽系統管理員',
)
// @UseGuards(AuthGuard('jwt'))
// @ApiBearerAuth()
@Controller('/api/v1/image')
export class ImageController {
constructor(
private readonly imageService: ImageService,
Expand All @@ -15,13 +26,26 @@ export class ImageController {
@Post('upload')
@UseInterceptors(FileInterceptor('file'))
@ApiConsumes('multipart/form-data')
@ApiOperation({ summary: '上傳圖片 Upload an image' })
@ApiOkResponse({ type: GetImageSuccessDto })
@ApiBody({
description: 'Upload an image file',
description: "網址會由該服務進行轉址 The URL will be redirected by the service.",
type: FileUploadDto,
})
async uploadImage(@UploadedFile() file){
const imageUrl = await this.imageService.uploadImage(file);
const shortenUrl = await this.urlService.shortenUrl(imageUrl)
return { shortenUrl };
let shortenUrl = await this.urlService.shortenUrl(imageUrl)
if (process.env.NODE_ENV === 'dev') {
shortenUrl = `http://localhost:${process.env.PORT}/api/v1/url/`;
} else {
shortenUrl = `https://${process.env.PRODUCTION_URL}/api/v1/url/`;
}

const shortenedUrl = await this.urlService.shortenUrl(imageUrl);
const redirectUrl = shortenUrl + shortenedUrl;
return getHttpResponse.successResponse({
message: '取得圖片網址',
data: redirectUrl,
});
}
}
16 changes: 13 additions & 3 deletions src/features/url/url.controller.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import { Controller, Get, Param, Redirect } from '@nestjs/common';
import { Controller, Get, HttpStatus, Param, Redirect, UseGuards } from '@nestjs/common';
import { UrlService } from './url.service';
import { ApiErrorDecorator } from 'src/common/decorators/error/error.decorator';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { AppError } from 'src/utils/appError';

@Controller('url')
@ApiTags('ShortenURL - 短網址')
@ApiErrorDecorator(
HttpStatus.INTERNAL_SERVER_ERROR,
'CriticalError',
'系統錯誤,請洽系統管理員',
)
@Controller('/api/v1/url')
export class UrlController {
constructor(private readonly urlService: UrlService) {}

@Get(':shortUrl')
@ApiOperation({ summary: '轉址短網址 Redirect short URL' })
@Redirect()
async redirectToOriginalUrl(@Param('shortUrl') shortUrl: string): Promise<{ url: string }> {
const originalUrl = await this.urlService.getOriginalUrl(shortUrl);

if (originalUrl) {
return { url: originalUrl };
} else {
return { url: '/not-found' }; // Redirect to a not-found page or handle accordingly
throw new AppError(HttpStatus.BAD_REQUEST, 'UserError', '無此網址或網址已失效');
}
}
}

0 comments on commit 981c3fb

Please sign in to comment.