summaryrefslogtreecommitdiffstats
path: root/webapp/actions/file_actions.jsx
blob: 1d9617901f02e84b8a3b8f73e57e830a7ead0e31 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import {batchActions} from 'redux-batched-actions';
import request from 'superagent';

import store from 'stores/redux_store.jsx';

import * as Utils from 'utils/utils.jsx';

import {FileTypes} from 'mattermost-redux/action_types';
import {forceLogoutIfNecessary} from 'mattermost-redux/actions/helpers';
import {getLogErrorAction} from 'mattermost-redux/actions/errors';
import {Client4} from 'mattermost-redux/client';

export function uploadFile(file, name, channelId, clientId, successCallback, errorCallback) {
    const {dispatch, getState} = store;

    function handleResponse(err, res) {
        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('channel_loader.connection_error', 'There appears to be a problem with your internet connection.')};
            } else {
                e = {message: Utils.localizeMessage('channel_loader.unknown_error', 'We received an unexpected status code from the server.') + ' (' + err.status + ')'};
            }

            forceLogoutIfNecessary(err, dispatch);

            const failure = {
                type: FileTypes.UPLOAD_FILES_FAILURE,
                clientIds: [clientId],
                channelId,
                rootId: null,
                error: err
            };

            dispatch(batchActions([failure, getLogErrorAction(err)]), getState);

            if (errorCallback) {
                errorCallback(e, err, res);
            }
        } else if (res) {
            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: null
                },
                {
                    type: FileTypes.UPLOAD_FILES_SUCCESS
                }
            ]), getState);

            if (successCallback) {
                successCallback(res.body, res);
            }
        }
    }

    dispatch({type: FileTypes.UPLOAD_FILES_REQUEST}, getState);

    return request.
        post(Client4.getFilesRoute()).
        set(Client4.getOptions().headers).
        attach('files', file, name).
        field('channel_id', channelId).
        field('client_ids', clientId).
        accept('application/json').
        end(handleResponse);
}

export async function getPublicLink(fileId, success) {
    Client4.getFilePublicLink(fileId).then(
        (data) => {
            if (data && success) {
                success(data.link);
            }
        }
    ).catch(
        () => {} //eslint-disable-line no-empty-function
    );
}