package com.custom.webui.controller;
|
|
import com.custom.webui.dto.resp.Result;
|
import com.custom.webui.security.CurrentUser;
|
import com.custom.webui.service.PendingAttachmentService;
|
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.Size;
|
import lombok.Data;
|
import lombok.RequiredArgsConstructor;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.validation.annotation.Validated;
|
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RestController;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* 临时附件文本接口。
|
*
|
* <p>存在的唯一理由:SSE 是 GET 请求,参数只能走 query string,
|
* 而图片 OCR 文字达几千字时中文 URL 编码会撑爆 Tomcat 的请求行上限
|
* (默认 8KB,2000 个汉字约 18KB),得到
|
* {@code Request header is too large} 这种前端完全看不懂的 400。
|
*
|
* <p>所以先 POST 存文本换一个短 id,SSE 只带 id。
|
*/
|
@Slf4j
|
@RestController
|
@RequestMapping("/api/attachments")
|
@RequiredArgsConstructor
|
public class AttachmentController {
|
|
private final PendingAttachmentService pendingAttachmentService;
|
|
/**
|
* 暂存一段附件文本,返回可放入 query 的短 id。
|
*
|
* <p>用于图片 OCR 文字:前端识别完成后先存这里,
|
* 发送消息时只传 {@code imageTextId}。
|
*/
|
@PostMapping("/text")
|
public Result<Map<String, Object>> storeText(@Validated @RequestBody TextRequest request) {
|
String id = pendingAttachmentService.store(CurrentUser.requireUserId(), request.getText());
|
|
Map<String, Object> data = new HashMap<>();
|
data.put("id", id);
|
data.put("charCount", request.getText() == null ? 0 : request.getText().length());
|
log.debug("附件文本已暂存 userId={} id={}", CurrentUser.username(), id);
|
return Result.success(data);
|
}
|
|
/** 暂存请求体 */
|
@Data
|
public static class TextRequest {
|
|
@NotBlank(message = "文本内容不能为空")
|
@Size(max = 20000, message = "文本过长,最多 20000 字")
|
private String text;
|
}
|
}
|