This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
file_actions.jsx
73 lines (63 loc) · 2.45 KB
/
file_actions.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {batchActions} from 'redux-batched-actions';
import request from 'superagent';
import {FileTypes} from 'mattermost-redux/action_types';
import {getLogErrorAction} from 'mattermost-redux/actions/errors';
import {forceLogoutIfNecessary} from 'mattermost-redux/actions/helpers';
import {Client4} from 'mattermost-redux/client';
import * as Utils from 'utils/utils.jsx';
export function uploadFile(file, name, channelId, rootId, clientId) {
return (dispatch) => {
dispatch({type: FileTypes.UPLOAD_FILES_REQUEST});
return request.
post(Client4.getFilesRoute()).
set(Client4.getOptions({method: 'post'}).headers).
attach('files', file, name).
field('channel_id', channelId).
field('client_ids', clientId).
accept('application/json');
};
}
export function handleFileUploadEnd(file, name, channelId, rootId, clientId, {err, res}) {
return (dispatch, getState) => {
if (err) {
let e;
if (res && res.body && res.body.id) {
e = res.body;
} else if (err.status === 0 || !err.status) {
e = {message: Utils.localizeMessage('file_upload.generic_error', 'There was a problem uploading your files.')};
} else {
e = {message: Utils.localizeMessage('channel_loader.unknown_error', 'We received an unexpected status code from the server.') + ' (' + err.status + ')'};
}
forceLogoutIfNecessary(err, dispatch, getState);
const failure = {
type: FileTypes.UPLOAD_FILES_FAILURE,
clientIds: [clientId],
channelId,
rootId,
error: err,
};
dispatch(batchActions([failure, getLogErrorAction(err)]));
return {error: e};
}
const data = res.body.file_infos.map((fileInfo, index) => {
return {
...fileInfo,
clientId: res.body.client_ids[index],
};
});
dispatch(batchActions([
{
type: FileTypes.RECEIVED_UPLOAD_FILES,
data,
channelId,
rootId,
},
{
type: FileTypes.UPLOAD_FILES_SUCCESS,
},
]));
return {data: res.body};
};
}