Files
monkey-ai-web/src/pages/WebsiteSettings/settingsApi.js
T

120 lines
3.9 KiB
JavaScript

/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { requestWebsite } from '../../helpers/websiteApi';
import { articleImageSource } from '../../components/common/ui/richTextImages';
import {
resolveImageUrl,
settingsFromConfig,
validateImageFile,
validateSettings,
} from './model';
export async function uploadWebsiteQrCode(file) {
return uploadWebsiteImage(file);
}
export async function uploadWebsiteImage(file) {
const error = validateImageFile(file);
if (error) throw new Error(error);
return uploadImage(file);
}
async function uploadImage(file) {
const body = new FormData();
body.append('file', file);
const data = await requestWebsite(
'post',
'/api/customer-service/upload',
body,
'图片上传失败,请重试',
);
if (!data?.url) throw new Error('上传接口未返回图片地址,请重试');
return resolveImageUrl(
data.url,
import.meta.env.VITE_WEBSITE_ASSET_BASE_URL || '',
);
}
export async function saveWebsiteSettings(configurationJson) {
if (typeof configurationJson !== 'string')
throw new Error('官网设置需序列化为 JSON 字符串后提交');
const payload = JSON.parse(configurationJson);
if (Object.keys(validateSettings(settingsFromConfig(payload))).length)
throw new Error('请完整填写并检查官网设置');
// Existing article editors keep local images in memory; persist URLs in the public config.
const uploaded = new Map();
for (const article of payload.insights?.articles || []) {
const images = [
...article.body.matchAll(
/<img\b[^>]*\bsrc\s*=\s*["'](data:image\/[^"']+)["'][^>]*>/gi,
),
];
for (const [, source] of images) {
if (!articleImageSource(source))
throw new Error('文章中包含无效图片,请重新插入');
if (!uploaded.has(source)) {
const [meta, encoded] = source.split(',');
const type = meta.slice(5).split(';')[0];
const bytes = Uint8Array.from(atob(encoded), (char) =>
char.charCodeAt(0),
);
const file = new File([bytes], `article.${type.split('/')[1]}`, {
type,
});
uploaded.set(source, await uploadImage(file));
}
article.body = article.body.replaceAll(
source,
uploaded.get(source).replaceAll('&', '&amp;'),
);
}
}
configurationJson = JSON.stringify(payload);
if (new TextEncoder().encode(configurationJson).byteLength > 60 * 1024)
throw new Error('官网配置不能超过 60 KiB,请精简内容或减少图片');
await requestWebsite(
'post',
'/api/website/page-config',
{ config: configurationJson },
'官网设置保存失败,请稍后重试',
);
return { saved: true, config: payload };
}
export async function getWebsiteSettings() {
const data = await requestWebsite(
'get',
'/api/website/page-config',
undefined,
'官网配置读取失败,请稍后重试',
);
if (data === null) return null;
if (typeof data !== 'string') throw new Error('官网配置响应格式不正确');
let config;
try {
config = JSON.parse(data);
} catch {
throw new Error('官网配置不是有效的 JSON,请联系管理员检查');
}
if (config === null) throw new Error('官网配置格式不正确');
settingsFromConfig(config);
return config;
}