« get me outta code hell

Use system tempfiles instead of fake temporary files - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorliam4 <towerofnix@gmail.com>2017-05-31 19:39:13 -0300
committerliam4 <towerofnix@gmail.com>2017-05-31 19:39:13 -0300
commit9c8a5ec9ccc6bdb49238551264bd18510ebcf1b7 (patch)
treef7e7b9c37f58d9becab9ff9e2906c09334a29a7a /src
parent4b1a544510f436fac951c8d161c4fbce44c42580 (diff)
Use system tempfiles instead of fake temporary files
Diffstat (limited to 'src')
-rw-r--r--src/loop-play.js13
1 files changed, 6 insertions, 7 deletions
diff --git a/src/loop-play.js b/src/loop-play.js
index 48b5790..4d8dd0a 100644
--- a/src/loop-play.js
+++ b/src/loop-play.js
@@ -1,6 +1,7 @@
 'use strict'
 
 const fs = require('fs')
+const tempy = require('tempy')
 
 const { spawn } = require('child_process')
 const { promisify } = require('util')
@@ -9,7 +10,6 @@ const sanitize = require('sanitize-filename')
 const promisifyProcess = require('./promisify-process')
 
 const writeFile = promisify(fs.writeFile)
-const unlink = promisify(fs.unlink)
 
 module.exports = async function loopPlay(fn) {
   // Looping play function. Takes one argument, the "pick" function,
@@ -28,14 +28,16 @@ module.exports = async function loopPlay(fn) {
     const [ title, href ] = picked
     console.log(`Downloading ${title}..\n${href}`)
 
-    const wavFile = `.${sanitize(title)}.wav`
+    const wavDir = tempy.directory()
+    const wavFile = wavDir + `.${sanitize(title)}.wav`
+    const downloadFile = tempy.file()
 
     const res = await fetch(href)
     const buffer = await res.buffer()
-    await writeFile('./.temp-track', buffer)
+    await writeFile(downloadFile, buffer)
 
     try {
-      await convert('./.temp-track', wavFile)
+      await convert(downloadFile, wavFile)
     } catch(err) {
       console.warn("Failed to convert " + title)
       console.warn("Selecting a new track\n")
@@ -43,8 +45,6 @@ module.exports = async function loopPlay(fn) {
       return await downloadNext()
     }
 
-    await unlink('./.temp-track')
-
     return wavFile
   }
 
@@ -53,7 +53,6 @@ module.exports = async function loopPlay(fn) {
   while (wavFile) {
     const nextPromise = downloadNext()
     await playFile(wavFile)
-    await unlink(wavFile)
     wavFile = await nextPromise
   }
 }