-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(media-get): support update media-get in UI
- Loading branch information
Showing
10 changed files
with
260 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.