@@ -9,6 +9,7 @@ import { FastifyReply, FastifyRequest } from 'fastify'
99import { LoginResponseDto } from '../../../authentication/dto/login-response.dto'
1010import { JwtIdentityPayload } from '../../../authentication/interfaces/jwt-payload.interface'
1111import { AuthManager } from '../../../authentication/services/auth-manager.service'
12+ import { serverConfig } from '../../../configuration/config.environment'
1213import { FilesManager } from '../../files/services/files-manager.service'
1314import { SendFile } from '../../files/utils/send-file'
1415import { SPACE_REPOSITORY } from '../../spaces/constants/spaces'
@@ -41,43 +42,67 @@ export class LinksManager {
4142 this . logger . warn ( `${ this . linkValidation . name } - ${ uuid } : ${ check } ` )
4243 }
4344 const spaceLink : SpaceLink = ok ? await this . linksQueries . spaceLink ( uuid ) : null
44- if ( spaceLink ?. owner ?. login ) {
45- spaceLink . owner . avatar = await getAvatarBase64 ( spaceLink . owner . login )
46- // for security reasons
47- delete spaceLink . owner . login
45+ if ( ok ) {
46+ if ( spaceLink ?. owner ?. login ) {
47+ spaceLink . owner . avatar = await getAvatarBase64 ( spaceLink . owner . login )
48+ // For security reasons
49+ delete spaceLink . owner . login
50+ }
51+ if ( spaceLink ?. share ) {
52+ // Only used when the link is a file or a directory
53+ spaceLink . fileEditors = serverConfig . fileEditors
54+ }
4855 }
4956 return { ok : ok , error : ok ? null : check , link : spaceLink }
5057 }
5158
52- async linkAccess ( identity : JwtIdentityPayload , uuid : string , req : FastifyRequest , res : FastifyReply ) : Promise < StreamableFile | LoginResponseDto > {
59+ async linkAccess (
60+ identity : JwtIdentityPayload ,
61+ uuid : string ,
62+ req : FastifyRequest ,
63+ res : FastifyReply
64+ ) : Promise < LoginResponseDto | Omit < LoginResponseDto , 'token' > > {
5365 const [ link , check , ok ] = await this . linkEnv ( identity , uuid )
5466 if ( ! ok ) {
5567 this . logger . warn ( `${ this . linkAccess . name } - *${ link . user . login } * (${ link . user . id } ) : ${ check } ` )
5668 throw new HttpException ( check as string , HttpStatus . BAD_REQUEST )
5769 }
5870 const user = new UserModel ( link . user )
59- const spaceLink : SpaceLink = await this . linksQueries . spaceLink ( uuid )
60- if ( ! spaceLink . space && ! spaceLink . share . isDir ) {
61- // download the file (authentication has been verified before)
62- this . logger . log ( `${ this . linkAccess . name } - *${ user . login } * (${ user . id } ) downloading ${ spaceLink . share . name } ` )
63- this . incrementLinkNbAccess ( link )
64- const spaceEnv : SpaceEnv = await this . spaceEnvFromLink ( user , spaceLink )
65- const sendFile : SendFile = this . filesManager . sendFileFromSpace ( spaceEnv , spaceLink . share . name )
66- try {
67- await sendFile . checks ( )
68- return await sendFile . stream ( req , res )
69- } catch ( e ) {
70- this . logger . error ( `${ this . linkAccess . name } - unable to send file : ${ e } ` )
71- throw new HttpException ( 'Unable to download file' , HttpStatus . INTERNAL_SERVER_ERROR )
72- }
73- } else if ( link . user . id !== identity . id ) {
74- // authenticate user to allow access to the directory
71+ if ( link . user . id !== identity . id ) {
72+ // Authenticate user to allow access to the directory
7573 this . logger . log ( `${ this . linkAccess . name } - *${ user . login } * (${ user . id } ) is logged` )
7674 this . incrementLinkNbAccess ( link )
7775 this . usersManager . updateAccesses ( user , req . ip , true ) . catch ( ( e : Error ) => this . logger . error ( `${ this . linkAccess . name } - ${ e } ` ) )
7876 return this . authManager . setCookies ( user , res )
7977 }
80- // already authenticated
78+ // Already authenticated
79+ return { user : user , server : serverConfig }
80+ }
81+
82+ async linkDownload ( identity : JwtIdentityPayload , uuid : string , req : FastifyRequest , res : FastifyReply ) : Promise < StreamableFile > {
83+ const [ link , check , ok ] = await this . linkEnv ( identity , uuid )
84+ if ( ! ok ) {
85+ this . logger . warn ( `${ this . linkDownload . name } - *${ link . user . login } * (${ link . user . id } ) : ${ check } ` )
86+ throw new HttpException ( check as string , HttpStatus . BAD_REQUEST )
87+ }
88+ const spaceLink : SpaceLink = await this . linksQueries . spaceLink ( uuid )
89+ if ( spaceLink . space || spaceLink . share ?. isDir ) {
90+ this . logger . error ( `${ this . linkDownload . name } - the provided link does not reference a downloadable file` )
91+ throw new HttpException ( 'This link does not allow file download' , HttpStatus . BAD_REQUEST )
92+ }
93+ const user = new UserModel ( link . user )
94+ // Download the file (authentication has been verified before)
95+ this . logger . log ( `${ this . linkDownload . name } - *${ user . login } * (${ user . id } ) downloading ${ spaceLink . share . name } ` )
96+ this . incrementLinkNbAccess ( link )
97+ const spaceEnv : SpaceEnv = await this . spaceEnvFromLink ( user , spaceLink )
98+ const sendFile : SendFile = this . filesManager . sendFileFromSpace ( spaceEnv , spaceLink . share . name )
99+ try {
100+ await sendFile . checks ( )
101+ return await sendFile . stream ( req , res )
102+ } catch ( e ) {
103+ this . logger . error ( `${ this . linkDownload . name } - unable to send file : ${ e } ` )
104+ throw new HttpException ( 'Unable to download file' , HttpStatus . INTERNAL_SERVER_ERROR )
105+ }
81106 }
82107
83108 async linkAuthentication ( identity : JwtIdentityPayload , uuid : string , linkPasswordDto : UserPasswordDto , req : FastifyRequest , res : FastifyReply ) {
0 commit comments