feat[Go] implement api/v1/thumbnails API (#15416)

### What problem does this PR solve?

As title

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
- [x] New Feature (non-breaking change which adds functionality
This commit is contained in:
Haruko386
2026-06-01 11:22:08 +08:00
committed by GitHub
parent da1ed6f0e7
commit 2d7044b57e
3 changed files with 100 additions and 2 deletions

View File

@@ -18,8 +18,11 @@ package handler
import (
"encoding/json"
"errors"
"fmt"
"mime"
"net/http"
"path/filepath"
"ragflow/internal/common"
"ragflow/internal/entity"
"strconv"
@@ -31,6 +34,8 @@ import (
"ragflow/internal/service"
)
var IMG_BASE64_PREFIX = "data:image/png;base64,"
// DocumentHandler document handler
type DocumentHandler struct {
documentService *service.DocumentService
@@ -120,6 +125,58 @@ func (h *DocumentHandler) GetDocumentByID(c *gin.Context) {
})
}
// GetThumbnail Get thumbnails for documents.
func (h *DocumentHandler) GetThumbnail(c *gin.Context) {
_, errorCode, errorMessage := GetUser(c)
if errorCode != common.CodeSuccess {
jsonError(c, errorCode, errorMessage)
return
}
id := c.Query("doc_ids")
if id == "" {
c.JSON(http.StatusBadRequest, gin.H{
"error": errors.New("invalid document id"),
})
return
}
result, err := h.documentService.GetThumbnail(id)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{
"error": fmt.Errorf("thumbnail not found"),
})
return
}
if result.Thumbnail != nil && *result.Thumbnail != "" {
newThumbURL := fmt.Sprintf("/api/v1/documents/images/%s-%s", result.KbID, *result.Thumbnail)
result.Thumbnail = &newThumbURL
}
c.JSON(http.StatusOK, gin.H{
"code": common.CodeSuccess,
"data": map[string]interface{}{result.ID: result.Thumbnail},
"message": "success",
})
}
// GetDocumentImage returns a document image from object storage.
func (h *DocumentHandler) GetDocumentImage(c *gin.Context) {
imageID := c.Param("image_id")
data, err := h.documentService.GetDocumentImage(imageID)
if err != nil {
jsonError(c, common.CodeDataError, "Image not found.")
return
}
contentType := mime.TypeByExtension(strings.ToLower(filepath.Ext(imageID)))
if contentType == "" {
contentType = "image/JPEG"
}
c.Data(http.StatusOK, contentType, data)
}
// UpdateDocument update document
// @Summary Update Document
// @Description Update document info