mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-18 13:47:21 +08:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user