package com.custom.webui.service;
import com.custom.webui.exception.BusinessException;
/**
* 会话导出服务:把一个对话导出成 JSON / TXT / PDF。
*
*
三者的定位不同,不是同一份内容换三种后缀:
*
* - JSON —— 给程序用的。除当前显示的分支外,还带上扁平的全量消息
* (含 parentId),因此**分支结构不会丢**,可以完整还原消息树。
* - TXT —— 给人看的纯文本,随便什么工具都能打开、能 grep。
* - PDF —— 用于归档、打印、发给别人。中文字体按子集嵌入,
* 在任何阅读器里都能正确显示。
*
*/
public interface ChatExportService {
/** 导出格式 */
enum Format {
JSON, TXT, PDF;
/** 解析格式参数,不认识的值直接报错而不是悄悄按默认值处理 */
public static Format parse(String raw) {
if (raw == null || raw.isBlank()) {
return TXT;
}
for (Format f : values()) {
if (f.name().equalsIgnoreCase(raw.trim())) {
return f;
}
}
throw BusinessException.badRequest(
"不支持的导出格式:" + raw + "(可选 json / txt / pdf)");
}
public String extension() {
return name().toLowerCase();
}
public String contentType() {
return switch (this) {
case JSON -> "application/json;charset=UTF-8";
case TXT -> "text/plain;charset=UTF-8";
case PDF -> "application/pdf";
};
}
}
/**
* 导出结果。
*
* @param data 文件字节
* @param filename 建议的文件名(已做非法字符清洗)
* @param contentType MIME 类型
*/
record ExportFile(byte[] data, String filename, String contentType) {
}
/**
* 导出一个会话。
*
* @throws com.custom.webui.exception.BusinessException 会话不存在或不属于该用户(404)
*/
ExportFile export(Long userId, Long chatId, Format format);
}