diff options
Diffstat (limited to 'downloaders.js')
-rw-r--r-- | downloaders.js | 94 |
1 files changed, 47 insertions, 47 deletions
diff --git a/downloaders.js b/downloaders.js index 941c805..9e7c786 100644 --- a/downloaders.js +++ b/downloaders.js @@ -1,25 +1,21 @@ -const { promisifyProcess } = require('./general-util') -const { promisify } = require('util') -const { spawn } = require('child_process') -const { URL } = require('url') -const mkdirp = promisify(require('mkdirp')) -const fs = require('fs') -const fetch = require('node-fetch') -const tempy = require('tempy') -const os = require('os') -const path = require('path') -const sanitize = require('sanitize-filename') - -const writeFile = promisify(fs.writeFile) -const rename = promisify(fs.rename) -const stat = promisify(fs.stat) -const readdir = promisify(fs.readdir) -const symlink = promisify(fs.symlink) +import {spawn} from 'node:child_process' +import {createReadStream, createWriteStream} from 'node:fs' +import {readdir, rename, stat, symlink, writeFile} from 'node:fs/promises' +import os from 'node:os' +import path from 'node:path' +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' const copyFile = (source, target) => { // Stolen from https://stackoverflow.com/a/30405105/4633828 - const rd = fs.createReadStream(source) - const wr = fs.createWriteStream(target) + const rd = createReadStream(source) + const wr = createWriteStream(target) return new Promise((resolve, reject) => { rd.on('error', reject) wr.on('error', reject) @@ -32,7 +28,7 @@ const copyFile = (source, target) => { }) } -// const disableBackResolving = arg => arg.split('/').map(str => str.replace(/^\../, '_..')).join('/') +export const rootCacheDir = path.join(os.homedir(), '.mtui', 'downloads') const cachify = (identifier, keyFunction, baseFunction) => { return async arg => { @@ -43,7 +39,7 @@ const cachify = (identifier, keyFunction, baseFunction) => { // Determine where the final file will end up. This is just a directory - // the file's own name is determined by the downloader. - const cacheDir = downloaders.rootCacheDir + '/' + identifier + const cacheDir = rootCacheDir + '/' + identifier const finalDirectory = cacheDir + '/' + sanitize(keyFunction(arg)) // Check if that directory only exists. If it does, return the file in it, @@ -102,15 +98,16 @@ const removeFileProtocol = arg => { } } -const downloaders = { - extension: 'mp3', // Generally target file extension, used by youtube-dl +// Generally target file extension, used by youtube-dl +export const extension = 'mp3' - rootCacheDir: os.homedir() + '/.mtui/downloads', +const downloaders = {} - http: cachify('http', +downloaders.http = + cachify('http', arg => { - const url = new URL(arg) - return url.hostname + url.pathname + const {hostname, pathname} = new url.URL(arg) + return hostname + pathname }, arg => { const out = ( @@ -121,9 +118,10 @@ const downloaders = { .then(response => response.buffer()) .then(buffer => writeFile(out, buffer)) .then(() => out) - }), + }) - youtubedl: cachify('youtubedl', +downloaders.youtubedl = + cachify('youtubedl', arg => (arg.match(/watch\?v=(.*)/) || ['', arg])[1], arg => { const outDir = tempy.directory() @@ -133,7 +131,7 @@ const downloaders = { '--quiet', '--no-warnings', '--extract-audio', - '--audio-format', downloaders.extension, + '--audio-format', extension, '--output', outFile, arg ] @@ -141,9 +139,10 @@ const downloaders = { return promisifyProcess(spawn('youtube-dl', opts)) .then(() => readdir(outDir)) .then(files => outDir + '/' + files[0]) - }), + }) - local: cachify('local', +downloaders.local = + cachify('local', arg => arg, arg => { // Usually we'd just return the given argument in a local @@ -171,9 +170,10 @@ const downloaders = { return copyFile(arg, out) .then(() => out) - }), + }) - locallink: cachify('locallink', +downloaders.locallink = + cachify('locallink', arg => arg, arg => { // Like the local downloader, but creates a symbolic link to the argument. @@ -184,22 +184,22 @@ const downloaders = { return symlink(path.resolve(arg), out) .then(() => out) - }), + }) - echo: arg => arg, +downloaders.echo = + arg => arg - getDownloaderFor: arg => { - if (arg.startsWith('http://') || arg.startsWith('https://')) { - if (arg.includes('youtube.com')) { - return downloaders.youtubedl - } else { - return downloaders.http - } +export default downloaders + +export function getDownloaderFor(arg) { + if (arg.startsWith('http://') || arg.startsWith('https://')) { + if (arg.includes('youtube.com')) { + return downloaders.youtubedl } else { - // return downloaders.local - return downloaders.locallink + return downloaders.http } + } else { + // return downloaders.local + return downloaders.locallink } } - -module.exports = downloaders |