diff --git a/electron/index.js b/electron/index.js index c7415e247..4d3b1e34f 100644 --- a/electron/index.js +++ b/electron/index.js @@ -18,9 +18,9 @@ import installDevtools from './installDevtools'; import fs from 'fs'; import path from 'path'; import { diskSpaceLinux, diskSpaceWindows, diskSpaceMac } from '../ui/util/diskspace'; +import mime from 'mime'; const { download } = require('electron-dl'); -const mime = require('mime'); const remote = require('@electron/remote/main'); const os = require('os'); const sudo = require('sudo-prompt'); @@ -366,12 +366,13 @@ ipcMain.handle('get-file-details-from-path', async (event, path) => { const name = folders[folders.length - 1]; let duration = 0, size = 0, mimeType; try { - await fs.promises.stat(path); + const stats = await fs.promises.stat(path); + size = stats.size; let ffprobeResults; try { ffprobeResults = await probe(path); duration = ffprobeResults.format.duration; - size = ffprobeResults.format.size; + size = ffprobeResults.format.size || stats.size; } catch (e) { } let fileReadResult; diff --git a/ui/component/publishFile/view.jsx b/ui/component/publishFile/view.jsx index 39538b8f0..d8b11b44a 100644 --- a/ui/component/publishFile/view.jsx +++ b/ui/component/publishFile/view.jsx @@ -1,7 +1,7 @@ // @flow import type { Node } from 'react'; import * as ICONS from 'constants/icons'; -import React, { useState, useEffect } from 'react'; +import React, { useState, useEffect, useCallback } from 'react'; import { ipcRenderer } from 'electron'; import { regexInvalidURI } from 'util/lbryURI'; import PostEditor from 'component/postEditor'; @@ -43,6 +43,13 @@ type Props = { setWaitForFile: (boolean) => void, }; +const MARKDOWN_FILE_EXTENSIONS = ['txt', 'md', 'markdown']; + +function parseName(newName) { + let INVALID_URI_CHARS = new RegExp(regexInvalidURI, 'gu'); + return newName.replace(INVALID_URI_CHARS, '-'); +} + function PublishFile(props: Props) { const { uri, @@ -74,7 +81,6 @@ function PublishFile(props: Props) { 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; @@ -92,6 +98,91 @@ function PublishFile(props: Props) { } }, [currentFileType, mode, isStillEditing, updatePublishForm]); + const updateFileInfo = useCallback( + (duration, size, isvid) => { + updatePublishForm({ fileDur: duration, fileSize: size, fileVid: isvid }); + }, + [updatePublishForm] + ); + + const handleFileReaderLoaded = useCallback( + (event: ProgressEvent) => { + // See: https://github.com/facebook/flow/issues/3470 + if (event.target instanceof FileReader) { + const text = event.target.result; + updatePublishForm({ fileText: text }); + setPublishMode(PUBLISH_MODES.POST); + } + }, + [updatePublishForm, setPublishMode] + ); + + const processSelectedFile = useCallback( + (fileData: FileData, clearName = true) => { + window.URL = window.URL || window.webkitURL; + + // select file, start to select a new one, then cancel + if (!fileData || fileData.error) { + if (isStillEditing || !clearName) { + updatePublishForm({ filePath: '' }); + } else { + updatePublishForm({ filePath: '', name: '' }); + } + return; + } + + // if video, extract duration so we can warn about bitrate if (typeof file !== 'string') + const file = fileData.file; + // Check to see if it's a video and if mp4 + const contentType = fileData.mimeType && fileData.mimeType.split('/'); // get this from electron side + const duration = fileData.duration; + const size = fileData.size; + const isVideo = contentType && contentType[0] === 'video'; + const isMp4 = contentType && contentType[1] === 'mp4'; + + let isTextPost = false; + + if (contentType && contentType[0] === 'text') { + isTextPost = contentType[1] === 'plain' || contentType[1] === 'markdown'; + setCurrentFileType(contentType.join('/')); + } else if (path.parse(fileData.path).ext) { + // If user's machine is missing a valid content type registration + // for markdown content: text/markdown, file extension will be used instead + const extension = path.parse(fileData.path).ext; + isTextPost = MARKDOWN_FILE_EXTENSIONS.includes(extension); + } + + if (isVideo) { + if (isMp4) { + updateFileInfo(duration || 0, size, isVideo); + } else { + updateFileInfo(duration || 0, size, isVideo); + } + } else { + updateFileInfo(0, size, isVideo); + } + + if (isTextPost && file) { + // Create reader + const reader = new FileReader(); + // Handler for file reader + reader.addEventListener('load', handleFileReaderLoaded); + // Read file contents + reader.readAsText(file); + setCurrentFileType('text/markdown'); + } else { + setPublishMode(PUBLISH_MODES.FILE); + } + + // Strip off extension and replace invalid characters + if (!isStillEditing) { + const fileWithoutExtension = path.parse(fileData.path).name; + updatePublishForm({ name: parseName(fileWithoutExtension) }); + } + }, + [handleFileReaderLoaded, isStillEditing, setPublishMode, updateFileInfo, updatePublishForm] + ); + // Since the filePath can be updated from outside this component // (for instance, when the user drags & drops a file), we need // to check for changes in the selected file using an effect. @@ -99,10 +190,14 @@ function PublishFile(props: Props) { if (!filePath) { return; } + let cancelled = false; async function readSelectedFileDetails() { // Read the file to get the file's duration (if possible) // and offer transcoding it. const result = await ipcRenderer.invoke('get-file-details-from-path', filePath); + if (cancelled) { + return; + } let file; if (result.buffer) { file = new File([result.buffer], result.name, { @@ -120,6 +215,9 @@ function PublishFile(props: Props) { processSelectedFile(fileData); } readSelectedFileDetails(); + return () => { + cancelled = true; + }; }, [filePath, processSelectedFile]); useEffect(() => { @@ -130,10 +228,6 @@ function PublishFile(props: Props) { 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); @@ -208,89 +302,12 @@ function PublishFile(props: Props) { } } - function parseName(newName) { - let INVALID_URI_CHARS = new RegExp(regexInvalidURI, 'gu'); - return newName.replace(INVALID_URI_CHARS, '-'); - } - function handleTitleChange(event) { const title = event.target.value; // Update title updatePublishForm({ title }); } - function handleFileReaderLoaded(event: ProgressEvent) { - // See: https://github.com/facebook/flow/issues/3470 - if (event.target instanceof FileReader) { - const text = event.target.result; - updatePublishForm({ fileText: text }); - setPublishMode(PUBLISH_MODES.POST); - } - } - - function processSelectedFile(fileData: FileData, clearName = true) { - window.URL = window.URL || window.webkitURL; - - // select file, start to select a new one, then cancel - if (!fileData || fileData.error) { - if (isStillEditing || !clearName) { - updatePublishForm({ filePath: '' }); - } else { - updatePublishForm({ filePath: '', name: '' }); - } - return; - } - - // if video, extract duration so we can warn about bitrate if (typeof file !== 'string') - const file = fileData.file; - // Check to see if it's a video and if mp4 - const contentType = fileData.mimeType && fileData.mimeType.split('/'); // get this from electron side - const duration = fileData.duration; - const size = fileData.size; - const isVideo = contentType && contentType[0] === 'video'; - const isMp4 = contentType && contentType[1] === 'mp4'; - - let isTextPost = false; - - if (contentType && contentType[0] === 'text') { - isTextPost = contentType[1] === 'plain' || contentType[1] === 'markdown'; - setCurrentFileType(contentType.join('/')); - } else if (path.parse(fileData.path).ext) { - // If user's machine is missing a valid content type registration - // for markdown content: text/markdown, file extension will be used instead - const extension = path.parse(fileData.path).ext; - isTextPost = MARKDOWN_FILE_EXTENSIONS.includes(extension); - } - - if (isVideo) { - if (isMp4) { - updateFileInfo(duration || 0, size, isVideo); - } else { - updateFileInfo(duration || 0, size, isVideo); - } - } else { - updateFileInfo(0, size, isVideo); - } - - if (isTextPost && file) { - // Create reader - const reader = new FileReader(); - // Handler for file reader - reader.addEventListener('load', handleFileReaderLoaded); - // Read file contents - reader.readAsText(file); - setCurrentFileType('text/markdown'); - } else { - setPublishMode(PUBLISH_MODES.FILE); - } - - // Strip off extension and replace invalid characters - if (!isStillEditing) { - const fileWithoutExtension = path.parse(fileData.path).name; - updatePublishForm({ name: parseName(fileWithoutExtension) }); - } - } - function handleFileChange(fileWithPath: FileWithPath) { if (fileWithPath) { updatePublishForm({ filePath: fileWithPath.path });