1
|
|
|
import React, {useEffect, useState} from "react"; |
2
|
|
|
import "./MediaShowcase.css"; |
3
|
|
|
|
4
|
|
|
const imageSizeLimit = 10 * 1000 * 1000; // 10MB |
5
|
|
|
const videoSizeLimit = 100 * 1000 * 1000; // 100MB |
6
|
|
|
|
7
|
|
|
function MediaShowcase(props: { file: File }): JSX.Element { |
8
|
|
|
const [fileContent, setFileContent] = useState<string>(""); |
9
|
|
|
|
10
|
|
|
useEffect(() => { |
11
|
|
|
getFileContent(); |
12
|
|
|
}, [props.file]); |
13
|
|
|
|
14
|
|
|
const getFileContent = () => { |
15
|
|
|
if ((props.file.type.includes("image") && props.file.size <= imageSizeLimit) || |
16
|
|
|
(props.file.type.includes("video") && props.file.size <= videoSizeLimit)) { |
17
|
|
|
const reader = new FileReader(); |
18
|
|
|
reader.onload = function (event) { |
19
|
|
|
if (event.target?.result) { |
20
|
|
|
setFileContent(event.target.result.toString()); |
21
|
|
|
} |
22
|
|
|
}; |
23
|
|
|
reader.readAsDataURL(props.file); |
24
|
|
|
} |
25
|
|
|
}; |
26
|
|
|
|
27
|
|
|
if (props.file.type.includes("image")) { |
28
|
|
|
if (props.file.size < imageSizeLimit) { |
29
|
|
|
return ( |
30
|
|
|
<React.Fragment> |
31
|
|
|
{fileContent != "" && <img className="selected-media" src={fileContent} alt="selected media"/>} |
32
|
|
|
{!fileContent && <h1>Loading...</h1>} |
33
|
|
|
</React.Fragment> |
34
|
|
|
); |
35
|
|
|
} else { |
36
|
|
|
return <h1>File too large</h1>; |
37
|
|
|
} |
38
|
|
|
} else if (props.file.type.includes("video")) { |
39
|
|
|
if (props.file.size < videoSizeLimit) { |
40
|
|
|
return ( |
41
|
|
|
<React.Fragment> |
42
|
|
|
{props.file.type.includes("video") && fileContent && |
43
|
|
|
<video className="selected-media" src={fileContent} autoPlay muted loop/>} |
44
|
|
|
{!fileContent && <h1>Loading...</h1>} |
45
|
|
|
</React.Fragment> |
46
|
|
|
); |
47
|
|
|
} else { |
48
|
|
|
return <h1>File too large</h1>; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
return <h1>Loading...</h1>; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
export default MediaShowcase; |
55
|
|
|
|