mattermost-mobile/app/actions/views/file_upload.js
Chris Duarte 18cc7f1e7f PLT-5518 RN: Allow user to upload + send image file attachments (#373)
* PLT-5518 RN: Allow user to upload + send image file attachments

* Review feedback
2017-03-21 17:58:31 -04:00

60 lines
1.7 KiB
JavaScript

// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import FormData from 'form-data';
import {Platform} from 'react-native';
import {uploadFile} from 'mattermost-redux/actions/files';
import {lookupMimeType} from 'mattermost-redux/utils/file_utils';
import {generateId} from 'app/utils/file';
import {ViewTypes} from 'app/constants';
export function handleUploadFiles(files, rootId, requestId) {
return async (dispatch, getState) => {
const state = getState();
const teamId = state.entities.teams.currentTeamId;
const channelId = state.entities.channels.currentChannelId;
const formData = new FormData();
files.forEach((file) => {
const name = file.path.split('/').pop();
const mimeType = lookupMimeType(name);
const clientId = generateId();
const fileData = {
uri: file.path,
name,
type: mimeType
};
formData.append('files', fileData);
formData.append('channel_id', channelId);
formData.append('client_ids', clientId);
});
let formBoundary;
if (Platform.os === 'ios') {
formBoundary = '--mobile.client.file.upload';
}
await uploadFile(teamId, channelId, formData, formBoundary, rootId, requestId)(dispatch, getState);
};
}
export function handleClearFiles(channelId, rootId) {
return {
type: ViewTypes.CLEAR_FILES_FOR_POST_DRAFT,
channelId,
rootId
};
}
export function handleRemoveFile(fileId, channelId, rootId) {
return {
type: ViewTypes.REMOVE_FILE_FROM_POST_DRAFT,
fileId,
channelId,
rootId
};
}