Skip to content

Commit

Permalink
refactor: simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
sinedied committed Mar 28, 2024
1 parent b4f7329 commit 66315c4
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions packages/api/src/functions/get-documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpResponseInit> {
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
Expand All @@ -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'));
}
}

Expand Down

0 comments on commit 66315c4

Please sign in to comment.