Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implenet new datamodel on the backend #9

Merged
merged 5 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"rules": {
"prettier/prettier": 2,
"no-console": 0,
"no-process-env": 0
"no-process-env": 0,
"no-inline-comments": 0,
"line-comment-position": 0
},
"globals": {
"NodeJS": true
Expand Down
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const logger: Logger = pino({
});

// Database handling and export
export const connectToDb = async (datasourceOptions: DataSourceOptions) => {
export const databaseManager = async (datasourceOptions: DataSourceOptions) => {
dbManager = new DatabaseManager(datasourceOptions, logger);
await dbManager.initializeDataSource();
};
Expand Down
34 changes: 27 additions & 7 deletions src/controllers/blob-storage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { BlobServiceClient, ContainerClient, StorageSharedKeyCredential } from '@azure/storage-blob';
import { Readable } from 'stream';

import {
BlobServiceClient,
BlobUploadCommonResponse,
ContainerClient,
StorageSharedKeyCredential
} from '@azure/storage-blob';
import * as dotenv from 'dotenv';
import pino from 'pino';

Expand All @@ -23,9 +30,6 @@ export class BlobStorageService {
private readonly containerClient: ContainerClient;

public constructor() {
logger.debug(
`Creating BlobServiceClient and ContainerClient for blob storage with account name '${accountName}' and container name '${containerName}'`
);
const sharedKeyCredential = new StorageSharedKeyCredential(accountName, accountKey);
this.blobServiceClient = new BlobServiceClient(
`https://${accountName}.blob.core.windows.net`,
Expand All @@ -42,17 +46,26 @@ export class BlobStorageService {
return this.containerClient;
}

public async uploadFile(fileName: string | undefined, fileContent: Buffer) {
public async uploadFile(fileName: string | undefined, fileContent: Readable) {
if (fileName === undefined) {
throw new Error('File name is undefined');
}
if (fileContent === undefined) {
throw new Error('File content is undefined');
}
logger.info(`Uploading file with file '${fileName}' to blob storage`);

const blockBlobClient = this.containerClient.getBlockBlobClient(fileName);

const uploadBlobResponse = await blockBlobClient.upload(fileContent, fileContent.length);
const uploadOptions = {
bufferSize: 4 * 1024 * 1024, // 4MB buffer size
maxBuffers: 5 // Parallelism of 5
};
const uploadBlobResponse: BlobUploadCommonResponse = await blockBlobClient.uploadStream(
fileContent,
uploadOptions.bufferSize,
uploadOptions.maxBuffers
);
return uploadBlobResponse;
}

Expand All @@ -71,6 +84,7 @@ export class BlobStorageService {
}

public async readFile(fileName: string) {
logger.info(`Getting file with file '${fileName}' to blob storage`);
const blockBlobClient = this.containerClient.getBlockBlobClient(fileName);
const downloadBlockBlobResponse = await blockBlobClient.download();
const readableStreamBody = downloadBlockBlobResponse.readableStreamBody;
Expand All @@ -84,7 +98,13 @@ export class BlobStorageService {
if (chunk instanceof Buffer) chunks.push(chunk);
else chunks.push(Buffer.from(chunk));
}
return Buffer.concat(chunks).toString('utf-8');
return Buffer.concat(chunks);
}

public async getReadableStream(fileName: string) {
const blockBlobClient = this.containerClient.getBlockBlobClient(fileName);
const downloadBlockBlobResponse = await blockBlobClient.download();
return downloadBlockBlobResponse.readableStreamBody as Readable;
}

public async readFileToBuffer(fileName: string) {
Expand Down
Loading
Loading