139 lines
3.2 KiB
JavaScript
139 lines
3.2 KiB
JavaScript
import {exec} from 'child_process'
|
|
import path from 'path'
|
|
import fs from 'fs-extra'
|
|
import url from 'url'
|
|
import qs from 'querystring'
|
|
|
|
function filewalker (dir, done) {
|
|
let results = []
|
|
|
|
fs.readdir(dir, function (err, list) {
|
|
if (err) return done(err)
|
|
|
|
let pending = list.length
|
|
|
|
if (!pending) return done(null, results)
|
|
|
|
list.forEach(function (file) {
|
|
file = path.resolve(dir, file)
|
|
|
|
fs.stat(file, function (err, stat) {
|
|
if (err) return done(err)
|
|
|
|
// If directory, execute a recursive call
|
|
if (stat && stat.isDirectory()) {
|
|
filewalker(file, function (err, res) {
|
|
if (err) return done(err)
|
|
results = results.concat(res)
|
|
if (!--pending) done(null, results)
|
|
})
|
|
} else {
|
|
results.push(file)
|
|
|
|
if (!--pending) done(null, results)
|
|
}
|
|
})
|
|
})
|
|
})
|
|
}
|
|
|
|
async function insertDB (db, track) {
|
|
let ensure = await db.get('SELECT * FROM Track WHERE title=? AND artist=? AND album=?', track.title, track.artist, track.album || '')
|
|
if (ensure) {
|
|
return null
|
|
}
|
|
|
|
await db.run('INSERT INTO Track VALUES (NULL,?,?,?,?,?,?,?,?)',
|
|
[track.title, track.artist, track.file, track.album || '', track.genre || null, track.track || null,
|
|
track.year || null, Math.floor(track.duration)])
|
|
|
|
return track
|
|
}
|
|
|
|
function getFiles (dir) {
|
|
return new Promise((resolve, reject) => {
|
|
filewalker(dir, (err, files) => {
|
|
if (err) return reject(err)
|
|
resolve(files)
|
|
})
|
|
})
|
|
}
|
|
|
|
function promiseExec (cmd, opts) {
|
|
return new Promise((resolve, reject) => {
|
|
exec(cmd, opts, function (err, stdout, stderr) {
|
|
if (err) return reject(err)
|
|
resolve({stdout, stderr})
|
|
})
|
|
})
|
|
}
|
|
|
|
function askAsync (rl, q) {
|
|
return new Promise((resolve, reject) => {
|
|
rl.question(q, resolve)
|
|
})
|
|
}
|
|
|
|
function copyAsync (fsrc, fdst) {
|
|
return new Promise((resolve, reject) => {
|
|
let source = fs.createReadStream(path.resolve(fsrc))
|
|
let dest = fs.createWriteStream(path.resolve(fdst))
|
|
|
|
source.pipe(dest)
|
|
source.on('end', resolve)
|
|
source.on('error', reject)
|
|
})
|
|
}
|
|
|
|
function HTTP_GET (link, headers = {}) {
|
|
let parsed = url.parse(link)
|
|
let opts = {
|
|
host: parsed.hostname,
|
|
port: parsed.port,
|
|
path: parsed.path,
|
|
headers: {
|
|
'Accept': '*/*',
|
|
'Accept-Language': 'en-US'
|
|
}
|
|
}
|
|
|
|
if (headers) {
|
|
opts.headers = Object.assign(opts.headers, headers)
|
|
}
|
|
|
|
let reqTimeOut
|
|
|
|
let httpModule = parsed.protocol === 'https:' ? require('https') : require('http')
|
|
return new Promise((resolve, reject) => {
|
|
let req = httpModule.get(opts, (res) => {
|
|
if (res.statusCode === 302 || res.statusCode === 301) {
|
|
return reject(new Error('Invalid endpoint'))
|
|
}
|
|
|
|
let data = ''
|
|
|
|
reqTimeOut = setTimeout(() => {
|
|
req.abort()
|
|
data = null
|
|
reject(new Error('Request took too long!'))
|
|
}, 5000)
|
|
|
|
res.on('data', (chunk) => {
|
|
data += chunk
|
|
})
|
|
|
|
res.on('end', () => {
|
|
clearTimeout(reqTimeOut)
|
|
|
|
resolve(data)
|
|
})
|
|
}).on('error', (e) => {
|
|
reject(new Error(e.message))
|
|
})
|
|
|
|
req.setTimeout(10000)
|
|
})
|
|
}
|
|
|
|
export default {getFiles, promiseExec, askAsync, insertDB, copyAsync, GET: HTTP_GET}
|