@@ -16,9 +16,8 @@ import { FilesInterceptor } from '@nestjs/platform-express';
1616import { AttachmentsService } from 'omniboxd/attachments/attachments.service' ;
1717import { Request , Response } from 'express' ;
1818import { UserId } from 'omniboxd/decorators/user-id.decorator' ;
19- import { Public } from 'omniboxd/auth/decorators/public.auth.decorator' ;
20- import { Cookies } from 'omniboxd/decorators/cookie.decorators' ;
2119import { AuthService } from 'omniboxd/auth/auth.service' ;
20+ import { CookieAuth } from 'omniboxd/auth' ;
2221
2322@Controller ( 'api/v1/attachments' )
2423export class AttachmentsController {
@@ -62,65 +61,47 @@ export class AttachmentsController {
6261 ) ;
6362 }
6463
65- @Public ( )
64+ setRedirect ( req : Request , res : Response ) {
65+ res
66+ . setHeader ( 'Cache-Control' , 'no-cache, no-store, must-revalidate' )
67+ . status ( HttpStatus . FOUND )
68+ . redirect ( `/user/login?redirect=${ encodeURIComponent ( req . url ) } ` ) ;
69+ }
70+
71+ @CookieAuth ( { onAuthFail : 'continue' } )
6672 @Get ( 'images/:attachmentId' )
6773 async displayImage (
74+ @UserId ( { optional : true } ) userId : string | undefined ,
6875 @Req ( ) req : Request ,
6976 @Res ( ) res : Response ,
70- @Cookies ( 'token' ) token : string ,
7177 @Param ( 'attachmentId' ) attachmentId : string ,
7278 ) {
73- let userId = '' ;
74-
75- if ( token ) {
76- const payload = this . authService . jwtVerify ( token ) ;
77- if ( payload && payload . sub ) {
78- userId = payload . sub ;
79- }
80- }
81-
82- this . logger . debug ( { userId, token, cookies : req . cookies } ) ;
8379 if ( userId ) {
84- return await this . attachmentsService . displayImage (
80+ return await this . attachmentsService . displayMedia (
8581 attachmentId ,
8682 userId ,
8783 res ,
8884 ) ;
8985 }
90- res
91- . setHeader ( 'Cache-Control' , 'no-cache, no-store, must-revalidate' )
92- . status ( HttpStatus . FOUND )
93- . redirect ( `/user/login?redirect=${ encodeURIComponent ( req . url ) } ` ) ;
86+ this . setRedirect ( req , res ) ;
9487 }
9588
96- @Public ( )
89+ @CookieAuth ( { onAuthFail : 'continue' } )
9790 @Get ( 'media/:attachmentId' )
9891 async displayMedia (
92+ @UserId ( { optional : true } ) userId : string | undefined ,
9993 @Req ( ) req : Request ,
10094 @Res ( ) res : Response ,
101- @Cookies ( 'token' ) token : string ,
10295 @Param ( 'attachmentId' ) attachmentId : string ,
10396 ) {
104- let userId = '' ;
105-
106- if ( token ) {
107- const payload = this . authService . jwtVerify ( token ) ;
108- if ( payload && payload . sub ) {
109- userId = payload . sub ;
110- }
111- }
112-
11397 if ( userId ) {
11498 return await this . attachmentsService . displayMedia (
11599 attachmentId ,
116100 userId ,
117101 res ,
118102 ) ;
119103 }
120- res
121- . setHeader ( 'Cache-Control' , 'no-cache, no-store, must-revalidate' )
122- . status ( HttpStatus . FOUND )
123- . redirect ( `/user/login?redirect=${ encodeURIComponent ( req . url ) } ` ) ;
104+ this . setRedirect ( req , res ) ;
124105 }
125106
126107 @Delete ( ':attachmentId' )
0 commit comments