diff --git a/packages/api-clients/template-bff.hbs b/packages/api-clients/template-bff.hbs index b77598e8ad..26c5f2740d 100644 --- a/packages/api-clients/template-bff.hbs +++ b/packages/api-clients/template-bff.hbs @@ -63,6 +63,8 @@ export const {{@key}}Endpoints = makeApi([ response: z.instanceof(Buffer), {{else if (and (eq method "get") (eq path "/eservices/:eServiceId/consumers"))}} response: z.instanceof(Buffer), + {{else if (and (eq method "get") (eq path "/eservices/:eServiceId/descriptors/:descriptorId/documents/:documentId"))}} + response: z.instanceof(Buffer), {{else}} response: {{{response}}}, {{/if}} diff --git a/packages/bff/.env b/packages/bff/.env index 395faf920f..d02018ac05 100644 --- a/packages/bff/.env +++ b/packages/bff/.env @@ -32,6 +32,7 @@ GENERATED_JWT_SECONDS_DURATION=60 S3_CUSTOM_SERVER=true S3_SERVER_HOST=http://localhost S3_SERVER_PORT=9000 +S3_BUCKET=interop-local-bucket RISK_ANALYSIS_DOCUMENTS_PATH="risk-analysis/docs" diff --git a/packages/bff/src/config/config.ts b/packages/bff/src/config/config.ts index 3ff643432c..33b0bc9f75 100644 --- a/packages/bff/src/config/config.ts +++ b/packages/bff/src/config/config.ts @@ -111,6 +111,7 @@ const BffProcessConfig = CommonHTTPServiceConfig.and(TenantProcessServerConfig) .and(TokenGenerationConfig) .and(SessionTokenGenerationConfig) .and(FileManagerConfig) + .and(S3RiskAnalysisConfig) .and(AllowedListConfig) .and(SelfCareConfig) .and(S3RiskAnalysisConfig) diff --git a/packages/bff/src/routers/catalogRouter.ts b/packages/bff/src/routers/catalogRouter.ts index c58b1cd655..2090551601 100644 --- a/packages/bff/src/routers/catalogRouter.ts +++ b/packages/bff/src/routers/catalogRouter.ts @@ -1,4 +1,5 @@ /* eslint-disable @typescript-eslint/explicit-function-return-type */ +import { constants } from "http2"; import { ZodiosEndpointDefinitions } from "@zodios/core"; import { ZodiosRouter } from "@zodios/express"; import { bffApi } from "pagopa-interop-api-clients"; @@ -331,7 +332,25 @@ const catalogRouter = ( ) .get( "/eservices/:eServiceId/descriptors/:descriptorId/documents/:documentId", - async (_req, res) => res.status(501).send() + async (req, res) => { + const ctx = fromBffAppContext(req.ctx, req.headers); + try { + const { contentType, document } = + await catalogService.getEServiceDocumentById( + unsafeBrandId(req.params.eServiceId), + unsafeBrandId(req.params.descriptorId), + unsafeBrandId(req.params.documentId), + ctx + ); + return res + .header(constants.HTTP2_HEADER_CONTENT_TYPE, contentType) + .status(200) + .end(document); + } catch (error) { + const errorRes = makeApiProblem(error, emptyErrorMapper, ctx.logger); + return res.status(errorRes.status).json(errorRes).end(); + } + } ) .post( "/eservices/:eServiceId/descriptors/:descriptorId/clone", diff --git a/packages/bff/src/services/catalogService.ts b/packages/bff/src/services/catalogService.ts index e5d25f2325..7b8f4a9e46 100644 --- a/packages/bff/src/services/catalogService.ts +++ b/packages/bff/src/services/catalogService.ts @@ -606,6 +606,26 @@ export function catalogServiceBuilder( return toBffCatalogApiEserviceRiskAnalysis(riskAnalysis); }, + getEServiceDocumentById: async ( + eServiceId: EServiceId, + descriptorId: DescriptorId, + documentId: EServiceDocumentId, + ctx: WithLogger + ): Promise<{ contentType: string; document: Buffer }> => { + const { path, contentType } = + await catalogProcessClient.getEServiceDocumentById({ + params: { + eServiceId, + descriptorId, + documentId, + }, + headers: ctx.headers, + }); + + const stream = await fileManager.get(config.s3Bucket, path, ctx.logger); + + return { contentType, document: Buffer.from(stream) }; + }, createDescriptor: async ( eServiceId: string, { headers, logger }: WithLogger diff --git a/packages/commons/src/file-manager/utils.ts b/packages/commons/src/file-manager/utils.ts index 28e3b62218..b2a4df0de2 100644 --- a/packages/commons/src/file-manager/utils.ts +++ b/packages/commons/src/file-manager/utils.ts @@ -1,5 +1,4 @@ export const streamToString = (data: Uint8Array): string => { const decoder = new TextDecoder("utf-8"); - return decoder.decode(data); };