[Glitch] Allow multiple files upload through web UI, including drag & drop
Port 750c67660d
to glitch-soc
This commit is contained in:
parent
a963ea67dd
commit
7ed2aeb6e9
|
@ -22,7 +22,7 @@ export function clearAlert() {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export function showAlert(title, message) {
|
export function showAlert(title = messages.unexpectedTitle, message = messages.unexpectedMessage) {
|
||||||
return {
|
return {
|
||||||
type: ALERT_SHOW,
|
type: ALERT_SHOW,
|
||||||
title,
|
title,
|
||||||
|
@ -44,6 +44,6 @@ export function showAlertForError(error) {
|
||||||
return showAlert(title, message);
|
return showAlert(title, message);
|
||||||
} else {
|
} else {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
return showAlert(messages.unexpectedTitle, messages.unexpectedMessage);
|
return showAlert();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,8 @@ import resizeImage from 'flavours/glitch/util/resize_image';
|
||||||
|
|
||||||
import { updateTimeline } from './timelines';
|
import { updateTimeline } from './timelines';
|
||||||
import { showAlertForError } from './alerts';
|
import { showAlertForError } from './alerts';
|
||||||
|
import { showAlert } from './alerts';
|
||||||
|
import { defineMessages } from 'react-intl';
|
||||||
|
|
||||||
let cancelFetchComposeSuggestionsAccounts;
|
let cancelFetchComposeSuggestionsAccounts;
|
||||||
|
|
||||||
|
@ -53,6 +55,10 @@ export const COMPOSE_UPLOAD_CHANGE_FAIL = 'COMPOSE_UPLOAD_UPDATE_FAIL';
|
||||||
|
|
||||||
export const COMPOSE_DOODLE_SET = 'COMPOSE_DOODLE_SET';
|
export const COMPOSE_DOODLE_SET = 'COMPOSE_DOODLE_SET';
|
||||||
|
|
||||||
|
const messages = defineMessages({
|
||||||
|
uploadErrorLimit: { id: 'upload_error.limit', defaultMessage: 'File upload limit exceeded.' },
|
||||||
|
});
|
||||||
|
|
||||||
export function changeCompose(text) {
|
export function changeCompose(text) {
|
||||||
return {
|
return {
|
||||||
type: COMPOSE_CHANGE,
|
type: COMPOSE_CHANGE,
|
||||||
|
@ -208,22 +214,34 @@ export function doodleSet(options) {
|
||||||
|
|
||||||
export function uploadCompose(files) {
|
export function uploadCompose(files) {
|
||||||
return function (dispatch, getState) {
|
return function (dispatch, getState) {
|
||||||
if (getState().getIn(['compose', 'media_attachments']).size > 3) {
|
const uploadLimit = 4;
|
||||||
|
const media = getState().getIn(['compose', 'media_attachments']);
|
||||||
|
const total = Array.from(files).reduce((a, v) => a + v.size, 0);
|
||||||
|
const progress = new Array(files.length).fill(0);
|
||||||
|
|
||||||
|
if (files.length + media.size > uploadLimit) {
|
||||||
|
dispatch(showAlert(undefined, messages.uploadErrorLimit));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch(uploadComposeRequest());
|
dispatch(uploadComposeRequest());
|
||||||
|
|
||||||
resizeImage(files[0]).then(file => {
|
for (const [i, f] of Array.from(files).entries()) {
|
||||||
|
if (media.size + i > 3) break;
|
||||||
|
|
||||||
|
resizeImage(f).then(file => {
|
||||||
const data = new FormData();
|
const data = new FormData();
|
||||||
data.append('file', file);
|
data.append('file', file);
|
||||||
|
|
||||||
return api(getState).post('/api/v1/media', data, {
|
return api(getState).post('/api/v1/media', data, {
|
||||||
onUploadProgress: ({ loaded, total }) => dispatch(uploadComposeProgress(loaded, total)),
|
onUploadProgress: function({ loaded }){
|
||||||
|
progress[i] = loaded;
|
||||||
|
dispatch(uploadComposeProgress(progress.reduce((a, v) => a + v, 0), total));
|
||||||
|
},
|
||||||
}).then(({ data }) => dispatch(uploadComposeSuccess(data)));
|
}).then(({ data }) => dispatch(uploadComposeSuccess(data)));
|
||||||
}).catch(error => dispatch(uploadComposeFail(error)));
|
}).catch(error => dispatch(uploadComposeFail(error)));
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export function changeUploadCompose(id, params) {
|
export function changeUploadCompose(id, params) {
|
||||||
return (dispatch, getState) => {
|
return (dispatch, getState) => {
|
||||||
|
|
|
@ -214,6 +214,7 @@ export default class ComposerOptions extends React.PureComponent {
|
||||||
onChange={handleChangeFiles}
|
onChange={handleChangeFiles}
|
||||||
ref={handleRefFileElement}
|
ref={handleRefFileElement}
|
||||||
type='file'
|
type='file'
|
||||||
|
multiple
|
||||||
{...hiddenComponent}
|
{...hiddenComponent}
|
||||||
/>
|
/>
|
||||||
<Dropdown
|
<Dropdown
|
||||||
|
|
|
@ -186,7 +186,7 @@ export default class UI extends React.Component {
|
||||||
this.setState({ draggingOver: false });
|
this.setState({ draggingOver: false });
|
||||||
this.dragTargets = [];
|
this.dragTargets = [];
|
||||||
|
|
||||||
if (e.dataTransfer && e.dataTransfer.files.length === 1) {
|
if (e.dataTransfer && e.dataTransfer.files.length >= 1) {
|
||||||
this.props.dispatch(uploadCompose(e.dataTransfer.files));
|
this.props.dispatch(uploadCompose(e.dataTransfer.files));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue