// @flow import type { Node } from 'react'; import * as ICONS from 'constants/icons'; import React, { useState, useEffect } from 'react'; import { regexInvalidURI } from 'util/lbryURI'; import PostEditor from 'component/postEditor'; import FileSelector from 'component/common/file-selector'; import Button from 'component/button'; import Card from 'component/common/card'; import { FormField } from 'component/common/form'; import Spinner from 'component/spinner'; import I18nMessage from 'component/i18nMessage'; import usePersistedState from 'effects/use-persisted-state'; import * as PUBLISH_MODES from 'constants/publish_types'; import PublishName from 'component/publishName'; type Props = { uri: ?string, mode: ?string, name: ?string, title: ?string, filePath: string | WebFile, fileMimeType: ?string, isStillEditing: boolean, balance: number, updatePublishForm: ({}) => void, disabled: boolean, publishing: boolean, showToast: (string) => void, inProgress: boolean, clearPublish: () => void, ffmpegStatus: any, optimize: boolean, size: number, duration: number, isVid: boolean, subtitle: string, setPublishMode: (string) => void, setPrevFileText: (string) => void, header: Node, channelId: string, setWaitForFile: (boolean) => void, }; function PublishFile(props: Props) { const { uri, mode, name, title, balance, filePath, fileMimeType, isStillEditing, updatePublishForm, disabled, publishing, inProgress, clearPublish, optimize, ffmpegStatus = {}, size, duration, isVid, setPublishMode, setPrevFileText, header, subtitle, } = props; const RECOMMENDED_BITRATE = 6000000; const PROCESSING_MB_PER_SECOND = 0.5; const MINUTES_THRESHOLD = 30; const HOURS_THRESHOLD = MINUTES_THRESHOLD * 60; const MARKDOWN_FILE_EXTENSIONS = ['txt', 'md', 'markdown']; const sizeInMB = Number(size) / 1000000; const secondsToProcess = sizeInMB / PROCESSING_MB_PER_SECOND; const ffmpegAvail = ffmpegStatus.available; const [currentFile, setCurrentFile] = useState(null); const [currentFileType, setCurrentFileType] = useState(null); const [optimizeAvail, setOptimizeAvail] = useState(false); const [userOptimize, setUserOptimize] = usePersistedState('publish-file-user-optimize', false); // Reset filePath if publish mode changed useEffect(() => { if (mode === PUBLISH_MODES.POST) { if (currentFileType !== 'text/markdown' && !isStillEditing) { updatePublishForm({ filePath: '' }); } } }, [currentFileType, mode, isStillEditing, updatePublishForm]); useEffect(() => { if (!filePath || filePath === '') { setCurrentFile(''); updateFileInfo(0, 0, false); } else if (typeof filePath !== 'string') { // Update currentFile file if (filePath.name !== currentFile && filePath.path !== currentFile) { handleFileChange(filePath); } } }, [filePath, currentFile, handleFileChange, updateFileInfo]); useEffect(() => { const isOptimizeAvail = currentFile && currentFile !== '' && isVid && ffmpegAvail; const finalOptimizeState = isOptimizeAvail && userOptimize; setOptimizeAvail(isOptimizeAvail); updatePublishForm({ optimize: finalOptimizeState }); }, [currentFile, filePath, isVid, ffmpegAvail, userOptimize, updatePublishForm]); function updateFileInfo(duration, size, isvid) { updatePublishForm({ fileDur: duration, fileSize: size, fileVid: isvid }); } function getBitrate(size, duration) { const s = Number(size); const d = Number(duration); if (s && d) { return (s * 8) / d; } else { return 0; } } function getTimeForMB(s) { if (s < MINUTES_THRESHOLD) { return Math.floor(secondsToProcess); } else if (s >= MINUTES_THRESHOLD && s < HOURS_THRESHOLD) { return Math.floor(secondsToProcess / 60); } else { return Math.floor(secondsToProcess / 60 / 60); } } function getUnitsForMB(s) { if (s < MINUTES_THRESHOLD) { if (secondsToProcess > 1) return __('seconds'); return __('second'); } else if (s >= MINUTES_THRESHOLD && s < HOURS_THRESHOLD) { if (Math.floor(secondsToProcess / 60) > 1) return __('minutes'); return __('minute'); } else { if (Math.floor(secondsToProcess / 3600) > 1) return __('hours'); return __('hour'); } } function getUploadMessage() { if (isVid && duration && getBitrate(size, duration) > RECOMMENDED_BITRATE) { return (

{__('Your video has a bitrate over 5 Mbps. We suggest transcoding to provide viewers the best experience.')}{' '}