-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileViewer.js
More file actions
165 lines (146 loc) · 5.49 KB
/
FileViewer.js
File metadata and controls
165 lines (146 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import React, {useEffect, useState} from 'react';
import {getFileType, encodeBase64UnicodeCustom} from '../../Helpers/Path';
import ImageViewer from './ImageViewer';
import TextViewer from './TextViewer';
import MediaViewer from './MediaViewer';
import PdfViewer from './PdfViewer';
import Loading from '../Loading/Loading';
import FileActionsDropdown from '../FSItem/FileActionsDropdown';
import deleteFileSystemItem from '../../Helpers/deleteFileSystemItem';
import API from '../../Helpers/API';
import './FileViewer.css';
export default function ({path, theme, onFileInfoLoaded, hideOpenFileLinkAction, onClose}) {
const [state] = useState({isUnmounted: false, loadIndex: 0});
const [error, setError] = useState(null);
const [contentError, setContentError] = useState(null);
const [isLoading, setIsLoading] = useState(false);
const [file, setFile] = useState(null);
const setViewerError = (viewerError) => {
if (!state.isUnmounted) setContentError(viewerError);
}
const updateFile = async () => {
const currentIndex = ++state.loadIndex;
let newFile = null;
let newError = false;
if (path) {
try {
setIsLoading(true);
const response = await API.getFileInfo(path);
if (response.ok) {
newFile = await response.json();
newFile.isFile = true;
} else if (response.status === 403) {
newError = 'Forbidden to access file';
} else if (response.status === 404) {
newError = 'File not found';
} else if (response.status === 401) {
newError = 'Unauthorized. It seems like your are not logged in';
} else {
newError = 'An error occured';
}
} catch (e) {
console.log(e);
newError = e.message;
}
}
if (currentIndex === state.loadIndex && !state.isUnmounted) {
onFileInfoLoaded && onFileInfoLoaded(newFile);
setError(newError);
setFile(newFile);
setIsLoading(false);
}
}
const renderContentError = (text) => {
return (
<label className={`file-viewer-content-error ${theme}`}>
{text}
</label>
);
}
const renderViewer = () => {
if (!file.permission.read) {
return (
<div className="text-center'">
<h3 className={`font-italic file-viewer-info-text ${theme}`}>
Not authorized for reading
</h3>
</div>
);
}
switch (getFileType(file.extension)) {
case 'image':
return <ImageViewer path={file.path} onError={setViewerError}/>;
case 'text':
return file.size <= 500 * 1024 ? (
<TextViewer path={file.path} onError={setViewerError}/>
) : renderContentError('File to big (> 500kB)');
case 'audio':
return <MediaViewer path={file.path} type="audio" onError={setViewerError}/>;
case 'video':
return <MediaViewer path={file.path} type="video" onError={setViewerError}/>;
case 'pdf':
return file.size <= 100 * 1024 * 1024 ? (
<PdfViewer path={file.path} onError={setViewerError}/>
) : renderContentError('File to big (> 100MB)');
}
return (
<div className="text-center">
<h3 className={`font-italic file-viewer-info-text ${theme}`}>
No preview for this file available
</h3>
</div>
);
};
useEffect(() => {
return () => {
state.isUnmounted = true;
};
}, []);
useEffect(() => {
updateFile();
}, [path]);
if (error) {
return (
<div className="text-center'">
<h3 className={`font-italic file-viewer-info-text ${theme}`}>
{error}
</h3>
</div>
);
}
if (isLoading) {
return (
<div className="center">
<Loading/>
</div>
);
}
if (!file) {
return null;
}
return (
<div className="file-viewer-container">
<div className="file-viewer-header-container">
<div style={{flexGrow: 1}}/>
<h4 className={`file-viewer-title ${theme}`}>{file.name}</h4>
<div style={{flexGrow: 1}}/>
<div className="m-1">
<FileActionsDropdown file={file}
title="Options"
hideOpenFileLink={hideOpenFileLinkAction}
onDelete={() => deleteFileSystemItem(
file,
onClose,
)}/>
</div>
</div>
<div className="file-viewer-content-remaining">
<div className="file-viewer-content-container">
<div className="file-viewer-content">
{contentError ? renderContentError(contentError) : renderViewer()}
</div>
</div>
</div>
</div>
);
}