Skip to content

Commit

Permalink
feat(media-get): support update media-get in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
foamzou committed Jul 9, 2023
1 parent b87daa5 commit f23a3fc
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 22 deletions.
41 changes: 41 additions & 0 deletions backend/src/handler/media_fetcher_lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const logger = require('consola');
const { getMediaGetInfo, getLatestMediaGetVersion, downloadTheLatestMediaGet } = require('../service/media_fetcher/media_get');

async function checkLibVersion(req, res) {
const query = req.query;

if (!['mediaGet'].includes(query.lib)) {
res.send({
status: 1,
message: "lib name is invalid",
});
return;
}

const latestVersion = await getLatestMediaGetVersion();
const mediaGetInfo = await getMediaGetInfo();

res.send({
status: 0,
data: {
mediaGetInfo,
latestVersion,
}
});
}

async function downloadTheLatestLib(req, res) {
const {version} = req.body;

const succeed = await downloadTheLatestMediaGet(version);

res.send({
status: succeed ? 0 : 1,
data: {}
});
}

module.exports = {
checkLibVersion: checkLibVersion,
downloadTheLatestLib: downloadTheLatestLib,
}
2 changes: 1 addition & 1 deletion backend/src/init_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = async function() {
logger.error('please install FFmpeg and FFprobe first');
process.exit(-1);
}
logger.info(`[media-get] ${mediaGetInfo.versionInfo}`);
logger.info(`[media-get] Version: ${mediaGetInfo.versionInfo}`);

// TODO check media-get latest version
}
4 changes: 4 additions & 0 deletions backend/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Songs = require('./handler/songs');
const SongMeta = require('./handler/song_meta');
const Playlists = require('./handler/playlists');
const Account = require('./handler/account');
const MediaFetcherLib = require('./handler/media_fetcher_lib');

