Skip to content

Commit

Permalink
custom URL short code
Browse files Browse the repository at this point in the history
  • Loading branch information
mishraomp committed Sep 4, 2024
1 parent 1e0ca02 commit 73e2875
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
8 changes: 7 additions & 1 deletion backend/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import {AppService} from './app.service';
import {UrlShortenDTO} from "./dto/url.shorten";
import isURL from 'validator/lib/isURL';
import {Response} from "express";
import * as process from "node:process";

@Controller()
export class AppController {
private logger = new Logger("AppController");

constructor(private readonly appService: AppService) {
}

Expand All @@ -30,8 +32,12 @@ export class AppController {
if (!this.isValidURL(urlShortenDTO.url)) {
throw new HttpException("Invalid URL", HttpStatus.BAD_REQUEST);
}
if (urlShortenDTO.customUrlCode && urlShortenDTO.customUrlCode.length != 12) {
throw new HttpException("Custom URL code too long", HttpStatus.BAD_REQUEST);
}
try {
return await this.appService.postURLShorten(urlShortenDTO.url);
const shortUrlCode = await this.appService.postURLShorten(urlShortenDTO.url, urlShortenDTO.customUrlCode);
return (process.env.APP_URL || 'http://localhost:3000/') + shortUrlCode;

Check failure

Code scanning / CodeQL

Reflected cross-site scripting High

Cross-site scripting vulnerability due to a
user-provided value
.
} catch (e) {
throw new HttpException({
status: HttpStatus.INTERNAL_SERVER_ERROR,
Expand Down
18 changes: 14 additions & 4 deletions backend/src/app.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Inject, Injectable, OnModuleDestroy} from "@nestjs/common";
import {HttpException, HttpStatus, Inject, Injectable, OnModuleDestroy} from "@nestjs/common";
import {Redis} from "ioredis";
@Injectable()
export class AppService implements OnModuleDestroy{
Expand All @@ -12,13 +12,23 @@ export class AppService implements OnModuleDestroy{
return this.redis.ping();
}

async postURLShorten(url: string): Promise<string> {
async postURLShorten(url: string, customUrlCode:string): Promise<string> {
const shortURLFromRedis = await this.redis.get(url);
if(shortURLFromRedis) {
return shortURLFromRedis;
}
const { nanoid } = await import('nanoid');
const shortURL = nanoid(12);
let shortURL:string;
if (customUrlCode) {
const existingURL = await this.redis.get(customUrlCode);
if(existingURL) {
throw new HttpException("Custom URL already exists",HttpStatus.CONFLICT);
}
shortURL = customUrlCode;
}else {
const { nanoid } = await import('nanoid');
shortURL = nanoid(12);
}

return new Promise((resolve, reject) => {
this.redis.multi()
.set(shortURL, url)
Expand Down
15 changes: 15 additions & 0 deletions backend/src/dto/url.shorten.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
import {ApiProperty} from "@nestjs/swagger";

export class UrlShortenDTO {
@ApiProperty({
example: "https://www2.gov.bc.ca/gov/content/environment",
description: "The URL for which short URL is to be generated",
required: true,
format: "url",
})
url: string;
@ApiProperty({
example: "W0jsiQdywoj",
description: "Random 12 character string, if not provided, system will generate it.",
maxLength: 12,
minLength: 12,
})
customUrlCode?: string;
}
2 changes: 2 additions & 0 deletions charts/app/templates/backend/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ spec:
value: info
- name: REDIS_HOST
value: {{.Release.Name}}-redis
- name: APP_URL
value: https://{{ .Release.Name }}-backend.apps.silver.devops.gov.bc.ca/
ports:
- name: http
containerPort: {{ .Values.backend.service.targetPort }}
Expand Down

0 comments on commit 73e2875

Please sign in to comment.