@ -8,6 +8,8 @@ import resizeImage from '../utils/resize_image';
import { importFetchedAccounts } from './importer' ;
import { updateTimeline } from './timelines' ;
import { showAlertForError } from './alerts' ;
import { showAlert } from './alerts' ;
import { defineMessages } from 'react-intl' ;
let cancelFetchComposeSuggestionsAccounts ;
@ -49,6 +51,10 @@ export const COMPOSE_UPLOAD_CHANGE_REQUEST = 'COMPOSE_UPLOAD_UPDATE_REQUEST'
export const COMPOSE _UPLOAD _CHANGE _SUCCESS = 'COMPOSE_UPLOAD_UPDATE_SUCCESS' ;
export const COMPOSE _UPLOAD _CHANGE _FAIL = 'COMPOSE_UPLOAD_UPDATE_FAIL' ;
const messages = defineMessages ( {
uploadErrorLimit : { id : 'upload_error.limit' , defaultMessage : 'File upload limit exceeded.' } ,
} ) ;
export function changeCompose ( text ) {
return {
type : COMPOSE _CHANGE ,
@ -184,20 +190,32 @@ export function submitComposeFail(error) {
export function uploadCompose ( files ) {
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 ;
}
dispatch ( uploadComposeRequest ( ) ) ;
resizeImage ( files [ 0 ] ) . then ( file => {
const data = new FormData ( ) ;
data . append ( 'file' , file ) ;
return api ( getState ) . post ( '/api/v1/media' , data , {
onUploadProgress : ( { loaded , total } ) => dispatch ( uploadComposeProgress ( loaded , total ) ) ,
} ) . then ( ( { data } ) => dispatch ( uploadComposeSuccess ( data ) ) ) ;
} ) . catch ( error => dispatch ( uploadComposeFail ( error ) ) ) ;
for ( const [ i , f ] of Array . from ( files ) . entries ( ) ) {
if ( media . size + i > 3 ) break ;
resizeImage ( f ) . then ( file => {
const data = new FormData ( ) ;
data . append ( 'file' , file ) ;
return api ( getState ) . post ( '/api/v1/media' , data , {
onUploadProgress : function ( { loaded } ) {
progress [ i ] = loaded ;
dispatch ( uploadComposeProgress ( progress . reduce ( ( a , v ) => a + v , 0 ) , total ) ) ;
} ,
} ) . then ( ( { data } ) => dispatch ( uploadComposeSuccess ( data ) ) ) ;
} ) . catch ( error => dispatch ( uploadComposeFail ( error ) ) ) ;
} ;
} ;
} ;