将RESP.app的备份数据转码成AnotherRedisDesktopManager的格式
将RESP.app的备份数据转码成AnotherRedisDesktopManager的格式
最近发现了AnotherRedisDesktopManager,这个软件可以直接展示proto数据。
将RESP.app导出的json文件,转码为AnotherRedisDesktopManager的ano文件(是一个list转了base64)
注意:AnotherRedisDesktopManager是没有分组的,这个工具只转了基础的功能,ip、端口、密码、颜色,复杂功能没有处理。
创建一个html文件,复制下面的代码贴进去,打开即可。
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>redis数据转码</title>
</head>
<body>
<div><h2>redis数据转码</h2><p style="color: #a8a6a6">将<a href="https://github.com/uglide/RedisDesktopManager" target="_blank">RESP.app</a>的数据转码成<a href="https://github.com/qishibo/AnotherRedisDesktopManager/" target="_blank">AnotherRedisDesktopManager</a>的格式</p><!--选择RESP.app的json文件--><input type="file" id="file"><br/><br/><button onclick="convert()">转换</button><br/><p id="result" style="color: green"></p><script>function convert() {// 获取文件(utf-8编码)let file = document.getElementById('file').files[0];// 读取文件let reader = new FileReader();reader.readAsText(file);reader.onload = function () {// 解析json(utf-8编码)if (reader.result.startsWith("\ufeff")) {reader.result = reader.result.substring(1);} else {if (reader.result.startsWith("\uFEFF")) {reader.result = reader.result.substring(1);}}let json = JSON.parse(reader.result);// 转换(转换后的格式为AnotherRedisDesktopManager的格式,一个大list)let result = [];for (let i = 0; i < json.length; i++) {//新版加了分组功能,需要判断若是有connections,则为分组,处理里面的连接,否则为连接if (json[i].connections) {for (let j = 0; j < json[i].connections.length; j++) {// 处理连接try {result.push(convertConnection(json[i].connections[j], result.length + 1));} catch (e) {console.log("解析失败", json[i].connections[j]);continue;}}} else {// 处理连接try {result.push(convertConnection(json[i], result.length + 1));} catch (e) {console.log("解析失败", json[i]);continue;}}}// console.log(result);if (result.length == 0) {alert("解析失败,请检查文件格式");return;}//将结果转为base64加密字符串let base64 = safeBtoa(JSON.stringify(result));//结果下载为 connections.ano 文件let link = document.createElement('a');link.href = "data:text/plain;charset=utf-8," + encodeURIComponent(base64);link.download = "connections.ano";link.click();//提示转换成功document.getElementById('result').innerHTML = "转换成功,请下载文件";}}/*** 处理连接,返回AnotherRedisDesktopManager的格式* @param connection*/function convertConnection(connection, order) {// 处理连接let result = {"host": connection.host,"port": connection.port,"auth": !connection.auth ? "" : connection.auth,"username": !connection.username ? "" : connection.username,"name": connection.name,"separator": connection.namespace_separator,"cluster": false,"connectionReadOnly": false,"key": generateKey(),"order": order};if (connection.icon_color) {result.color = connection.icon_color;}return result;}/*** 生成一个随机的key,时间戳_5位随机字符串*/function generateKey() {return Date.now() + "_" + Math.random().toString(36).substring(2, 7);}/*** 安全地将字符串编码为 Base64(支持 Unicode)* @param {string} str - 要编码的字符串* @returns {string} - Base64 编码后的字符串*/function safeBtoa(str) {// 先将Unicode字符转为UTF-8编码的二进制字符串const utf8Str = unescape(encodeURIComponent(str));// 再转为Base64return btoa(utf8Str);}</script>
</div></body>
</html>