import { ArrowLeftOutlined, ArrowRightOutlined, CopyOutlined, CloudDownloadOutlined, DeleteOutlined, DownloadOutlined, EditOutlined, EllipsisOutlined, LinkOutlined, MenuFoldOutlined, MenuUnfoldOutlined, ShareAltOutlined } from '@ant-design/icons' import { Button, Descriptions, Divider, Dropdown, Input, Layout, Menu, message, notification, Space, Tag, Typography } from 'antd' import * as clipboardy from 'clipboardy' import moment from 'moment' import prettyBytes from 'pretty-bytes' import React, { useEffect, useState } from 'react' import ReactPlayer from 'react-player' import { useHistory } from 'react-router' import useSWR from 'swr' import useSWRImmutable from 'swr/immutable' import { useDebounce } from 'use-debounce/lib' import { directDownload } from '../../../utils/Download' import { fetcher } from '../../../utils/Fetcher' import Remove from '../../dashboard/components/Remove' import Rename from '../../dashboard/components/Rename' import Share from '../../dashboard/components/Share' import Icon from './Icon' interface Props { me: any, data: any, error: any, mutate: () => void, pageParams: { id: string }, isInDrawer?: boolean, onCloseDrawer?: () => void } const Viewer: React.FC = ({ data, me, error, mutate, pageParams, isInDrawer, onCloseDrawer }) => { const history = useHistory() const [collapsed, setCollapsed] = useState() const { data: user } = useSWRImmutable(data?.file ? `/users/${data.file.user_id}` : null, fetcher) const { data: datafilesParts } = useSWR(data?.file.name && /\.part0*\d+$/.test(data.file.name) ? `/files?name.like=${data.file.name.replace(/\.part0*\d+$/, '')}&user_id=${data.file.user_id}${me?.user.id !== data.file.user_id ? '&shared=1' : ''}&parent_id${data.file.parent_id ? `=${data.file.parent_id}` : '=null'}` : null, fetcher) const [links, setLinks] = useState<{ raw: string, download: string, share: string }>() const [showContent] = useDebounce(collapsed, 250) const [contentStyle, setContentStyle] = useState<{ display: string } | undefined>() const [selectShare, setSelectShare] = useState() const [fileRename, setFileRename] = useState() const [selectDeleted, setSelectDeleted] = useState() useEffect(() => { if (data?.file) { setLinks({ raw: `${process.env.REACT_APP_API_URL || window.location.origin}/api/v1/files/${pageParams.id}?raw=1&password=${sessionStorage.getItem(`pass-${pageParams.id}`)}`, download: `${process.env.REACT_APP_API_URL || window.location.origin}/api/v1/files/${pageParams.id}?raw=1&dl=1&password=${sessionStorage.getItem(`pass-${pageParams.id}`)}`, share: `${window.location.origin}/view/${pageParams.id}` }) // setBlobURL(undefined) // download(pageParams.id).then(stream => { // const video = document.querySelector('video') // const mime = 'video/mp4; codecs="mp4a.40.2,avc1.64001f"' // const mediaSource = new MediaSource() // if (video) { // setBlobURL(URL.createObjectURL(mediaSource)) // // video.src = URL.createObjectURL(mediaSource) // mediaSource.addEventListener('sourceopen', async () => { // // URL.revokeObjectURL(video.src) // // mediaSource.duration = 8 // // const buffer = mediaSource.addSourceBuffer(mime) // const buffer = mediaSource.addSourceBuffer(mime) // buffer.mode = 'sequence' // buffer.addEventListener('updateend', () => { // // buffer.remove(0, 0) // if (!buffer.updating && mediaSource.readyState === 'open') { // mediaSource.endOfStream() // } // // video.play() // }) // buffer.addEventListener('error', console.error) // const reader = stream.getReader() // let isDone = false // while (!isDone) { // console.log(mediaSource.readyState) // const { done, value } = await reader.read() // if (done) { // isDone = done // break // } // console.log(mediaSource.readyState, value) // buffer.appendBuffer(value) // } // }) // } // }) // download(pageParams.id).then(async stream => { // console.log(new Date()) // const resp = new Response(stream, { // headers: { // 'Content-Type': data?.file.mime_type, // 'Content-Disposition': `inline; filename="${data?.file.name}"`, // 'Cache-Control': 'public, max-age=604800', // 'ETag': Buffer.from(data?.file.id).toString('base64') // } // }) // resp.blob().then(blob => { // console.log(new Date()) // const url = URL.createObjectURL(blob) // setBlobURL(url) // }) // }) } }, [data]) useEffect(() => { if (collapsed) { setContentStyle({ display: 'none' }) } }, [collapsed]) useEffect(() => { if (!showContent) { setContentStyle(undefined) } }, [showContent]) useEffect(() => { if (error?.status === 402) { notification.error({ message: 'Premium Feature', description: error.data?.error || 'Upgrade your plan to view this file', }) return history.replace('/pricing') } }, [error]) const copy = (val: string) => { clipboardy.write(val) return message.info('Copied!') } const back = () => { // if (errorMe) { // return history.push('/login') // } // setBlobURL(undefined) if (isInDrawer) return onCloseDrawer?.() window.close() return history.goBack() } return <> {data?.file.type === 'image' ? : data?.file.type === 'video' ? : } {/* !blobURL ? */} {/* {data?.file.type === 'video' ? : } */}   {data?.file.name.replace(/\.part0*\d+$/, '')}} contentStyle={{ color: '#fff' }} labelStyle={{ color: '#fff' }} column={1}> {datafilesParts?.length ? prettyBytes(datafilesParts?.files.reduce((res: number, file: any) => res + Number(file.size), 0)) + ` (${datafilesParts?.length} parts)` : data?.file.size && prettyBytes(Number(data?.file.size || 0))} {moment(data?.file.uploaded_at).local().format('lll')} {user?.user && @{user?.user.username} }   Raw URL}> } value={links?.raw} onSearch={copy} />   Download URL}> } value={links?.download} onSearch={copy} /> {data?.file.sharing_options?.length &&   Share URL}> } value={links?.share} onSearch={copy} /> }
{!showContent &&
{ if (me?.user.id === data?.file.user_id) { return history.replace(`/dashboard${data?.file.parent_id ? `?parent=${data?.file.parent_id}` : ''}`) } else { return history.replace('/dashboard/shared') } }} /> } export default Viewer