« get me outta code hell

Generally optimize downloads (less file copying) - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/downloaders.js
diff options
context:
space:
mode:
authorliam4 <towerofnix@gmail.com>2017-06-04 17:09:47 -0300
committerliam4 <towerofnix@gmail.com>2017-06-04 17:09:47 -0300
commit170b9091f662e082a410128644b63ae787747c94 (patch)
tree402ba316240ec95dd13e2e2f58290d4a96a25718 /src/downloaders.js
parent9fb775446f151b492f17bd42b757b958f5ea3fa0 (diff)
Generally optimize downloads (less file copying)
Basically all we do is let downloaders specify the output file,
rather than be forced to download or copy into a specific given
file. Since avconv/convert automatically gets us the displayed
file name we want anyways (shown in play), this doesn't change
anything visible to the user, but does make things faster.
Diffstat (limited to 'src/downloaders.js')
-rw-r--r--src/downloaders.js23
1 files changed, 10 insertions, 13 deletions
diff --git a/src/downloaders.js b/src/downloaders.js
index 3f3a427..5f65346 100644
--- a/src/downloaders.js
+++ b/src/downloaders.js
@@ -7,18 +7,20 @@ const { spawn } = require('child_process')
 const { promisify } = require('util')
 
 const writeFile = promisify(fs.writeFile)
-const rename = promisify(fs.rename)
 
 function makeHTTPDownloader() {
-  return function(arg, out) {
+  return function(arg) {
+    const out = tempy.file()
+
     return fetch(arg)
       .then(response => response.buffer())
       .then(buffer => writeFile(out, buffer))
+      .then(() => out)
   }
 }
 
 function makeYouTubeDownloader() {
-  return function(arg, out) {
+  return function(arg) {
     const tempDir = tempy.directory()
 
     const opts = [
@@ -29,20 +31,15 @@ function makeYouTubeDownloader() {
     ]
 
     return promisifyProcess(spawn('youtube-dl', opts), false)
-      .then(() => rename(tempDir + '/dl.wav', out))
+      .then(() => tempDir + '/dl.wav')
   }
 }
 
 function makeLocalDownloader() {
-  return function(arg, out) {
-    const read = fs.createReadStream(arg)
-    const write = fs.createWriteStream(out)
-
-    return new Promise((resolve, reject) => {
-      write.on('error', err => reject(err))
-      write.on('close', () => resolve())
-      read.pipe(write)
-    })
+  return function(arg) {
+    // Since we're grabbing the file from the local file system, there's no
+    // need to download or copy it!
+    return arg
   }
 }