Skip to content

Commit

Permalink
#56 find file extension and properly name the files to be uploaded; f…
Browse files Browse the repository at this point in the history
…ix few tests to match the new donated item schema
  • Loading branch information
anjaliputta committed Oct 27, 2024
1 parent 89c998a commit 6b2cf78
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"express": "^4.20.0",
"joi": "^17.13.3",
"morgan": "^1.10.0",
"multer": "^1.4.5-lts.1",
"portfinder": "^1.0.32",
"smcloudstore": "^0.2.1"
},
Expand Down
8 changes: 4 additions & 4 deletions server/src/__mocks__/mockDonatedItem.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
export const updateItem = {
itemType: 'Updated Book',
currentStatus: 'Received',
programId: 1,
donorId: 1,
programId: '1',
donorId: '1',
dateDonated: new Date().toISOString()
};
export const newItem = {
itemType: 'Book',
currentStatus: 'Received',
programId: 1,
donorId: 1,
programId: '1',
donorId: '1',
dateDonated: new Date().toISOString()
};
8 changes: 4 additions & 4 deletions server/src/__tests__/donatedItem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ describe('DonatedItem API Tests', () => {
});

it('handles errors when the provided Program or Donor does not exist', async () => {
const programId = 99;
const donorId = 29;
const programId = '99';
const donorId = '29';
mockPrismaClient.program.findUnique.mockResolvedValue(null);
mockPrismaClient.donor.findUnique.mockResolvedValue(null);

Expand All @@ -57,8 +57,8 @@ describe('DonatedItem API Tests', () => {
mockPrismaClient.program.findUnique.mockResolvedValue(null);
mockPrismaClient.donor.findUnique.mockResolvedValue(null);

const donorId = 19;
const programId = 99;
const donorId = '19';
const programId = '99';

const response = await request(app).put('/donatedItem/details/1').send({
...updateItem,
Expand Down
9 changes: 5 additions & 4 deletions server/src/routes/donatedItemRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import prisma from '../prismaClient'; // Import Prisma client
import { donatedItemValidator } from '../validators/donatedItemValidator'; // Import the validator
import { validateDonor } from '../services/donorService';
import { validateProgram } from '../services/programService';
import { uploadToAzure } from '../services/donatedItemService';
import { uploadToStorage, getFileExtension } from '../services/donatedItemService';

const router = Router();
const upload = multer({ storage: multer.memoryStorage() });
Expand Down Expand Up @@ -38,10 +38,11 @@ router.post('/', [upload.array('imageFiles'), donatedItemValidator], async (req:
},
});

// upload images to Azure and get their filenames
const imageUrls = await Promise.all(imageFiles.map((file, index) => {
// Upload images to cloud storage and get their filenames
const imageUrls = await Promise.all(imageFiles.map(async (file) => {
const fileExtension = getFileExtension(file.mimetype);
const formattedDate = new Date().toISOString();
return uploadToAzure(file, `item-${formattedDate}-${newItem.id}.jpg`);
return uploadToStorage(file, `item-${formattedDate}-${newItem.id}${fileExtension}`);
}));

const newStatus = await prisma.donatedItemStatus.create({
Expand Down
16 changes: 15 additions & 1 deletion server/src/services/donatedItemService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import multer from 'multer';
import { storage } from "../configs/SMCloudStoreConfig";

export async function uploadToAzure(file: Express.Multer.File, filename: string): Promise<string> {
export async function uploadToStorage(file: Express.Multer.File, filename: string): Promise<string> {
const containerName = 'mdma-dev';
await storage.putObject(containerName, filename, file.buffer, {
'Content-Type': file.mimetype
});
return `${containerName}/${filename}`;
}

// extract file extension from MIME type
export function getFileExtension(mimeType: string) {
switch (mimeType) {
case 'image/jpeg':
return '.jpeg';
case 'image/png':
return '.png';
case 'image/gif':
return '.gif';
default:
return '.jpg';
}
}

0 comments on commit 6b2cf78

Please sign in to comment.