mattermost-mobile/app/actions/views/file_upload.js
Stephen Kiers 29705d0715 ICU-573 Allow flag on file upload to trigger unescape of filename (#1383)
* properly encode filename to be handled by android

* Perf updates

* remove form-data devDependency

* Broke out encoding to function

* missed one

* Removed fontSize prop from Emoji

* trigger jenkins

* reverse jenkins trigger

* fix styles

* eslint upgrade

* Fine, harrison…

* needed semicolon now….
2018-01-31 11:42:02 -05:00

122 lines
3.3 KiB
JavaScript

// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import {Platform} from 'react-native';
import {uploadFile} from 'mattermost-redux/actions/files';
import {
buildFileUploadData,
encodeHeaderURIStringToUTF8,
generateId
} from 'app/utils/file';
import {ViewTypes} from 'app/constants';
export function handleUploadFiles(files, rootId) {
return async (dispatch, getState) => {
const state = getState();
const channelId = state.entities.channels.currentChannelId;
const formData = new FormData();
const clientIds = [];
files.forEach((file) => {
const fileData = buildFileUploadData(file);
const clientId = generateId();
clientIds.push({
clientId,
localPath: fileData.uri,
name: fileData.name,
type: fileData.mimeType,
extension: fileData.extension
});
fileData.name = encodeHeaderURIStringToUTF8(file.fileName);
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';
}
dispatch({
type: ViewTypes.SET_TEMP_UPLOAD_FILES_FOR_POST_DRAFT,
clientIds,
channelId,
rootId
});
const clientIdsArray = clientIds.map((c) => c.clientId);
await uploadFile(channelId, rootId, clientIdsArray, formData, formBoundary)(dispatch, getState);
};
}
export function retryFileUpload(file, rootId) {
return async (dispatch, getState) => {
const state = getState();
const channelId = state.entities.channels.currentChannelId;
const formData = new FormData();
const fileData = {
uri: file.localPath,
name: file.name,
type: file.type
};
fileData.name = encodeHeaderURIStringToUTF8(file.fileName);
formData.append('files', fileData);
formData.append('channel_id', channelId);
formData.append('client_ids', file.clientId);
let formBoundary;
if (Platform.os === 'ios') {
formBoundary = '--mobile.client.file.upload';
}
dispatch({
type: ViewTypes.RETRY_UPLOAD_FILE_FOR_POST,
clientId: file.clientId,
channelId,
rootId
});
await uploadFile(channelId, rootId, [file.clientId], formData, formBoundary)(dispatch, getState);
};
}
export function handleClearFiles(channelId, rootId) {
return {
type: ViewTypes.CLEAR_FILES_FOR_POST_DRAFT,
channelId,
rootId
};
}
export function handleClearFailedFiles(channelId, rootId) {
return {
type: ViewTypes.CLEAR_FAILED_FILES_FOR_POST_DRAFT,
channelId,
rootId
};
}
export function handleRemoveFile(clientId, channelId, rootId) {
return {
type: ViewTypes.REMOVE_FILE_FROM_POST_DRAFT,
clientId,
channelId,
rootId
};
}
export function handleRemoveLastFile(channelId, rootId) {
return {
type: ViewTypes.REMOVE_LAST_FILE_FROM_POST_DRAFT,
channelId,
rootId
};
}