Skip to content
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
7 changes: 3 additions & 4 deletions src/controllers/studentTaskController.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const jwt = require('jsonwebtoken');
const mongoose = require('mongoose');
const EducationTask = require('../models/educationTask');
const { uploadToS3 } = require('../services/s3Service');
const { uploadFileToAzureBlobStorage } = require('../utilities/AzureBlobImages');
const config = require('../config');

const studentTaskController = function () {
Expand Down Expand Up @@ -340,9 +340,8 @@ const studentTaskController = function () {
.json({ message: 'Invalid file type. Only PDF, DOCX, TXT, and images are allowed.' });
}

const key = `tasks/${taskId}/${studentId}/${file.originalname}-${Date.now()}`;
await uploadToS3(file, taskId, studentId, key);
uploadedUrl = `https://${process.env.S3_BUCKET_NAME}.s3.${process.env.AWS_REGION}.amazonaws.com/${key}`;
const blobName = `tasks/${taskId}/${studentId}/${file.originalname}-${Date.now()}`;
uploadedUrl = await uploadFileToAzureBlobStorage(file, blobName);
} else if (req.body.url) {
if (!isValidURL(req.body.url)) {
return res.status(400).json({ error: 'Please enter a valid url' });
Expand Down
24 changes: 24 additions & 0 deletions src/utilities/AzureBlobImages.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,31 @@ const fetchImagesFromAzureBlobStorage = async (imageUrls) => {
return images.filter((img) => img !== null);
};

const uploadFileToAzureBlobStorage = async (file, blobName) => {
if (!file) {
throw new Error('File is required');
}
if (!blobName) {
throw new Error('Blob name is required');
}

const blobServiceClient = BlobServiceClient.fromConnectionString(
process.env.AZURE_STORAGE_CONNECTION_STRING,
);
const containerClient = blobServiceClient.getContainerClient(
process.env.AZURE_STORAGE_CONTAINER_NAME,
);

const blockBlobClient = containerClient.getBlockBlobClient(blobName);
await blockBlobClient.uploadData(file.buffer, {
blobHTTPHeaders: { blobContentType: file.mimetype },
});

return blockBlobClient.url;
};

module.exports = {
saveImagestoAzureBlobStorage,
fetchImagesFromAzureBlobStorage,
uploadFileToAzureBlobStorage,
};
Loading