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
29 changes: 29 additions & 0 deletions src/controllers/Upload/helpers/handleGoogleDrive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,35 @@ describe('handleGoogleDrive — native Google Apps mime types', () => {
expect(reqFiles[0].originalname).toMatch(/\.pdf$/);
});

it('derives size from the downloaded buffer, not the picker-reported sizeBytes', async () => {
const zeroSizeDoc = { ...baseDocFile, sizeBytes: 0 };
const req = makeReq([zeroSizeDoc]);
const res = makeRes();
const handleUpload = jest.fn();
mockedAxios.get.mockResolvedValue({
data: Buffer.from('<html><body><h1>hello</h1></body></html>'),
} as never);
await handleGoogleDrive(req, res as unknown as express.Response, handleUpload);
const reqFiles = (req as unknown as {
files: { size: number; buffer: Buffer }[];
}).files;
expect(reqFiles[0].size).toBeGreaterThan(0);
expect(Buffer.isBuffer(reqFiles[0].buffer)).toBe(true);
expect(handleUpload).toHaveBeenCalled();
});

it('requests arraybuffer responseType so bodies are not coerced to strings', async () => {
const req = makeReq([baseDocFile]);
const res = makeRes();
const handleUpload = jest.fn();
await handleGoogleDrive(req, res as unknown as express.Response, handleUpload);
expect(mockedAxios.get).toHaveBeenCalledWith(
'google_drive',
expect.any(String),
expect.objectContaining({ responseType: 'arraybuffer' })
);
});

it('returns 400 and does not call handleUpload when googleDriveAuth is missing', async () => {
const req = makeReq([basePdfFile], undefined);
(req.body as Record<string, unknown>).googleDriveAuth = undefined;
Expand Down
9 changes: 5 additions & 4 deletions src/controllers/Upload/helpers/handleGoogleDrive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,21 @@ export async function handleGoogleDrive(
req.files = await Promise.all(
files.map(async (file) => {
const { url, originalname } = resolveUrlAndName(file);
const contents = await instrumentedAxios.get(
const contents = await instrumentedAxios.get<ArrayBuffer>(
'google_drive',
url,
{
headers: {
Authorization: `Bearer ${googleDriveAuth}`,
},
responseType: 'blob',
responseType: 'arraybuffer',
}
);
const buffer = Buffer.from(contents.data);
return {
originalname,
size: file.sizeBytes,
buffer: contents.data,
size: buffer.length,
buffer,
};
})
);
Expand Down