aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/http/shared.js
blob: 00d41e994f9e361e35cb2abc144bb05a87d16ce5 (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
67
68
69
70
71
72
73
74
import axios from 'axios';

// uncomment for local dev
// const base = `http://localhost:8080/api`
const base = `https://api.ctrl-v.app/api`
export function fetchPaste(hash, pass = "") {
  const serverURL = `${base}/${hash}`

  if (pass === "") {
    return axios.get(serverURL)
  } else {
    const bodyFormData = new FormData();
    bodyFormData.set('password', pass);
    return axios({
      method: 'post',
      url: `${base}/${hash}`,
      data: bodyFormData,
      headers: { 'Content-Type': 'multipart/form-data' },
    })
  }
}

export function newPaste(paste) {
  const {title, content, language, pass, expiry} = paste
  const bodyFormData = new FormData();
  bodyFormData.set('title', title);
  bodyFormData.set('content', content);
  bodyFormData.set('language', language);
  bodyFormData.set('password', pass);
  bodyFormData.set('expiry', parseExpiry(expiry));

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

export 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();
}

export function fmtDateStr(dateString) {
  const d = new Date(dateString)
  const options = { hour: '2-digit', minute: '2-digit', year: 'numeric', month: 'long', day: 'numeric' }
  return d.toLocaleDateString("en-US", options).toLocaleLowerCase()
}