@@ -3,6 +3,8 @@ import { NotFoundError, ValidationError } from '../errors';
33import { LetterDto , PatchLetterResponse } from '../contracts/letters' ;
44import { mapToPatchLetterResponse } from '../mappers/letter-mapper' ;
55import { ApiErrorDetail } from '../contracts/errors' ;
6+ import { getSignedUrl } from "@aws-sdk/s3-request-presigner" ;
7+ import { S3Client , GetObjectCommand } from "@aws-sdk/client-s3" ;
68
79
810export const getLettersForSupplier = async ( supplierId : string , status : string , limit : number , letterRepo : LetterRepository ) : Promise < LetterBase [ ] > => {
@@ -30,7 +32,7 @@ export const patchLetterStatus = async (letterToUpdate: LetterDto, letterId: str
3032 return mapToPatchLetterResponse ( updatedLetter ) ;
3133}
3234
33- export const getLetterData = async ( supplierId : string , letterId : string , letterRepo : LetterRepository ) : Promise < LetterBase > => {
35+ export const getLetterDataUrl = async ( supplierId : string , letterId : string , letterRepo : LetterRepository ) : Promise < string > => {
3436
3537 let letter ;
3638
@@ -43,5 +45,20 @@ export const getLetterData = async (supplierId: string, letterId: string, letter
4345 throw error ;
4446 }
4547
48+ return getPresignedUrl ( letter . url ) ;
49+ }
50+
51+ async function getPresignedUrl ( s3Uri : string ) {
52+ const client = new S3Client ( ) ;
53+
54+ const url = new URL ( s3Uri ) ; // works for s3:// URIs
55+ const bucket = url . hostname ;
56+ const key = url . pathname . slice ( 1 ) ; // remove leading '/'
57+
58+ const command = new GetObjectCommand ( {
59+ Bucket : bucket ,
60+ Key : key ,
61+ } ) ;
4662
63+ return await getSignedUrl ( client , command , { expiresIn : 3600 } ) ;
4764}
0 commit comments