« get me outta code hell

Move cache stuff to downloaders.js - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/downloaders.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-05-29 09:59:01 -0300
committerFlorrie <towerofnix@gmail.com>2018-05-29 09:59:01 -0300
commit7c28e5c072ca27e6f2fc4a488c32718514aad924 (patch)
tree2af00ef680fd9bde46c7b9b8dc0ab9eb890d705a /downloaders.js
parentb64a14b761a38da13b81370828a2d5e9ad68c330 (diff)
Move cache stuff to downloaders.js
Diffstat (limited to 'downloaders.js')
-rw-r--r--downloaders.js18
1 files changed, 15 insertions, 3 deletions
diff --git a/downloaders.js b/downloaders.js
index 0853046..cd78fb0 100644
--- a/downloaders.js
+++ b/downloaders.js
@@ -27,7 +27,16 @@ class Downloader {
 const downloaders = {
   extension: 'mp3', // Generally target file extension
 
+  cache: {
+    http: {},
+    youtubedl: {},
+    local: {}
+  },
+
   http: arg => {
+    const cached = downloaders.cache.http[arg]
+    if (cached) return cached
+
     const out = (
       tempy.directory() + '/' +
       sanitize(decodeURIComponent(path.basename(arg))))
@@ -35,10 +44,13 @@ const downloaders = {
     return fetch(arg)
       .then(response => response.buffer())
       .then(buffer => writeFile(out, buffer))
-      .then(() => out)
+      .then(() => downloaders.cache.http[arg] = out)
   },
 
   youtubedl: arg => {
+    const cached = downloaders.cache.youtubedl[arg]
+    if (cached) return cached
+
     const out = (
       tempy.directory() + '/' + sanitize(arg) +
       '.' + downloaders.extname)
@@ -52,7 +64,7 @@ const downloaders = {
     ]
 
     return promisifyProcess(spawn('youtube-dl', opts))
-      .then(() => out)
+      .then(() => downloaders.cache.youtubedl[arg] = out)
       .catch(err => false)
   },
 
@@ -85,7 +97,7 @@ const downloaders = {
       tempy.directory() + '/' + sanitize(base) + path.extname(arg))
 
     return copyFile(arg, out)
-      .then(() => out)
+      .then(() => downloaders.cache.local[arg] = out)
   },
 
   echo: arg => arg,