Fix uploads, stop useEffect loops
Some checks failed
Node.js CI / lint (push) Has been cancelled
Node.js CI / build (16.x, macos-13) (push) Has been cancelled
Node.js CI / build (16.x, ubuntu-latest) (push) Has been cancelled
Node.js CI / build (16.x, windows-latest) (push) Has been cancelled

This commit is contained in:
unfunny
2026-11-16 16:57:51 -05:00
parent 61ac3bf76f
commit f849612e4e
2 changed files with 104 additions and 86 deletions

View File

@@ -18,9 +18,9 @@ import installDevtools from './installDevtools';
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import { diskSpaceLinux, diskSpaceWindows, diskSpaceMac } from '../ui/util/diskspace'; import { diskSpaceLinux, diskSpaceWindows, diskSpaceMac } from '../ui/util/diskspace';
import mime from 'mime';
const { download } = require('electron-dl'); const { download } = require('electron-dl');
const mime = require('mime');
const remote = require('@electron/remote/main'); const remote = require('@electron/remote/main');
const os = require('os'); const os = require('os');
const sudo = require('sudo-prompt'); 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]; const name = folders[folders.length - 1];
let duration = 0, size = 0, mimeType; let duration = 0, size = 0, mimeType;
try { try {
await fs.promises.stat(path); const stats = await fs.promises.stat(path);
size = stats.size;
let ffprobeResults; let ffprobeResults;
try { try {
ffprobeResults = await probe(path); ffprobeResults = await probe(path);
duration = ffprobeResults.format.duration; duration = ffprobeResults.format.duration;
size = ffprobeResults.format.size; size = ffprobeResults.format.size || stats.size;
} catch (e) { } catch (e) {
} }
let fileReadResult; let fileReadResult;

View File

@@ -1,7 +1,7 @@
// @flow // @flow
import type { Node } from 'react'; import type { Node } from 'react';
import * as ICONS from 'constants/icons'; import * as ICONS from 'constants/icons';
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect, useCallback } from 'react';
import { ipcRenderer } from 'electron'; import { ipcRenderer } from 'electron';
import { regexInvalidURI } from 'util/lbryURI'; import { regexInvalidURI } from 'util/lbryURI';
import PostEditor from 'component/postEditor'; import PostEditor from 'component/postEditor';
@@ -43,6 +43,13 @@ type Props = {
setWaitForFile: (boolean) => void, 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) { function PublishFile(props: Props) {
const { const {
uri, uri,
@@ -74,7 +81,6 @@ function PublishFile(props: Props) {
const PROCESSING_MB_PER_SECOND = 0.5; const PROCESSING_MB_PER_SECOND = 0.5;
const MINUTES_THRESHOLD = 30; const MINUTES_THRESHOLD = 30;
const HOURS_THRESHOLD = MINUTES_THRESHOLD * 60; const HOURS_THRESHOLD = MINUTES_THRESHOLD * 60;
const MARKDOWN_FILE_EXTENSIONS = ['txt', 'md', 'markdown'];
const sizeInMB = Number(size) / 1000000; const sizeInMB = Number(size) / 1000000;
const secondsToProcess = sizeInMB / PROCESSING_MB_PER_SECOND; const secondsToProcess = sizeInMB / PROCESSING_MB_PER_SECOND;
const ffmpegAvail = ffmpegStatus.available; const ffmpegAvail = ffmpegStatus.available;
@@ -92,6 +98,91 @@ function PublishFile(props: Props) {
} }
}, [currentFileType, mode, isStillEditing, updatePublishForm]); }, [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 // Since the filePath can be updated from outside this component
// (for instance, when the user drags & drops a file), we need // (for instance, when the user drags & drops a file), we need
// to check for changes in the selected file using an effect. // to check for changes in the selected file using an effect.
@@ -99,10 +190,14 @@ function PublishFile(props: Props) {
if (!filePath) { if (!filePath) {
return; return;
} }
let cancelled = false;
async function readSelectedFileDetails() { async function readSelectedFileDetails() {
// Read the file to get the file's duration (if possible) // Read the file to get the file's duration (if possible)
// and offer transcoding it. // and offer transcoding it.
const result = await ipcRenderer.invoke('get-file-details-from-path', filePath); const result = await ipcRenderer.invoke('get-file-details-from-path', filePath);
if (cancelled) {
return;
}
let file; let file;
if (result.buffer) { if (result.buffer) {
file = new File([result.buffer], result.name, { file = new File([result.buffer], result.name, {
@@ -120,6 +215,9 @@ function PublishFile(props: Props) {
processSelectedFile(fileData); processSelectedFile(fileData);
} }
readSelectedFileDetails(); readSelectedFileDetails();
return () => {
cancelled = true;
};
}, [filePath, processSelectedFile]); }, [filePath, processSelectedFile]);
useEffect(() => { useEffect(() => {
@@ -130,10 +228,6 @@ function PublishFile(props: Props) {
updatePublishForm({ optimize: finalOptimizeState }); updatePublishForm({ optimize: finalOptimizeState });
}, [currentFile, filePath, isVid, ffmpegAvail, userOptimize, updatePublishForm]); }, [currentFile, filePath, isVid, ffmpegAvail, userOptimize, updatePublishForm]);
function updateFileInfo(duration, size, isvid) {
updatePublishForm({ fileDur: duration, fileSize: size, fileVid: isvid });
}
function getBitrate(size, duration) { function getBitrate(size, duration) {
const s = Number(size); const s = Number(size);
const d = Number(duration); 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) { function handleTitleChange(event) {
const title = event.target.value; const title = event.target.value;
// Update title // Update title
updatePublishForm({ 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) { function handleFileChange(fileWithPath: FileWithPath) {
if (fileWithPath) { if (fileWithPath) {
updatePublishForm({ filePath: fileWithPath.path }); updatePublishForm({ filePath: fileWithPath.path });