Skip to content

Commit 7de5ab6

Browse files
authored
feat: add support for additional file types from LibreTranslate issue #18 (#19)
1 parent 7b5fc69 commit 7de5ab6

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

src/translate/Source.tsx

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,30 @@ const IconContainer = styled.div`
1313
z-index: 1;
1414
`;
1515

16+
const supportedTextExtensions = ['txt', 'nfo', 'html', 'htm', 'xml', 'xhtml', 'md', 'srt'];
17+
const supportedBinaryExtensions = ['odt', 'docx', 'pptx', 'epub'];
18+
const allSupportedExtensions = [...supportedTextExtensions, ...supportedBinaryExtensions];
19+
1620
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
1721
const file = event?.target?.files && event.target.files[0];
1822
if (file) {
19-
const reader = new FileReader();
20-
reader.onload = (e) => {
21-
//@ts-expect-error: result exists
22-
actions.setQuestion(e.target.result);
23-
snackActions.showSnack("File succesully read");
24-
};
25-
reader.readAsText(file);
23+
const fileExtension = file.name.split('.').pop()?.toLowerCase();
24+
const isTextFormat = supportedTextExtensions.includes(fileExtension || '');
25+
const isBinaryFormat = supportedBinaryExtensions.includes(fileExtension || '');
26+
27+
if (isTextFormat) {
28+
const reader = new FileReader();
29+
reader.onload = (e) => {
30+
//@ts-expect-error: result exists
31+
actions.setQuestion(e.target.result);
32+
snackActions.showSnack("File successfully read");
33+
};
34+
reader.readAsText(file);
35+
} else if (isBinaryFormat) {
36+
snackActions.showSnack(`File format ${fileExtension} requires server-side processing`);
37+
} else {
38+
snackActions.showSnack("Unsupported file format");
39+
}
2640
}
2741
};
2842

@@ -64,7 +78,10 @@ export const Source = ({
6478
title={
6579
<span style={{ textAlign: "center" }}>
6680
Upload file. <br /> supported file types:
67-
.txt,.nfo,.html,.htm,.xml,.xhtml,.md
81+
{allSupportedExtensions.map(ext => `.${ext}`).join(', ')}<br />
82+
<span style={{ fontSize: '0.8em', color: '#666' }}>
83+
{supportedBinaryExtensions.map(ext => `.${ext}`).join(', ')} require server-side processing
84+
</span>
6885
</span>
6986
}
7087
>
@@ -78,7 +95,7 @@ export const Source = ({
7895
<FileUploadOutlinedIcon fontSize="inherit" />
7996
<VisuallyHiddenInput
8097
type="file"
81-
accept=".txt,.nfo,.html,.htm,.xml,.xhtml,.md"
98+
accept={allSupportedExtensions.map(ext => `.${ext}`).join(',')}
8299
onChange={handleFileChange}
83100
multiple={false}
84101
/>

0 commit comments

Comments
 (0)