« get me outta code hell

downloaders.js - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/downloaders.js
blob: 0054ca7e2dcab7dbce79c912a403b356f0d0d23e (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
const removeFileProtocol = arg => {
  const fileProto = 'file://'
  if (arg.startsWith(fileProto)) {
    return decodeURIComponent(arg.slice(fileProto.length))
  } else {
    return arg
  }
}

const downloaders = {
  fetch: arg => {
    return fetch(arg)
      .then(response => response.blob())
      .then(blob => URL.createObjectURL(blob))
  },

  getDownloaderFor: arg => {
    if (arg.startsWith('http://') || arg.startsWith('https://')) {
      return downloaders.fetch
    } else {
      return null
    }
  }
}

module.exports = downloaders