Refactor WebFile.
This commit is contained in:
parent
3bc2373984
commit
fa5c1a1cf3
15 changed files with 48 additions and 54 deletions
9
flow-typed/file-with-path.js
vendored
Normal file
9
flow-typed/file-with-path.js
vendored
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
// @flow
|
||||||
|
|
||||||
|
declare type FileWithPath = {
|
||||||
|
file: File,
|
||||||
|
// The full path will only be available in
|
||||||
|
// the application. For browser, the name
|
||||||
|
// of the file will be used.
|
||||||
|
path: string,
|
||||||
|
}
|
6
flow-typed/web-file.js
vendored
6
flow-typed/web-file.js
vendored
|
@ -1,6 +0,0 @@
|
||||||
// @flow
|
|
||||||
|
|
||||||
declare type WebFile = File & {
|
|
||||||
path?: string,
|
|
||||||
title?: string,
|
|
||||||
}
|
|
|
@ -3,8 +3,8 @@ import React from 'react';
|
||||||
import { useRadioState, Radio, RadioGroup } from 'reakit/Radio';
|
import { useRadioState, Radio, RadioGroup } from 'reakit/Radio';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
files: Array<WebFile>,
|
files: Array<File>,
|
||||||
onChange: (WebFile | void) => void,
|
onChange: (File | void) => void,
|
||||||
};
|
};
|
||||||
|
|
||||||
type RadioProps = {
|
type RadioProps = {
|
||||||
|
@ -26,7 +26,7 @@ function FileList(props: Props) {
|
||||||
|
|
||||||
const getFile = (value?: string) => {
|
const getFile = (value?: string) => {
|
||||||
if (files && files.length) {
|
if (files && files.length) {
|
||||||
return files.find((file: WebFile) => file.name === value);
|
return files.find((file: File) => file.name === value);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -46,12 +46,12 @@ function FileList(props: Props) {
|
||||||
|
|
||||||
if (radio.state) {
|
if (radio.state) {
|
||||||
// Find selected element
|
// Find selected element
|
||||||
const stop = radio.stops.find(item => item.id === radio.currentId);
|
const stop = radio.stops.find((item) => item.id === radio.currentId);
|
||||||
const element = stop && stop.ref.current;
|
const element = stop && stop.ref.current;
|
||||||
// Only update state if new item is selected
|
// Only update state if new item is selected
|
||||||
if (element && element.value !== radio.state) {
|
if (element && element.value !== radio.state) {
|
||||||
const file = getFile(element.value);
|
const file = getFile(element.value);
|
||||||
// Sselect new file and update state
|
// Select new file and update state
|
||||||
onChange(file);
|
onChange(file);
|
||||||
radio.setState(element.value);
|
radio.setState(element.value);
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ import { FormField } from 'component/common/form';
|
||||||
type Props = {
|
type Props = {
|
||||||
type: string,
|
type: string,
|
||||||
currentPath?: ?string,
|
currentPath?: ?string,
|
||||||
onFileChosen: (WebFile) => void,
|
onFileChosen: (FileWithPath) => void,
|
||||||
label?: string,
|
label?: string,
|
||||||
placeholder?: string,
|
placeholder?: string,
|
||||||
accept?: string,
|
accept?: string,
|
||||||
|
@ -43,7 +43,7 @@ class FileSelector extends React.PureComponent<Props> {
|
||||||
const file = files[0];
|
const file = files[0];
|
||||||
|
|
||||||
if (this.props.onFileChosen) {
|
if (this.props.onFileChosen) {
|
||||||
this.props.onFileChosen(file);
|
this.props.onFileChosen({ file, path: file.path || file.name });
|
||||||
}
|
}
|
||||||
this.fileInput.current.value = null; // clear the file input
|
this.fileInput.current.value = null; // clear the file input
|
||||||
};
|
};
|
||||||
|
@ -86,14 +86,7 @@ class FileSelector extends React.PureComponent<Props> {
|
||||||
const file = new File([result.buffer], result.name, {
|
const file = new File([result.buffer], result.name, {
|
||||||
type: result.mime,
|
type: result.mime,
|
||||||
});
|
});
|
||||||
// "path" is a read only property so we have to use this
|
this.props.onFileChosen({ file, path: result.path });
|
||||||
// hack to overcome the limitation.
|
|
||||||
// $FlowFixMe
|
|
||||||
Object.defineProperty(file, 'path', {
|
|
||||||
value: result.path,
|
|
||||||
writable: false,
|
|
||||||
});
|
|
||||||
this.props.onFileChosen(file);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -11,10 +11,10 @@ import Icon from 'component/common/icon';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
modal: { id: string, modalProps: {} },
|
modal: { id: string, modalProps: {} },
|
||||||
filePath: string | WebFile,
|
filePath: string | File,
|
||||||
clearPublish: () => void,
|
clearPublish: () => void,
|
||||||
updatePublishForm: ({}) => void,
|
updatePublishForm: ({}) => void,
|
||||||
openModal: (id: string, { files: Array<WebFile> }) => void,
|
openModal: (id: string, { files: Array<File> }) => void,
|
||||||
// React router
|
// React router
|
||||||
history: {
|
history: {
|
||||||
entities: {}[],
|
entities: {}[],
|
||||||
|
@ -37,7 +37,7 @@ function FileDrop(props: Props) {
|
||||||
const { drag, dropData } = useDragDrop();
|
const { drag, dropData } = useDragDrop();
|
||||||
const [files, setFiles] = React.useState([]);
|
const [files, setFiles] = React.useState([]);
|
||||||
const [error, setError] = React.useState(false);
|
const [error, setError] = React.useState(false);
|
||||||
const [target, setTarget] = React.useState<?WebFile>(null);
|
const [target, setTarget] = React.useState<?File>(null);
|
||||||
const hideTimer = React.useRef(null);
|
const hideTimer = React.useRef(null);
|
||||||
const targetTimer = React.useRef(null);
|
const targetTimer = React.useRef(null);
|
||||||
const navigationTimer = React.useRef(null);
|
const navigationTimer = React.useRef(null);
|
||||||
|
|
|
@ -6,7 +6,7 @@ type Props = {
|
||||||
uri: ?string,
|
uri: ?string,
|
||||||
label: ?string,
|
label: ?string,
|
||||||
disabled: ?boolean,
|
disabled: ?boolean,
|
||||||
filePath: string | WebFile,
|
filePath: string | File,
|
||||||
fileText: ?string,
|
fileText: ?string,
|
||||||
fileMimeType: ?string,
|
fileMimeType: ?string,
|
||||||
streamingUrl: ?string,
|
streamingUrl: ?string,
|
||||||
|
|
|
@ -19,7 +19,7 @@ type Props = {
|
||||||
mode: ?string,
|
mode: ?string,
|
||||||
name: ?string,
|
name: ?string,
|
||||||
title: ?string,
|
title: ?string,
|
||||||
filePath: string | WebFile,
|
filePath: string | File,
|
||||||
fileMimeType: ?string,
|
fileMimeType: ?string,
|
||||||
isStillEditing: boolean,
|
isStillEditing: boolean,
|
||||||
balance: number,
|
balance: number,
|
||||||
|
@ -97,8 +97,8 @@ function PublishFile(props: Props) {
|
||||||
updateFileInfo(0, 0, false);
|
updateFileInfo(0, 0, false);
|
||||||
} else if (typeof filePath !== 'string') {
|
} else if (typeof filePath !== 'string') {
|
||||||
// Update currentFile file
|
// Update currentFile file
|
||||||
if (filePath.name !== currentFile && filePath.path !== currentFile) {
|
if (filePath.name !== currentFile) {
|
||||||
handleFileChange(filePath);
|
handleFileChange({ file: filePath, path: filePath.name });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [filePath, currentFile, handleFileChange, updateFileInfo]);
|
}, [filePath, currentFile, handleFileChange, updateFileInfo]);
|
||||||
|
@ -209,11 +209,11 @@ function PublishFile(props: Props) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleFileChange(file: WebFile, clearName = true) {
|
function handleFileChange(fileWithPath: FileWithPath, clearName = true) {
|
||||||
window.URL = window.URL || window.webkitURL;
|
window.URL = window.URL || window.webkitURL;
|
||||||
|
|
||||||
// select file, start to select a new one, then cancel
|
// select file, start to select a new one, then cancel
|
||||||
if (!file) {
|
if (!fileWithPath) {
|
||||||
if (isStillEditing || !clearName) {
|
if (isStillEditing || !clearName) {
|
||||||
updatePublishForm({ filePath: '' });
|
updatePublishForm({ filePath: '' });
|
||||||
} else {
|
} else {
|
||||||
|
@ -223,6 +223,7 @@ function PublishFile(props: Props) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// if video, extract duration so we can warn about bitrateif (typeof file !== 'string') {
|
// if video, extract duration so we can warn about bitrateif (typeof file !== 'string') {
|
||||||
|
const file = fileWithPath.file;
|
||||||
const contentType = file.type && file.type.split('/');
|
const contentType = file.type && file.type.split('/');
|
||||||
const isVideo = contentType && contentType[0] === 'video';
|
const isVideo = contentType && contentType[0] === 'video';
|
||||||
const isMp4 = contentType && contentType[1] === 'mp4';
|
const isMp4 = contentType && contentType[1] === 'mp4';
|
||||||
|
@ -270,10 +271,10 @@ function PublishFile(props: Props) {
|
||||||
setPublishMode(PUBLISH_MODES.FILE);
|
setPublishMode(PUBLISH_MODES.FILE);
|
||||||
}
|
}
|
||||||
|
|
||||||
const publishFormParams: { filePath: string | WebFile, name?: string, optimize?: boolean } = {
|
const publishFormParams: { filePath: string | File, name?: string, optimize?: boolean } = {
|
||||||
// if electron, we'll set filePath to the path string because SDK is handling publishing.
|
// if electron, we'll set filePath to the path string because SDK is handling publishing.
|
||||||
// File.path will be undefined from web due to browser security, so it will default to the File Object.
|
// File.path will be undefined from web due to browser security, so it will default to the File Object.
|
||||||
filePath: file.path || file,
|
filePath: fileWithPath.path || file,
|
||||||
};
|
};
|
||||||
// Strip off extention and replace invalid characters
|
// Strip off extention and replace invalid characters
|
||||||
let fileName = name || (file.name && file.name.substring(0, file.name.lastIndexOf('.'))) || '';
|
let fileName = name || (file.name && file.name.substring(0, file.name.lastIndexOf('.'))) || '';
|
||||||
|
@ -282,8 +283,7 @@ function PublishFile(props: Props) {
|
||||||
publishFormParams.name = parseName(fileName);
|
publishFormParams.name = parseName(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
// File path is not supported on web for security reasons so we use the name instead.
|
setCurrentFile(fileWithPath.path);
|
||||||
setCurrentFile(file.path || file.name);
|
|
||||||
updatePublishForm(publishFormParams);
|
updatePublishForm(publishFormParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -137,12 +137,10 @@ function SelectAsset(props: Props) {
|
||||||
label={fileSelectorLabel}
|
label={fileSelectorLabel}
|
||||||
name="assetSelector"
|
name="assetSelector"
|
||||||
currentPath={pathSelected}
|
currentPath={pathSelected}
|
||||||
onFileChosen={(file) => {
|
onFileChosen={(fileWithPath) => {
|
||||||
if (file.name) {
|
if (fileWithPath.file.name) {
|
||||||
setFileSelected(file);
|
setFileSelected(fileWithPath.file);
|
||||||
// what why? why not target=WEB this?
|
setPathSelected(fileWithPath.path);
|
||||||
// file.path is undefined in web but available in electron
|
|
||||||
setPathSelected(file.name || file.path);
|
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
accept={accept}
|
accept={accept}
|
||||||
|
|
|
@ -160,9 +160,9 @@ function SelectThumbnail(props: Props) {
|
||||||
label={__('Thumbnail')}
|
label={__('Thumbnail')}
|
||||||
placeholder={__('Choose an enticing thumbnail')}
|
placeholder={__('Choose an enticing thumbnail')}
|
||||||
accept={accept}
|
accept={accept}
|
||||||
onFileChosen={(file) =>
|
onFileChosen={(fileWithPath) =>
|
||||||
openModal(MODALS.CONFIRM_THUMBNAIL_UPLOAD, {
|
openModal(MODALS.CONFIRM_THUMBNAIL_UPLOAD, {
|
||||||
file,
|
file: fileWithPath,
|
||||||
cb: (url) => updateThumbnailParams && updateThumbnailParams({ thumbnail_url: url }),
|
cb: (url) => updateThumbnailParams && updateThumbnailParams({ thumbnail_url: url }),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -130,7 +130,7 @@ export default function SettingSystem(props: Props) {
|
||||||
<FileSelector
|
<FileSelector
|
||||||
type="openDirectory"
|
type="openDirectory"
|
||||||
currentPath={daemonSettings.download_dir}
|
currentPath={daemonSettings.download_dir}
|
||||||
onFileChosen={(newDirectory: WebFile) => {
|
onFileChosen={(newDirectory: FileWithPath) => {
|
||||||
setDaemonSetting('download_dir', newDirectory.path);
|
setDaemonSetting('download_dir', newDirectory.path);
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
@ -224,7 +224,7 @@ export default function SettingSystem(props: Props) {
|
||||||
type="openDirectory"
|
type="openDirectory"
|
||||||
placeholder={__('A Folder containing FFmpeg')}
|
placeholder={__('A Folder containing FFmpeg')}
|
||||||
currentPath={ffmpegPath || daemonSettings.ffmpeg_path}
|
currentPath={ffmpegPath || daemonSettings.ffmpeg_path}
|
||||||
onFileChosen={(newDirectory: WebFile) => {
|
onFileChosen={(newDirectory: FileWithPath) => {
|
||||||
// $FlowFixMe
|
// $FlowFixMe
|
||||||
setDaemonSetting('ffmpeg_path', newDirectory.path);
|
setDaemonSetting('ffmpeg_path', newDirectory.path);
|
||||||
findFFmpeg();
|
findFFmpeg();
|
||||||
|
|
|
@ -4,7 +4,7 @@ import { Modal } from 'modal/modal';
|
||||||
import { formatFileSystemPath } from 'util/url';
|
import { formatFileSystemPath } from 'util/url';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
upload: WebFile => void,
|
upload: (File) => void,
|
||||||
filePath: string,
|
filePath: string,
|
||||||
closeModal: () => void,
|
closeModal: () => void,
|
||||||
showToast: ({}) => void,
|
showToast: ({}) => void,
|
||||||
|
|
|
@ -5,7 +5,7 @@ import ModalConfirmThumbnailUpload from './view';
|
||||||
|
|
||||||
const perform = (dispatch) => ({
|
const perform = (dispatch) => ({
|
||||||
closeModal: () => dispatch(doHideModal()),
|
closeModal: () => dispatch(doHideModal()),
|
||||||
upload: (file, cb) => dispatch(doUploadThumbnail(null, file, null, null, file.path, cb)),
|
upload: (fileWithPath, cb) => dispatch(doUploadThumbnail(null, fileWithPath.file, null, null, fileWithPath.path, cb)),
|
||||||
updatePublishForm: (value) => dispatch(doUpdatePublishForm(value)),
|
updatePublishForm: (value) => dispatch(doUpdatePublishForm(value)),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,8 @@ import { Modal } from 'modal/modal';
|
||||||
import { DOMAIN } from 'config';
|
import { DOMAIN } from 'config';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
file: WebFile,
|
file: FileWithPath,
|
||||||
upload: (WebFile, (string) => void) => void,
|
upload: (FileWithPath, (string) => void) => void,
|
||||||
cb: (string) => void,
|
cb: (string) => void,
|
||||||
closeModal: () => void,
|
closeModal: () => void,
|
||||||
updatePublishForm: ({}) => void,
|
updatePublishForm: ({}) => void,
|
||||||
|
@ -23,7 +23,7 @@ class ModalConfirmThumbnailUpload extends React.PureComponent<Props> {
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { closeModal, file } = this.props;
|
const { closeModal, file } = this.props;
|
||||||
const filePath = file && (file.path || file.name);
|
const filePath = file && file.path;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Modal
|
<Modal
|
||||||
|
|
|
@ -9,12 +9,12 @@ import Button from 'component/button';
|
||||||
import FileList from 'component/common/file-list';
|
import FileList from 'component/common/file-list';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
files: Array<WebFile>,
|
files: Array<File>,
|
||||||
hideModal: () => void,
|
hideModal: () => void,
|
||||||
updatePublishForm: ({}) => void,
|
updatePublishForm: ({}) => void,
|
||||||
history: {
|
history: {
|
||||||
location: { pathname: string },
|
location: { pathname: string },
|
||||||
push: string => void,
|
push: (string) => void,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ const ModalFileSelection = (props: Props) => {
|
||||||
navigateToPublish();
|
navigateToPublish();
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleFileChange = (file?: WebFile) => {
|
const handleFileChange = (file?: File) => {
|
||||||
// $FlowFixMe
|
// $FlowFixMe
|
||||||
setSelectedFile(file);
|
setSelectedFile(file);
|
||||||
};
|
};
|
||||||
|
|
|
@ -15,7 +15,7 @@ import Icon from 'component/common/icon';
|
||||||
import { NO_FILE } from 'redux/actions/publish';
|
import { NO_FILE } from 'redux/actions/publish';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
filePath: string | WebFile,
|
filePath: string | File,
|
||||||
isMarkdownPost: boolean,
|
isMarkdownPost: boolean,
|
||||||
optimize: boolean,
|
optimize: boolean,
|
||||||
title: ?string,
|
title: ?string,
|
||||||
|
@ -104,7 +104,7 @@ const ModalPublishPreview = (props: Props) => {
|
||||||
// @endif
|
// @endif
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFilePathName(filePath: string | WebFile) {
|
function getFilePathName(filePath: string | File) {
|
||||||
if (!filePath) {
|
if (!filePath) {
|
||||||
return NO_FILE;
|
return NO_FILE;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue