2901-zustend для стора. сохранение черновиков построчно

редактор xml пока не работает, но есть
ui переработал
This commit is contained in:
2026-01-29 10:58:58 +03:00
parent b99e328d35
commit 4da5fdd130
23 changed files with 2391 additions and 1384 deletions

View File

@@ -2,6 +2,7 @@ package handlers
import (
"fmt"
"io"
"net/http"
"time"
@@ -11,15 +12,20 @@ import (
"go.uber.org/zap"
"rmser/internal/services/drafts"
"rmser/internal/services/ocr"
"rmser/pkg/logger"
)
type DraftsHandler struct {
service *drafts.Service
service *drafts.Service
ocrService *ocr.Service
}
func NewDraftsHandler(service *drafts.Service) *DraftsHandler {
return &DraftsHandler{service: service}
func NewDraftsHandler(service *drafts.Service, ocrService *ocr.Service) *DraftsHandler {
return &DraftsHandler{
service: service,
ocrService: ocrService,
}
}
func (h *DraftsHandler) GetDraft(c *gin.Context) {
@@ -354,3 +360,54 @@ func (h *DraftsHandler) ReorderItems(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"success": true})
}
// Upload обрабатывает загрузку файла и прогоняет через OCR
func (h *DraftsHandler) Upload(c *gin.Context) {
// Лимит размера тела запроса (20MB)
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 20<<20)
// Получаем файл из формы
fileHeader, err := c.FormFile("file")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "file is required"})
return
}
// Открываем файл для чтения
file, err := fileHeader.Open()
if err != nil {
logger.Log.Error("Failed to open uploaded file", zap.Error(err))
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to open file"})
return
}
defer file.Close()
// Читаем байты файла
fileBytes, err := io.ReadAll(file)
if err != nil {
logger.Log.Error("Failed to read file bytes", zap.Error(err))
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to read file"})
return
}
// Получаем userID из контекста
userID := c.MustGet("userID").(uuid.UUID)
// Вызываем ProcessDocument
draft, err := h.ocrService.ProcessDocument(c.Request.Context(), userID, fileBytes, fileHeader.Filename)
if err != nil {
// Если черновик создан, но произошла ошибка OCR, возвращаем черновик со статусом ERROR
// Проверяем, что draft не nil (черновик был создан)
if draft != nil {
c.JSON(http.StatusOK, draft)
return
}
// Если черновик не был создан, возвращаем ошибку
logger.Log.Error("ProcessDocument failed", zap.Error(err))
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Успешная обработка
c.JSON(http.StatusOK, draft)
}