Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ui-storagebrowser] fixes the preview for non-text files #3981

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ import {
isOFSRoot,
inTrash
} from '../../../../utils/storageBrowserUtils';
import { SUPPORTED_COMPRESSED_FILE_EXTENTION } from '../../../../utils/constants/storageBrowser';
import { SupportedFileTypes } from '../../../../utils/constants/storageBrowser';
import { TFunction } from 'i18next';
import { getFileType } from '../../StorageFilePage/StorageFilePage.util';

export enum ActionType {
Copy = 'copy',
Expand All @@ -61,7 +62,7 @@ const isValidFileOrFolder = (filePath: string): boolean => {
};

const isFileCompressed = (filePath: string): boolean => {
return SUPPORTED_COMPRESSED_FILE_EXTENTION.some(ext => filePath.endsWith(ext));
return getFileType(filePath) === SupportedFileTypes.COMPRESSED;
};

const isActionEnabled = (file: StorageDirectoryTableData, action: ActionType): boolean => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
height: 90%;
}

.preview__unsupported {
.preview__compressed {
font-size: vars.$font-size-lg;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,25 @@ describe('StorageFilePage', () => {
expect(screen.queryByRole('link', { name: 'Download' })).toBeNull();
});

// TODO: fix this test when mocking of useLoadData onSuccess callback is mproperly mocked
it.skip('should render a textarea for text files', () => {
// TODO: fix this test when mocking of useLoadData onSuccess callback is properly mocked
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we mock this now?

it('should render a textarea for text files', () => {
render(
<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} onReload={mockReload} />
);

const textarea = screen.getByRole('textbox');
expect(textarea).toBeInTheDocument();
expect(textarea).toHaveValue('Initial file content');
// expect(textarea).toHaveValue('Initial file content');
});

it('should render a textarea for other files', () => {
render(
<StorageFilePage fileName={'dockerfile'} fileStats={mockFileStats} onReload={mockReload} />
);

const textarea = screen.getByRole('textbox');
expect(textarea).toBeInTheDocument();
// expect(textarea).toHaveValue('Initial file content');
});

it('should render an image for image files', () => {
Expand Down Expand Up @@ -295,18 +305,18 @@ describe('StorageFilePage', () => {
expect(video.children[0]).toHaveAttribute('src', expect.stringContaining('videofile.mp4'));
});

it('should display a message for unsupported file types', () => {
it('should display a message for compresed file types', () => {
ramprasadagarwal marked this conversation as resolved.
Show resolved Hide resolved
render(
<StorageFilePage
fileStats={{
...mockFileStats,
path: '/path/to/unsupportedfile.xyz'
path: '/path/to/compressed.zip'
}}
fileName="unsupportedfile.xyz"
fileName="compressed.zip"
onReload={mockReload}
/>
);

expect(screen.getByText(/preview not available for this file/i)).toBeInTheDocument();
expect(screen.getByText(/preview not available for compressed file/i)).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import Pagination from '../../../reactComponents/Pagination/Pagination';
import {
DEFAULT_PREVIEW_PAGE_SIZE,
EDITABLE_FILE_FORMATS,
SUPPORTED_FILE_EXTENSIONS,
SupportedFileTypes
} from '../../../utils/constants/storageBrowser';
import useLoadData from '../../../utils/hooks/useLoadData/useLoadData';
Expand Down Expand Up @@ -207,7 +206,7 @@ const StorageFilePage = ({ fileName, fileStats, onReload }: StorageFilePageProps
</div>

<div className="preview__content">
{fileType === SupportedFileTypes.TEXT && (
{[SupportedFileTypes.TEXT, SupportedFileTypes.OTHER].includes(fileType) && (
<div className="preview__editable-file">
<textarea
value={fileContent}
Expand Down Expand Up @@ -252,12 +251,11 @@ const StorageFilePage = ({ fileName, fileStats, onReload }: StorageFilePageProps
</video>
)}

{fileType === SupportedFileTypes.OTHER && (
<div className="preview__unsupported">
{t('Preview not available for this file. Please download the file to view.')}
<br />
{t(`Supported file extensions:
${Object.keys(SUPPORTED_FILE_EXTENSIONS).join(', ')}`)}
{fileType === SupportedFileTypes.COMPRESSED && (
<div className="preview__compresed">
{t(
'Preview not available for compressed file. Please download the file to view.'
)}
</div>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ export const getFileMetaData = (t: TFunction, fileStats: FileStats): MetaData[][
};

export const getFileType = (fileName: string): SupportedFileTypes => {
const fileExtension = fileName?.split('.')?.pop()?.toLowerCase();
if (!fileExtension) {
return SupportedFileTypes.OTHER;
for (const fileExtension in SUPPORTED_FILE_EXTENSIONS) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove the split on "." lower case transformation? This seems less robust given that this function still receives the complete file name.

A file called "testpng" and that is lacking a proper file type will now be assumed to be a png file just because it has it in the name at the end. Also "test.PNG" which should be matched won't be matched.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The older logic failed to handle file extensions with multiple dots (e.g., .tar.gz), as it only considered the part after the last dot, causing misidentification.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have also updated the logic to accomodate the testpng and test.PNG test case

if (fileName.endsWith(fileExtension)) {
return SUPPORTED_FILE_EXTENSIONS[fileExtension];
}
}
return SUPPORTED_FILE_EXTENSIONS[fileExtension] ?? SupportedFileTypes.OTHER;
return SupportedFileTypes.OTHER;
};
13 changes: 9 additions & 4 deletions desktop/core/src/desktop/js/utils/constants/storageBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export enum SupportedFileTypes {
DOCUMENT = 'document',
AUDIO = 'audio',
VIDEO = 'video',
COMPRESSED = 'compressed',
OTHER = 'other'
}

Expand Down Expand Up @@ -59,9 +60,13 @@ export const SUPPORTED_FILE_EXTENSIONS: Record<string, SupportedFileTypes> = {

mp3: SupportedFileTypes.AUDIO,

mp4: SupportedFileTypes.VIDEO
};
mp4: SupportedFileTypes.VIDEO,

export const EDITABLE_FILE_FORMATS = new Set([SupportedFileTypes.TEXT]);
zip: SupportedFileTypes.COMPRESSED,
'tar.gz': SupportedFileTypes.COMPRESSED,
tgz: SupportedFileTypes.COMPRESSED,
bz2: SupportedFileTypes.COMPRESSED,
bzip: SupportedFileTypes.COMPRESSED
};

export const SUPPORTED_COMPRESSED_FILE_EXTENTION = ['zip', 'tar.gz', 'tgz', 'bz2', 'bzip'];
export const EDITABLE_FILE_FORMATS = new Set([SupportedFileTypes.TEXT, SupportedFileTypes.OTHER]);