Skip to content

Commit 6a5a010

Browse files
authored
Merge pull request #369 from avinxshKD/fix/file-browser-bonus
fix: address file browser bonus issues
2 parents fcf221b + 5abb159 commit 6a5a010

3 files changed

Lines changed: 32 additions & 4 deletions

File tree

DEV-GUIDE.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Dev Guide
2+
3+
## File size limit used by file browser
4+
5+
The file open size limit is configured in [src/toolbarActions/toolbarFunctions.js](src/toolbarActions/toolbarFunctions.js).
6+
7+
- `MAX_FILE_SIZE_MB` controls the limit in MB.
8+
- `MAX_FILE_SIZE` is derived from it as bytes.
9+
10+
To change the limit, update `MAX_FILE_SIZE_MB` only.

src/component/fileBrowser.jsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ const LocalFileBrowser = ({ superState, dispatcher }) => {
2020
const [dirButton, setDirButton] = useState(false);
2121
const [fileState, setFileState] = useState([]);
2222

23+
const getLocalFileState = (state) => state.map((file) => ({
24+
key: file.key,
25+
modified: file.modified,
26+
size: file.size,
27+
fileName: file.fileObj ? file.fileObj.name : null,
28+
}));
29+
2330
useEffect(() => {
2431
if ('showDirectoryPicker' in window) {
2532
setDirButton(true);
@@ -35,7 +42,7 @@ const LocalFileBrowser = ({ superState, dispatcher }) => {
3542
// setFileState({ files: allFiles });
3643
// }
3744
try {
38-
window.localStorage.setItem('fileList', JSON.stringify(fileState));
45+
window.localStorage.setItem('fileList', JSON.stringify(getLocalFileState(fileState)));
3946
} catch (e) {
4047
toast.error(e.message);
4148
}
@@ -47,13 +54,14 @@ const LocalFileBrowser = ({ superState, dispatcher }) => {
4754

4855
const handleSelectFile = (data) => {
4956
const fileExtensions = ['jpeg', 'jpg', 'png', 'exe'];
50-
if (fileExtensions.includes(data.fileObj.name.split('.').pop())) {
57+
const fileExt = data.fileObj.name.split('.').pop().toLowerCase();
58+
if (fileExtensions.includes(fileExt)) {
5159
// eslint-disable-next-line no-alert
5260
alert('Wrong file extension');
5361
return;
5462
}
5563

56-
if (data.fileObj.name.split('.').pop() === 'graphml') {
64+
if (fileExt === 'graphml') {
5765
let foundi = -1;
5866
superState.graphs.forEach((g, i) => {
5967
if ((g.fileName === data.fileObj.name)) {
@@ -251,7 +259,7 @@ const LocalFileBrowser = ({ superState, dispatcher }) => {
251259

252260
setFileState(filesArray);
253261
try {
254-
window.localStorage.setItem('fileList', JSON.stringify(filesArray));
262+
window.localStorage.setItem('fileList', JSON.stringify(getLocalFileState(filesArray)));
255263
} catch (e) {
256264
toast.error(e.message);
257265
}

src/toolbarActions/toolbarFunctions.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import parser from '../graph-builder/graphml/parser';
33
import { actionType as T } from '../reducer';
44

55
const getGraphFun = (superState) => superState.curGraphInstance;
6+
const MAX_FILE_SIZE_MB = 10;
7+
const MAX_FILE_SIZE = MAX_FILE_SIZE_MB * 1024 * 1024;
68

79
const createNode = (state, setState) => {
810
setState({
@@ -106,6 +108,10 @@ async function saveGraphMLFile(state) {
106108

107109
const readFile = async (state, setState, file, fileHandle) => {
108110
if (file) {
111+
if (file.size > MAX_FILE_SIZE) {
112+
toast.error(`File size exceeds ${MAX_FILE_SIZE_MB}MB`);
113+
return;
114+
}
109115
const fr = new FileReader();
110116
const projectName = file.name;
111117
if (file.name.split('.').pop() === 'graphml') {
@@ -127,6 +133,10 @@ const readFile = async (state, setState, file, fileHandle) => {
127133

128134
const readTextFile = (state, setState, file, fileHandle) => {
129135
if (file) {
136+
if (file.size > MAX_FILE_SIZE) {
137+
toast.error(`File size exceeds ${MAX_FILE_SIZE_MB}MB`);
138+
return;
139+
}
130140
setState({
131141
type: T.EDIT_TEXTFILE,
132142
payload: { show: true, fileObj: file, fileHandle },

0 commit comments

Comments
 (0)