aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/helpers/httpHelper.js
blob: 27695d5525b1bd48e226b3ec3fed4d7e0fc1cde0 (plain) (blame)
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
import axios from 'axios';

const base = `http://localhost:8080/api`

export function FetchPaste(hash) {
    const serverURL = `${base}/${hash}`
    console.log(serverURL)
    return axios.get(serverURL)
}

export function FetchPasswordPaste(hash, pass) {
    var bodyFormData = new FormData();
    bodyFormData.set('password', pass);

    return axios({
        method: 'post',
        url: `${base}/${hash}`,
        data: bodyFormData,
        headers: { 'Content-Type': 'multipart/form-data' },
    })
}

export function PostNewPaste(state) {
    var bodyFormData = new FormData();
    bodyFormData.set('title', state.title);
    bodyFormData.set('content', state.content);
    bodyFormData.set('password', state.pass);
    bodyFormData.set('expiry', parseExpiry(state.expiry));

    return axios({
        method: 'post',
        url: base,
        data: bodyFormData,
        headers: { 'Content-Type': 'multipart/form-data' },
    })
}

function parseExpiry(e) {
    var cur = new Date();
    var inSeconds = 0
    switch (e) {
        case '5 years':
            inSeconds = 600 * 6 * 24 * 7 * 4 * 12 * 5
            break;
        case '1 year':
            inSeconds = 600 * 6 * 24 * 7 * 4 * 12
            break;
        case '1 month':
            inSeconds = 600 * 6 * 24 * 7 * 4
            break;
        case '1 day':
            inSeconds = 600 * 6 * 24
            break;
        case '1 hour':
            inSeconds = 600 * 6
            break;
        case '10 min':
            inSeconds = 600
            break;
        case '1 week':
        default:
            inSeconds = 600 * 6 * 24 * 7
            break;
    }
    return new Date(cur.getTime() + inSeconds * 1000).toISOString();
}