« get me outta code hell

manual tempdir creation & handling - 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:
author(quasar) nebula <qznebula@protonmail.com>2024-05-10 21:56:29 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-05-10 21:56:29 -0300
commit27af62f38d2f0a99af5c34963d27197467fb0141 (patch)
treebb09cddc836b16db57242f6d785478bab17e343b /downloaders.js
parenta36e372ba88b59e08fa938f76b261fdc2797bef2 (diff)
manual tempdir creation & handling
This is mostly for compatibility with devices where the home
directory isn't on the same device as the system temporary
directory, so locallink and other symlink-based operations
get trolled (fail!).

This doesn't address the more general issue of e.g. playing
music off of an external drive probably fails(!!) - but in
those cases locallink isn't appropriate anyway, so they're
outta scope of this commit.
Diffstat (limited to 'downloaders.js')
-rw-r--r--downloaders.js19
1 files changed, 10 insertions, 9 deletions
diff --git a/downloaders.js b/downloaders.js
index 9e7c786..8dbdea8 100644
--- a/downloaders.js
+++ b/downloaders.js
@@ -8,9 +8,9 @@ import url from 'node:url'
 import {mkdirp} from 'mkdirp'
 import fetch from 'node-fetch'
 import sanitize from 'sanitize-filename'
-import tempy from 'tempy'
 
 import {promisifyProcess} from './general-util.js'
+import temporaryDirectory from './tempdir.js'
 
 const copyFile = (source, target) => {
   // Stolen from https://stackoverflow.com/a/30405105/4633828
@@ -110,9 +110,10 @@ downloaders.http =
       return hostname + pathname
     },
     arg => {
-      const out = (
-        tempy.directory() + '/' +
-        sanitize(decodeURIComponent(path.basename(arg))))
+      const out =
+        path.join(
+          temporaryDirectory(),
+          sanitize(decodeURIComponent(path.basename(arg))))
 
       return fetch(arg)
         .then(response => response.buffer())
@@ -124,8 +125,8 @@ downloaders.youtubedl =
   cachify('youtubedl',
     arg => (arg.match(/watch\?v=(.*)/) || ['', arg])[1],
     arg => {
-      const outDir = tempy.directory()
-      const outFile = outDir + '/%(id)s-%(uploader)s-%(title)s.%(ext)s'
+      const outDir = temporaryDirectory()
+      const outFile = path.join(outDir, '%(id)s-%(uploader)s-%(title)s.%(ext)s')
 
       const opts = [
         '--quiet',
@@ -138,7 +139,7 @@ downloaders.youtubedl =
 
       return promisifyProcess(spawn('youtube-dl', opts))
         .then(() => readdir(outDir))
-        .then(files => outDir + '/' + files[0])
+        .then(files => path.join(outDir, files[0]))
     })
 
 downloaders.local =
@@ -166,7 +167,7 @@ downloaders.local =
       // TODO: Is it necessary to sanitize here?
       // Haha, the answer to "should I sanitize" is probably always YES..
       const base = path.basename(arg, path.extname(arg))
-      const out = tempy.directory() + '/' + sanitize(base) + path.extname(arg)
+      const out = path.join(temporaryDirectory(), sanitize(base) + path.extname(arg))
 
       return copyFile(arg, out)
         .then(() => out)
@@ -180,7 +181,7 @@ downloaders.locallink =
 
       arg = removeFileProtocol(arg)
       const base = path.basename(arg, path.extname(arg))
-      const out = tempy.directory() + '/' + sanitize(base) + path.extname(arg)
+      const out = path.join(temporaryDirectory(), sanitize(base) + path.extname(arg))
 
       return symlink(path.resolve(arg), out)
         .then(() => out)