Skip to content
Open
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
68 changes: 22 additions & 46 deletions api/helpers/blob.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,43 @@
const { BlobServiceClient } = require('@azure/storage-blob');
const uuidv1 = require('uuid/v1');
const azure = require('azure-storage');
const blobService = azure.createBlobService(

const blobServiceClient = BlobServiceClient.fromConnectionString(
process.env.AZURE_STORAGE_CONNECTION_STRING
);

module.exports = {
createBlockBlobFromText
createBlockBlobFromText,
};

function createContainerIfNotExists(shareName) {
return new Promise((resolve, reject) => {
blobService.createContainerIfNotExists(shareName, function(error, result) {
if (!error) {
resolve(result);
} else {
reject(error);
}
});
});
async function createContainerIfNotExists(containerName) {
const containerClient = blobServiceClient.getContainerClient(containerName);
const createResponse = await containerClient.createIfNotExists();
return createResponse.succeeded;
}

// Returns [file:BlobResult, fileUrl:string]
async function createBlockBlobFromText(
containerName,
fileName,
file,
prefix = ''
) {
// Returns [file:BlockBlobClient, fileUrl:string]
async function createBlockBlobFromText(containerName, fileName, file, prefix = '') {
await createContainerIfNotExists(containerName);

const { buffer, mimetype } = file;

const ts = Math.round(new Date().getTime() / 1000);
const uuidSuffix = uuidv1().split('-').pop();
const finalName = `${prefix}_${ts}_${uuidSuffix}_${fileName.toLowerCase().trim()}`;

const containerClient = blobServiceClient.getContainerClient(containerName);
const blockBlobClient = containerClient.getBlockBlobClient(finalName);

const options = {};
if (mimetype && mimetype.length) {
const cacheMaxAgeInSeconds = 31536000;
options.contentSettings = {
contentType: mimetype,
cacheControl: `max-age=${cacheMaxAgeInSeconds}`
options.blobHTTPHeaders = {
blobContentType: mimetype,
blobCacheControl: `max-age=${cacheMaxAgeInSeconds}`,
};
}

const ts = Math.round(new Date().getTime() / 1000);
const uuidSuffix = uuidv1()
.split('-')
.pop();
const finalName = `${prefix}_${ts}_${uuidSuffix}_${fileName
.toLowerCase()
.trim()}`;

return new Promise((resolve, reject) => {
blobService.createBlockBlobFromText(
containerName,
finalName,
buffer,
options,
function(error, file) {
if (error) {
reject(error);
return;
}
await blockBlobClient.upload(buffer, buffer.length, options);

resolve([file, blobService.getUrl(file.container, file.name)]);
}
);
});
return [blockBlobClient, blockBlobClient.url];
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
],
"main": "app.js",
"dependencies": {
"@azure/storage-blob": "^12.27.0",
"applicationinsights": "^2.7.3",
"axios": "^1.6.4",
"azure-storage": "^2.10.6",
"bcryptjs": "^2.4.3",
"body-parser": "1.20.3",
"connect-mongo": "^3.2.0",
Expand Down
Loading