const asyncWrapper = (cb) => {
return (req, res, next) => cb(req, res, next).catch(next);
Expand All @@ -27,4 +28,7 @@ router.post('/api/account', asyncWrapper(Account.set));
router.get('/api/account/qrlogin-create', asyncWrapper(Account.qrLoginCreate));
router.get('/api/account/qrlogin-check', asyncWrapper(Account.qrLoginCheck));

router.get('/api/media-fetcher-lib/version-check', asyncWrapper(MediaFetcherLib.checkLibVersion));
router.post('/api/media-fetcher-lib/update', asyncWrapper(MediaFetcherLib.downloadTheLatestLib));

module.exports = router;
104 changes: 98 additions & 6 deletions backend/src/service/media_fetcher/media_get.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,121 @@
const logger = require('consola');
const cmd = require('../../utils/cmd');
const https = require('https');
var isWin = require('os').platform().indexOf('win32') > -1;
const isLinux = require('os').platform().indexOf('linux') > -1;
const isDarwin = require('os').platform().indexOf('darwin') > -1;
const fs = require('fs');

function getBinPath() {
return `${__dirname}/../../../bin/media-get` + (isWin ? '.exe' : '');
function getBinPath(isTemp = false) {
return `${__dirname}/../../../bin/media-get` + (isTemp ? '-tmp-' : '') + (isWin ? '.exe' : '');
}

async function getMediaGetInfo() {
const {code, message} = await cmd(getBinPath(), ['-h']);
async function getMediaGetInfo(isTempBin = false) {
const {code, message} = await cmd(getBinPath(isTempBin), ['-h']);
if (code != 0) {
logger.error(`please install media-get first`);
return false;
}

const hasInstallFFmpeg = message.indexOf('FFmpeg,FFprobe: installed') > -1;
const versionInfo = message.match(/(Version:.+?)\n/);
const versionInfo = message.match(/Version:(.+?)\n/);

return {
hasInstallFFmpeg,
versionInfo: versionInfo ? versionInfo[1] : '',
versionInfo: versionInfo ? versionInfo[1].trim() : '',
}
}

function asyncHttpsGet(url) {
return new Promise((resolve) => {
https.get(url, res => {
res.on('data', data => {
resolve(data.toString());
})
res.on('error', err => {
l(err);
resolve(null);
})
});
});
}

async function getLatestMediaGetVersion() {
const latestVerisonUrl = 'https://ghproxy.com/https://raw.githubusercontent.com/foamzou/media-get/main/LATEST_VERSION';
const latestVersion = await asyncHttpsGet(latestVerisonUrl);
if (latestVersion === null || (latestVersion || "").split('.').length !== 3) {
logger.error('获取 media-get 最新版本号失败, got: ' + latestVersion);
return false;
}
return latestVersion;
}

async function downloadFile(url, filename) {
return new Promise((resolve, reject) => {
https.get(url, res => {
res.pipe(fs.createWriteStream(filename));
res.on('end', () => {
resolve();
}
);
}
);
});
}

function getMediaGetRemoteFilename(latestVersion) {
let suffix = 'win.exe';
if (isLinux) {
suffix = 'linux';
}
if (isDarwin) {
suffix = 'darwin';
}
if (process.arch === 'arm64') {
suffix += '-arm64';
}
return `https://ghproxy.com/https://github.com/foamzou/media-get/releases/download/v${latestVersion}/media-get-${latestVersion}-${suffix}`;
}

const renameFile = (oldName, newName) => {
return new Promise((resolve, reject) => {
fs.rename(oldName, newName, (err) => {
if (err) {
logger.error(err)
resolve(false);
} else {
resolve(true);
}
});
});
};

async function downloadTheLatestMediaGet(version) {
const remoteFile = getMediaGetRemoteFilename(version);
logger.info('start to download media-get: ' + remoteFile);
await downloadFile(remoteFile, getBinPath(true));
fs.chmodSync(getBinPath(true), '755');
logger.info('download finished');

const temBinInfo = await getMediaGetInfo(true)
if (!temBinInfo
|| temBinInfo.versionInfo === ""
) {
logger.error('testing new bin failed. Don`t update')
return false;
}

const renameRet = await renameFile(getBinPath(true), getBinPath());
if (!renameRet) {
logger.error('rename failed');
return false;
}
return true;
}

module.exports = {
getBinPath: getBinPath,
getMediaGetInfo: getMediaGetInfo,
getLatestMediaGetVersion: getLatestMediaGetVersion,
downloadTheLatestMediaGet: downloadTheLatestMediaGet,
}
2 changes: 1 addition & 1 deletion frontend/.env.development
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NODE_ENV = 'development'
VITE_APP_MODE = 'development'
VITE_APP_API_URL = 'http://127.0.0.1:5566/api'
VITE_APP_API_URL = 'http://172.16.9.215:5566/api'
VITE_APP_API_URL = 'http://10.0.0.2:5566/api'
VITE_APP_API_URL = 'http://127.0.0.1:5566/api'
6 changes: 6 additions & 0 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
<el-col :span="3">
<el-link type="primary" @click="account()">我的音乐账号</el-link>
</el-col>
<el-col :span="3">
<el-link type="primary" @click="setting()">设置</el-link>
</el-col>
</el-row>
</el-header>
<router-view
Expand Down Expand Up @@ -209,6 +212,9 @@ export default {
playlist() {
this.$router.push("/playlist");
},
setting() {
this.$router.push("/setting");
},
async playTheSong(metaInfo, pageUrl, suggestMatchSongId) {
console.log("------------------------");
console.log(metaInfo);
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@ export const createSyncSongWithSongIdJob = (songId) => {
"songId": songId,
"source": "netease"
});
};

export const checkMediaFetcherLib = data => get("/media-fetcher-lib/version-check", data);
export const updateMediaFetcherLib = (version) => {
return post("/media-fetcher-lib/update", {
"version": version,
});
};
12 changes: 10 additions & 2 deletions frontend/src/router/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { createRouter, createWebHistory, createWebHashHistory } from "vue-router"
import storage from "../utils/storage"

const PathPlaylist = '/playlist';
const PathSetting = '/setting';

const routes = [
{
path: '/',
Expand All @@ -12,10 +15,15 @@ const routes = [
component: () => import('../views/pc/Account.vue')
},
{
path: '/playlist',
path: PathPlaylist,
name: "Playlist",
component: () => import('../views/pc/Playlist.vue')
},
{
path: PathSetting,
name: "Setting",
component: () => import('../views/pc/Setting.vue')
},
]
export const router = createRouter({
history: createWebHashHistory(),
Expand All @@ -32,7 +40,7 @@ router.beforeEach((to, from, next) => {
if (!mk) {
next("/account");
}
if (to.path === "/playlist" && !wyAccount) {
if ([PathPlaylist, PathSetting].includes(to.path) && !wyAccount) {
next("/account");
return;
}
Expand Down
79 changes: 79 additions & 0 deletions frontend/src/views/pc/Setting.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<template>
<el-container>
<el-main>
<el-row>
<el-col :span="24">
<h2>配置页面</h2>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<h3>
当前使用的 media-get 版本号: {{ mediaGetVersion }}. 最新的版本号:
{{ latestVersion }}
</h3>
<el-button type="primary" :disabled="updating" @click="updateMediaGet">
<template v-if="!updating">更新 media-get</template>
<template v-else>更新中</template>
</el-button>
</el-col>
</el-row>
<!-- 其他配置模块 -->
</el-main>
</el-container>
</template>

<script>
import { checkMediaFetcherLib, updateMediaFetcherLib } from "../../api";
import { ElMessage } from "element-plus";
export default {
data: () => {
return {
mediaGetVersion: "查询中",
latestVersion: "查询中",
updating: false,
};
},
async mounted() {
const ret = await checkMediaFetcherLib({ lib: "mediaGet" });
if (ret !== false && ret.data) {
this.mediaGetVersion = ret.data.mediaGetInfo.versionInfo;
this.latestVersion = ret.data.latestVersion;
}
},
methods: {
async updateMediaGet() {
if (this.mediaGetVersion === this.latestVersion) {
ElMessage({
center: true,
type: "info",
message: "已经是最新版本",
});
return false;
}
this.updating = true; // Disable the button and show "更新中" text
const ret = await updateMediaFetcherLib(this.latestVersion);
if (ret === false || ret.status != 0) {
ElMessage({
center: true,
type: "error",
message: "更新失败",
});
this.updating = false; // Enable the button again
return false;
}
ElMessage({
center: true,
type: "success",
message: "更新成功",
});
this.updating = false; // Enable the button again
},
},
};
</script>
Loading

0 comments on commit f23a3fc

Please sign in to comment.