Skip to content

Commit

Permalink
fix file size issue (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdevalapurkar authored May 13, 2021
1 parent 1f6a2ff commit 2fb8012
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
16 changes: 15 additions & 1 deletion app/src/components/attachments/AttachmentsList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ describe('AttachmentsList', () => {
fileName: 'filename.test',
lastModified: '2021-04-09 11:53:53',
size: 3028
},
{
id: 20,
fileName: 'filename20.test',
lastModified: '2021-04-09 11:53:53',
size: 30280000
},
{
id: 30,
fileName: 'filename30.test',
lastModified: '2021-04-09 11:53:53',
size: 30280000000
}
];

Expand All @@ -39,12 +51,14 @@ describe('AttachmentsList', () => {
expect(getByText('No Attachments')).toBeInTheDocument();
});

it('renders correctly with attachments', async () => {
it('renders correctly with attachments (of various sizes)', async () => {
const { getByText } = render(
<AttachmentsList projectId={1} attachmentsList={attachmentsList} getAttachments={jest.fn()} />
);

expect(getByText('filename.test')).toBeInTheDocument();
expect(getByText('filename20.test')).toBeInTheDocument();
expect(getByText('filename30.test')).toBeInTheDocument();
});

it('viewing file contents in new tab works as expected', async () => {
Expand Down
17 changes: 16 additions & 1 deletion app/src/components/attachments/AttachmentsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ const AttachmentsList: React.FC<IAttachmentsListProps> = (props) => {
}
};

const getFormattedFileSize = (fileSize: number) => {
// kilobyte size
if (fileSize < 1000000) {
return `${(fileSize / 1000).toFixed(1)} KB`;
}

// megabyte size
if (fileSize < 1000000000) {
return `${(fileSize / 1000000).toFixed(1)} MB`;
}

// gigabyte size
return `${(fileSize / 1000000000).toFixed(1)} GB`;
};

return (
<>
<YesNoDialog
Expand Down Expand Up @@ -118,7 +133,7 @@ const AttachmentsList: React.FC<IAttachmentsListProps> = (props) => {
</Link>
</TableCell>
<TableCell>{getFormattedDate(DATE_FORMAT.ShortDateFormatMonthFirst, row.lastModified)}</TableCell>
<TableCell>{row.size / 1000000} MB</TableCell>
<TableCell>{getFormattedFileSize(row.size)}</TableCell>
<TableCell align="right" className={clsx(index === 0 && classes.tableCellBorderTop)}>
<IconButton
color="primary"
Expand Down

0 comments on commit 2fb8012

Please sign in to comment.