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