« get me outta code hell

mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/general-util.js
diff options
context:
space:
mode:
Diffstat (limited to 'general-util.js')
-rw-r--r--general-util.js17
1 files changed, 14 insertions, 3 deletions
diff --git a/general-util.js b/general-util.js
index a7bfb11..3aa4180 100644
--- a/general-util.js
+++ b/general-util.js
@@ -3,6 +3,7 @@ const { promisify } = require('util')
 const fetch = require('node-fetch')
 const fs = require('fs')
 const npmCommandExists = require('command-exists')
+const url = require('url')
 
 const readFile = promisify(fs.readFile)
 
@@ -61,9 +62,19 @@ function downloadPlaylistFromLocalPath(path) {
 }
 
 module.exports.downloadPlaylistFromOptionValue = function(arg) {
-  // TODO: Verify things!
-  if (arg.startsWith('http://') || arg.startsWith('https://')) {
-    return downloadPlaylistFromURL(arg)
+  let argURL
+  try {
+    argURL = new url.URL(arg)
+  } catch (err) {
+    // Definitely not a URL.
+  }
+
+  if (argURL) {
+    if (argURL.protocol === 'http:' || argURL.protocol === 'https:') {
+      return downloadPlaylistFromURL(arg)
+    } else if (argURL.protocol === 'file:') {
+      return downloadPlaylistFromLocalPath(url.fileURLToPath(argURL))
+    }
   } else {
     return downloadPlaylistFromLocalPath(arg)
   }