From 051f95fee77fc764bdfb4032c1c965ad7d4b1b42 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Thu, 18 Oct 2018 18:15:33 +0300 Subject: [PATCH] Fix cross-device rename --- common/async.js | 13 ++++++++++++- download.js | 3 ++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/common/async.js b/common/async.js index 57b72c6..3f59a39 100644 --- a/common/async.js +++ b/common/async.js @@ -72,4 +72,15 @@ function askAsync (rl, q) { }) } -export default {getFiles, promiseExec, askAsync, insertDB} +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) + }) +} + +export default {getFiles, promiseExec, askAsync, insertDB, copyAsync} diff --git a/download.js b/download.js index a6767de..e55f726 100755 --- a/download.js +++ b/download.js @@ -49,7 +49,8 @@ async function download (furl) { } let fn = path.join(musicdir, filename) - await fs.rename(file.source, fn) + await asn.copyAsync(file.source, fn) + await fs.unlink(file.source) let id3 = await asn.promiseExec(`id3 -a "${clean.artist}" -t "${clean.title}" "${fn}"`)