將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>