« get me outta code hell

Open JSON playlist files in new tab - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2019-10-15 18:13:40 -0300
committerFlorrie <towerofnix@gmail.com>2019-10-15 18:13:40 -0300
commit0f1197138e3aae2d9cc25f3f1d3c6c3d7a235422 (patch)
tree55c61c843707ac91e2b696e63604c037a259c6c3
parentc0d6ea0363473cbb43e4f1f50c92803ed14742cb (diff)
Open JSON playlist files in new tab
-rw-r--r--general-util.js17
-rw-r--r--ui.js13
2 files changed, 25 insertions, 5 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)
   }
diff --git a/ui.js b/ui.js
index f334a70..368c8ea 100644
--- a/ui.js
+++ b/ui.js
@@ -223,7 +223,7 @@ class AppElement extends FocusElement {
     this.queueTimeLabel = new Label('')
     this.paneRight.addChild(this.queueTimeLabel)
 
-    this.queueListingElement.on('open', item => this.openThroughSystem(item))
+    this.queueListingElement.on('open', item => this.openSpecialOrThroughSystem(item))
     this.queueListingElement.on('queue', item => this.play(item))
     this.queueListingElement.on('remove', item => this.unqueue(item))
     this.queueListingElement.on('shuffle', () => this.shuffleQueue())
@@ -581,7 +581,7 @@ class AppElement extends FocusElement {
 
     grouplikeListing.on('browse', item => grouplikeListing.loadGrouplike(item))
     grouplikeListing.on('download', item => this.SQP.download(item))
-    grouplikeListing.on('open', item => this.openThroughSystem(item))
+    grouplikeListing.on('open', item => this.openSpecialOrThroughSystem(item))
     grouplikeListing.on('queue', (item, opts) => this.handleQueueOptions(item, opts))
 
     const updateListingsFor = item => {
@@ -797,6 +797,14 @@ class AppElement extends FocusElement {
     }
   }
 
+  openSpecialOrThroughSystem(item) {
+    if (item.url.endsWith('.json')) {
+      return this.handlePlaylistSource(item.url, true)
+    } else {
+      return this.openThroughSystem(item)
+    }
+  }
+
   openThroughSystem(item) {
     if (!isOpenable(item)) {
       return
@@ -916,6 +924,7 @@ class AppElement extends FocusElement {
         canControlQueue && isPlayable(item) && {label: 'Remove from queue', action: () => this.unqueue(item)},
         {divider: true},
 
+        isOpenable(item) && item.url.endsWith('.json') && {label: 'Open (JSON Playlist)', action: () => this.openSpecialOrThroughSystem(item)},
         isOpenable(item) && {label: 'Open (System)', action: () => this.openThroughSystem(item)},
         {divider: true},