From 7ccba32bd008945372b8c47c9b0e70706c6a77d6 Mon Sep 17 00:00:00 2001 From: sinedied Date: Thu, 28 Mar 2024 09:51:07 +0100 Subject: [PATCH] refactor: simplify code --- packages/api/src/functions/get-documents.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/api/src/functions/get-documents.ts b/packages/api/src/functions/get-documents.ts index ef75d74..4df7651 100644 --- a/packages/api/src/functions/get-documents.ts +++ b/packages/api/src/functions/get-documents.ts @@ -5,18 +5,21 @@ import { finished } from 'node:stream/promises'; import { HttpRequest, HttpResponseInit, InvocationContext, app } from '@azure/functions'; import { BlobServiceClient } from '@azure/storage-blob'; import 'dotenv/config'; +import { notFound } from '../utils'; async function getDocument(request: HttpRequest, context: InvocationContext): Promise { + const connectionString = process.env.AZURE_STORAGE_CONNECTION_STRING; + const containerName = process.env.AZURE_STORAGE_CONTAINER_NAME; const { fileName } = request.params; try { let fileData: Uint8Array; - if (process.env.AZURE_STORAGE_CONNECTION_STRING && process.env.AZURE_STORAGE_CONTAINER_NAME) { - const blobServiceClient = BlobServiceClient.fromConnectionString(process.env.AZURE_STORAGE_CONNECTION_STRING); - const containerClient = blobServiceClient.getContainerClient(process.env.AZURE_STORAGE_CONTAINER_NAME); - const blobClient = containerClient.getBlobClient(fileName); - const response = await blobClient.download(); + if (connectionString && containerName) { + context.log(`Reading blob from: "${containerName}/${fileName}"`); + const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString); + const containerClient = blobServiceClient.getContainerClient(containerName); + const response = await containerClient.getBlobClient(fileName).download(); fileData = await streamToBuffer(response.readableStreamBody!); } else { // If no environment variables are set, it means we are running locally @@ -30,10 +33,7 @@ async function getDocument(request: HttpRequest, context: InvocationContext): Pr body: fileData, }; } catch { - return { - status: 404, - jsonBody: { error: 'Document not found' }, - }; + return notFound(new Error('Document not found')); } }