Skip to content

Commit

Permalink
Stream default image insteed of redirecting
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBartusek committed Feb 28, 2024
1 parent 360b392 commit c1822aa
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
File renamed without changes
8 changes: 6 additions & 2 deletions apps/api/src/images/images.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Controller, Get, Header, NotFoundException, Param, Redirect, Res } from
import { ApiTags } from '@nestjs/swagger';
import { Response } from 'express';
import { ImagesService } from './images.service';
import { createReadStream } from 'node:fs';
import { join } from 'node:path';

const ONE_DAY = 24 * 60 * 60;
const CACHE_TIME = 30 * ONE_DAY;
Expand All @@ -13,8 +15,10 @@ export class ImagesController {

@Get('default')
@Header('Cache-Control', `public, max-age=${CACHE_TIME}`)
@Redirect('/public/default.png', 302)
getDefault() {}
getDefault(@Res() res: Response) {
const file = createReadStream(join(process.cwd(), '/src/assets/default.png'));
file.pipe(res);
}

@Get(':key')
@Header('Cache-Control', `public, max-age=${CACHE_TIME}`)
Expand Down
5 changes: 0 additions & 5 deletions apps/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import RedisStore from 'connect-redis';
import { json } from 'express';
import * as session from 'express-session';
import * as passport from 'passport';
import { join } from 'path';
import { AppModule } from './app.module';
import Utils from './helpers/utils';
import redisClient from './redis/connect';
Expand All @@ -16,10 +15,6 @@ async function bootstrapNestApp() {
const app = await NestFactory.create<NestExpressApplication>(AppModule);

configureNestApp(app);

// Serve public folder (not frontend)
app.useStaticAssets(join(__dirname, '../..', 'public'), { prefix: '/public/' });

setupSwagger(app);

await app.listen(3000);
Expand Down

0 comments on commit c1822aa

Please sign in to comment.