summaryrefslogtreecommitdiffstats
path: root/webapp/actions/file_actions.jsx
blob: 9a565a07c11d1b7e2d3396f2bfe77081434c783c (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
// 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 {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: this.translations.connectionError};
            } else {
                e = {message: this.translations.unknownError + ' (' + 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
    );
}