批量修改文章和其他地方的图片链接 #6465
Answered
by
guqing
junyangfan
asked this question in
Q&A
批量修改文章和其他地方的图片链接
#6465
-
目前我使用的halo版本是2.x,使用postgres数据库,我想使用相关命令批量修改文章和其他地方的图片链接(因图片域名变更),看现在halo的设计模式,只有一张相关表,而且没办法执行sql语句,请求我该怎么解决这个问题呢? |
Beta Was this translation helpful? Give feedback.
Answered by
guqing
Aug 26, 2024
Replies: 2 comments 2 replies
-
目前你可以考虑通过自动生成的 API( 这里建议通过插件的方式来实现。 |
Beta Was this translation helpful? Give feedback.
2 replies
-
const fs = require('fs');
function decodeBase64(base64String) {
return decodeURIComponent(escape(atob(base64String)));
}
function encodeBase64(str) {
return btoa(unescape(encodeURIComponent(str)));
}
function replaceInJsonData(jsonArray, searchString, replaceString) {
jsonArray.forEach(item => {
if (item.data) {
const decodedData = decodeBase64(item.data);
const replacedData = decodedData.replace(new RegExp(searchString, 'g'), replaceString);
item.data = encodeBase64(replacedData);
}
});
return jsonArray;
}
function processJsonFile(inputFilePath, outputFilePath, searchString, replaceString) {
fs.readFile(inputFilePath, 'utf8', (err, data) => {
if (err) {
console.error('读取文件时出错:', err);
return;
}
try {
const jsonData = JSON.parse(data);
const updatedJsonData = replaceInJsonData(jsonData, searchString, replaceString);
fs.writeFile(outputFilePath, JSON.stringify(updatedJsonData, null, 2), (err) => {
if (err) {
console.error('写入文件时出错:', err);
} else {
console.log('文件已成功更新');
}
});
} catch (error) {
console.error('解析 JSON 数据时出错:', error);
}
});
}
// 输入文件路径
const inputFilePath = '/Users/guqing/Downloads/20240902164220-backup-UJrBp/extensions.data';
// 输出文件路径,执行成功后将文件名改为 extensions.data
const outputFilePath = '/Users/guqing/Downloads/20240902164220-backup-UJrBp/extensions-out.data';
// 要查找的字符串
const searchString = 'https://guqing.io';
// 要替换为的字符串
const replaceString = 'https://ryanc.cc';
processJsonFile(inputFilePath, outputFilePath, searchString, replaceString); 修改注释部分,然后执行
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
guqing
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
还有个办法就是从 console 导出一个备份然后解压