Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/media-viewer/viewer.tsx
1691 views
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
/*
7
Image viewer component -- for viewing standard image types.
8
*/
9
10
import { filename_extension } from "@cocalc/util/misc";
11
12
import { useState } from "../../app-framework";
13
import { webapp_client } from "../../webapp-client";
14
import { MediaViewerButtonBar } from "./button-bar";
15
import { VIDEO_EXTS, IMAGE_EXTS, AUDIO_EXTS } from "../../file-associations";
16
17
interface Props {
18
project_id: string;
19
path: string;
20
}
21
22
export const MediaViewer: React.FC<Props> = ({ project_id, path }) => {
23
// used to force reload when button explicitly clicked
24
const [param, set_param] = useState<number>(0);
25
26
// the URL to the file:
27
let url = webapp_client.project_client.read_file({
28
project_id,
29
path,
30
});
31
if (param) {
32
url += `?param=${param}`; // this forces reload whenever refresh button clicked
33
}
34
35
return (
36
<div style={{ marginTop: "1px" }} className={"smc-vfill"}>
37
<MediaViewerButtonBar refresh={() => set_param(Math.random())} />
38
<div
39
style={{
40
overflowY: "auto",
41
flex: 1,
42
marginTop: "1px",
43
padding: "1px",
44
borderTop: "1px solid lightgray",
45
textAlign: "center",
46
background: "black",
47
}}
48
>
49
<RenderMedia url={url} path={path} />
50
</div>
51
</div>
52
);
53
};
54
55
function get_mode(path: string): string {
56
const ext = filename_extension(path).toLowerCase();
57
if (VIDEO_EXTS.includes(ext)) {
58
return "video";
59
}
60
if (IMAGE_EXTS.includes(ext)) {
61
return "image";
62
}
63
if (AUDIO_EXTS.includes(ext)) {
64
return "audio";
65
}
66
console.warn(`Unknown media extension ${ext}`);
67
return "";
68
}
69
70
const RenderMedia: React.FC<{ path: string; url: string }> = ({
71
path,
72
url,
73
}) => {
74
switch (get_mode(path)) {
75
case "image":
76
return (
77
<img src={url} style={{ maxWidth: "100%", background: "white" }} />
78
);
79
case "video":
80
return (
81
<video
82
src={url}
83
style={{ maxWidth: "100%" }}
84
controls={true}
85
autoPlay={true}
86
loop={true}
87
/>
88
);
89
case "audio":
90
return <audio src={url} autoPlay={true} controls={true} loop={false} />;
91
default:
92
// should never happen
93
return (
94
<div style={{ color: "white", fontSize: "200%" }}>Unknown type</div>
95
);
96
}
97
};
98
99