48 lines
1.1 KiB
JavaScript
48 lines
1.1 KiB
JavaScript
const fs = require('fs');
|
|
const request = require('request');
|
|
const _path = require("path");
|
|
|
|
//递归创建目录 同步方法
|
|
function mkdirsSync(dirname) {
|
|
//console.log(dirname);
|
|
if (fs.existsSync(dirname)) {
|
|
return true;
|
|
} else {
|
|
if (mkdirsSync(_path.dirname(dirname))) {
|
|
fs.mkdirSync(dirname);
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
function storeResource(path, url) {
|
|
return new Promise((resolve, reject) => {
|
|
mkdirsSync(_path.dirname(path));
|
|
const writeableStream = fs.createWriteStream(path);
|
|
request.get(url).pipe(writeableStream).on('close', () => {
|
|
resolve(true);
|
|
});
|
|
});
|
|
}
|
|
|
|
async function storeMusic(path, url) {
|
|
return await storeResource(path, url);
|
|
}
|
|
|
|
function storeLrc(path, content) {
|
|
return new Promise((resolve, reject) => {
|
|
mkdirsSync(_path.dirname(path));
|
|
fs.writeFileSync(path, content, { overwrite: true });
|
|
resolve(true);
|
|
});
|
|
}
|
|
|
|
async function storePic(path, url) {
|
|
return await storeResource(path, url);
|
|
}
|
|
|
|
module.exports = {
|
|
storeMusic,
|
|
storeLrc,
|
|
storePic
|
|
} |