可以实现基本存储
parent
7258dd890e
commit
eafb397980
55
app/main.js
55
app/main.js
|
@ -2,11 +2,16 @@ const http = require("http");
|
|||
const url = require("url");
|
||||
const { ServerModel } = require('./model/ServerModel');
|
||||
const { SearchModel } = require('./model/SearchModel');
|
||||
const { storeMusic, storeLrc, storePic } = require('./model/StoreModel');
|
||||
const { SuccessResult, ErrorResult } = require('./model/ResultModel');
|
||||
|
||||
const { QQSource } = require('./source/QQSource');
|
||||
const { MyFreemp3QQSource } = require('./source/MyFreemp3QQSource');
|
||||
|
||||
// 防止进程中断
|
||||
// process.on('uncaughtException', function (err) {
|
||||
// console.log('Caught exception: ' + err);
|
||||
// });
|
||||
// ***********************************************************************
|
||||
// 注册源
|
||||
const searcher = new SearchModel();
|
||||
|
@ -26,7 +31,34 @@ _server.get("/api/search", async (req, res, query) => {
|
|||
//从源去进行搜索
|
||||
let result = await searcher.searchMusic(query.source_name, query.keywords);
|
||||
return new SuccessResult(result);
|
||||
})
|
||||
});
|
||||
|
||||
_server.get("/api/store", async (req, res, query) => {
|
||||
//存储文件
|
||||
let music_path = query.music_path;
|
||||
let music_url = query.music_url;
|
||||
let lrc_content = query.lrc_content;
|
||||
let pic_url = query.pic_url;
|
||||
let store_music = parseInt(query.store_music);
|
||||
let store_lrc = parseInt(query.store_lrc);
|
||||
let store_pic = parseInt(query.store_pic);
|
||||
let path_name = music_path.split('.').slice(0,-1).join('.');
|
||||
let pic_extension = pic_url.split('?')[0].split('.').pop();
|
||||
let result = '';
|
||||
if(store_music) {
|
||||
await storeMusic(music_path, music_url);
|
||||
result += '保存歌曲成功! ';
|
||||
}
|
||||
if(store_lrc) {
|
||||
await storeLrc(path_name + '.lrc', lrc_content);
|
||||
result += '保存歌词成功! ';
|
||||
}
|
||||
if(store_pic) {
|
||||
await storePic(path_name + '.' + pic_extension, pic_url);
|
||||
result += '保存封面成功! ';
|
||||
}
|
||||
return new SuccessResult(result);
|
||||
});
|
||||
|
||||
|
||||
// ***********************************************************************
|
||||
|
@ -35,15 +67,20 @@ const server = http.createServer(async (req, res) => {
|
|||
let method = req.method;
|
||||
let reqUrl = req.url;
|
||||
let reqApi = reqUrl.split('?')[0];
|
||||
console.log(method);
|
||||
console.log(method, reqUrl);
|
||||
let result;
|
||||
if(method == "GET") {
|
||||
let query = url.parse(reqUrl, true).query;
|
||||
result = await _server.trigger(method, reqApi, req, res, query, null);
|
||||
}else if(method == "POST") {
|
||||
let query = url.parse(reqUrl, true).query;
|
||||
let data = {};
|
||||
result = await _server.trigger(method, reqApi, req, res, query, data);
|
||||
try {
|
||||
if(method == "GET") {
|
||||
let query = url.parse(reqUrl, true).query;
|
||||
result = await _server.trigger(method, reqApi, req, res, query, null);
|
||||
}else if(method == "POST") {
|
||||
let query = url.parse(reqUrl, true).query;
|
||||
let data = {};
|
||||
result = await _server.trigger(method, reqApi, req, res, query, data);
|
||||
}
|
||||
}catch(err) {
|
||||
console.error(err);
|
||||
result = new ErrorResult(err);
|
||||
}
|
||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||
//res.setHeader('Access-Control-Allow-Methods', '*');
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
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
|
||||
}
|
|
@ -63,7 +63,7 @@
|
|||
<Row style="vertical-align: middle; line-height: 32px;">
|
||||
<i-col span="4">链接:</i-col>
|
||||
<i-col span="20">
|
||||
<a :href="current_item.url" target="_blank" style="margin-right: 20px;">下载歌曲</a>
|
||||
<a :href="current_item.url" target="_blank" download="download" style="margin-right: 20px;">下载歌曲</a>
|
||||
<a :href="current_item.link" target="_blank">源链接</a>
|
||||
</i-col>
|
||||
</Row>
|
||||
|
@ -79,6 +79,7 @@
|
|||
<i-input v-model="store_path" style="display: inline-block;"></i-input>
|
||||
<p style="color:#888;word-break:break-all;">{{ renderedStorePath }}</p>
|
||||
<i-button :loading="is_storing" @click="handleStore">确认存储</i-button>
|
||||
<p style="color:green;">{{ store_message }}</p>
|
||||
</p>
|
||||
</i-col>
|
||||
</Row>
|
||||
|
@ -141,6 +142,7 @@
|
|||
is_store_music: true,
|
||||
is_store_lrc: true,
|
||||
is_storing: false,
|
||||
store_message: '',
|
||||
ajax: ajax
|
||||
}
|
||||
},
|
||||
|
@ -151,12 +153,26 @@
|
|||
}
|
||||
},
|
||||
methods: {
|
||||
ajaxHandle(prms) {
|
||||
return new Promise((resolve, reject) => {
|
||||
prms.then(response => {
|
||||
if(response.data.errno == -1) {
|
||||
this.$Message.error(response.data.message);
|
||||
console.error(response.data.message);
|
||||
}else {
|
||||
resolve(response);
|
||||
}
|
||||
}).catch(err => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
},
|
||||
get(url, query) {
|
||||
if(query) url = url + '?' + qs(query);
|
||||
return this.ajax.get(url);
|
||||
return this.ajaxHandle(this.ajax.get(url));
|
||||
},
|
||||
post(url, data) {
|
||||
return this.ajax.post(url, qs(data || {}));
|
||||
return this.ajaxHandle(this.ajax.post(url, qs(data || {})));
|
||||
},
|
||||
async searches() {
|
||||
let selected_sources = this.selected_sources.map(item => item);
|
||||
|
@ -192,7 +208,6 @@
|
|||
source_name: source_name,
|
||||
keywords: keywords
|
||||
}).then(response => {
|
||||
console.log(response.data.data);
|
||||
var list = response.data.data;
|
||||
if(list instanceof Array && list.length > 0) {
|
||||
this.messages.push({status: 'success', message: source_name + '共' + list.length + '条搜索结果'});
|
||||
|
@ -203,7 +218,7 @@
|
|||
}
|
||||
}).catch(err => {
|
||||
this.messages.push({status: 'error', message: source_name + err});
|
||||
console.error(err);
|
||||
console.error(err.message);
|
||||
resolve([]);
|
||||
});
|
||||
});
|
||||
|
@ -211,14 +226,32 @@
|
|||
},
|
||||
handleStore() {
|
||||
this.is_storing = true;
|
||||
this.$Message.success('存储成功');
|
||||
this.is_storing = false;
|
||||
this.store_message = '';
|
||||
this.get('/api/store', {
|
||||
music_path: this.renderedStorePath,
|
||||
music_url: this.current_item.url,
|
||||
lrc_content: this.current_item.lrc,
|
||||
pic_url: this.current_item.pic,
|
||||
store_music: this.is_store_music + 0,
|
||||
store_lrc: this.is_store_lrc + 0,
|
||||
store_pic: this.is_store_pic + 0
|
||||
}).then(response => {
|
||||
this.store_message = response.data.message;
|
||||
this.$Message.success(response.data.message);
|
||||
this.is_storing = false;
|
||||
}).catch(err => {
|
||||
this.$Message.error(err.message);
|
||||
this.is_storing = false;
|
||||
});
|
||||
},
|
||||
getSources() {
|
||||
this.is_searching = true;
|
||||
this.get('/api/source/all').then(response => {
|
||||
this.sources = response.data.data;
|
||||
this.is_searching = false;
|
||||
}).catch(err => {
|
||||
this.$Message.error(err.message);
|
||||
this.is_searching = false;
|
||||
});
|
||||
}
|
||||
},
|
Loading…
Reference in New Issue