Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

add audit hooks to file APIs (#21807)

Jin Hai12d9a38e24bf
diff --git a/build.sh b/build.sh
index c23129f6bf..f0b54f7df1 100755
--- a/build.sh
+++ b/build.sh
@@ -25,8 +25,10 @@ RAGFLOW_CLI_BINARY="$PROJECT_ROOT/bin/ragflow-cli"
# Strip symbols from Go binaries (set via --strip / -s)
STRIP_SYMBOLS=""
-# Extra Go build tags appended to every build (comma separated). This tree has
-# no enterprise edition, so 'ee' is rejected below rather than forwarded.
+# Extra Go build tags appended to every build (comma separated). This tree
+# carries the enterprise sources, so RAGFLOW_GO_TAGS=ee selects them through the
+# //go:build ee constraints; without it the same command lines build the
+# community edition.
EXTRA_GO_TAGS="${RAGFLOW_GO_TAGS:-}"
GO_BUILD_TAGS="cgo,static,sonic"
[ -n "$EXTRA_GO_TAGS" ] && GO_BUILD_TAGS="$GO_BUILD_TAGS,$EXTRA_GO_TAGS"
diff --git a/internal/handler/file.go b/internal/handler/file.go
index 305999c858..19c05af749 100644
--- a/internal/handler/file.go
+++ b/internal/handler/file.go
@@ -26,6 +26,7 @@ import (
"ragflow/internal/utility"
"strconv"
"strings"
+ "time"
"github.com/gin-gonic/gin"
@@ -232,9 +233,24 @@ type CreateFolderRequest struct {
// @Failure 400 {object} map[string]interface{}
// @Router /v1/file/upload [post]
func (h *FileHandler) UploadFile(c *gin.Context) {
+ startAt := time.Now()
+ operationLog := &common.OperationLog{
+ EventTime: startAt,
+ Operation: "upload_file",
+ APIPath: c.FullPath(),
+ HTTPMethod: c.Request.Method,
+ IPAddress: c.ClientIP(),
+ }
+ defer func() {
+ operationLog.DurationMS = time.Since(startAt).Milliseconds()
+ common.Audit(operationLog)
+ }()
+
user, errorCode, errorMessage := GetUser(c)
if errorCode != common.CodeSuccess {
common.ErrorWithCode(c, errorCode, errorMessage)
+ operationLog.Code = int(common.CodeBadRequest)
+ operationLog.Error = errors.New(errorMessage)
return
}
@@ -248,16 +264,22 @@ func (h *FileHandler) UploadFile(c *gin.Context) {
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, uploadLimit)
if cl := c.Request.ContentLength; cl > uploadLimit {
common.ResponseWithCodeData(c, common.CodeBadRequest, nil, "request body exceeds deployment upload limit")
+ operationLog.Code = int(common.CodeBadRequest)
+ operationLog.Error = errors.New("request body exceeds deployment upload limit")
return
}
if err := c.Request.ParseMultipartForm(32 << 20); err != nil {
common.ResponseWithCodeData(c, common.CodeBadRequest, nil, "Failed to parse multipart form: "+err.Error())
+ operationLog.Code = int(common.CodeBadRequest)
+ operationLog.Error = fmt.Errorf("failed to parse multipart form: %w", err)
return
}
form := c.Request.MultipartForm
if form == nil {
common.ResponseWithCodeData(c, common.CodeBadRequest, nil, "No file part!")
+ operationLog.Code = int(common.CodeBadRequest)
+ operationLog.Error = errors.New("no file part!")
return
}
var totalSize int64
@@ -278,6 +300,8 @@ func (h *FileHandler) UploadFile(c *gin.Context) {
rootFolder, err := h.fileService.GetRootFolder(ctx, tenantID)
if err != nil {
respondFileServiceError(c, err)
+ operationLog.Code = int(common.CodeBadRequest)
+ operationLog.Error = err
return
}
parentID = rootFolder["id"].(string)
@@ -286,23 +310,28 @@ func (h *FileHandler) UploadFile(c *gin.Context) {
files := form.File["file"]
if len(files) == 0 {
common.ResponseWithCodeData(c, common.CodeBadRequest, nil, "No file selected!")
+ operationLog.Code = int(common.CodeBadRequest)
+ operationLog.Error = errors.New("no file selected")
return
}
for _, fileHeader := range files {
if fileHeader.Filename == "" {
common.ResponseWithCodeData(c, common.CodeBadRequest, nil, "No file selected!")
+ operationLog.Code = int(common.CodeBadRequest)
+ operationLog.Error = errors.New("no file selected")
return
}
}
- ctx := c.Request.Context()
result, err := h.fileService.UploadFile(ctx, userID, parentID, files, uploadLimit)
if err != nil {
if respondPermissionErrorIf(c, err, true) {
return
}
common.ErrorWithCode(c, common.CodeBadRequest, err.Error())
+ operationLog.Code = int(common.CodeBadRequest)
+ operationLog.Error = err
return
}
@@ -326,6 +355,8 @@ func (h *FileHandler) UploadFile(c *gin.Context) {
rootFolder, err := h.fileService.GetRootFolder(ctx, tenantID)
if err != nil {
respondFileServiceError(c, err)
+ operationLog.Code = int(common.CodeBadRequest)
+ operationLog.Error = err
return
}
parentID = rootFolder["id"].(string)
@@ -337,6 +368,8 @@ func (h *FileHandler) UploadFile(c *gin.Context) {
return
}
common.ErrorWithCode(c, common.CodeBadRequest, err.Error())
+ operationLog.Code = int(common.CodeBadRequest)
+ operationLog.Error = err
return
}
@@ -362,15 +395,32 @@ type DeleteFileRequest struct {
// @Success 200 {object} map[string]interface{}
// @Router /api/v1/files [delete]
func (h *FileHandler) DeleteFiles(c *gin.Context) {
+ startAt := time.Now()
+ operationLog := &common.OperationLog{
+ EventTime: startAt,
+ Operation: "delete_files",
+ APIPath: c.FullPath(),
+ HTTPMethod: c.Request.Method,
+ IPAddress: c.ClientIP(),
+ }
+ defer func() {
+ operationLog.DurationMS = time.Since(startAt).Milliseconds()
+ common.Audit(operationLog)
+ }()
+
user, errorCode, errorMessage := GetUser(c)
if errorCode != common.CodeSuccess {
common.ErrorWithCode(c, errorCode, errorMessage)
+ operationLog.Code = int(errorCode)
+ operationLog.Error = errors.New(errorMessage)
return
}
var req DeleteFileRequest
if err := c.ShouldBindJSON(&req); err != nil {
common.ErrorWithCode(c, common.CodeBadRequest, err.Error())
+ operationLog.Code = int(common.CodeBadRequest)
+ operationLog.Error = err
return
}
@@ -379,6 +429,8 @@ func (h *FileHandler) DeleteFiles(c *gin.Context) {
return
}
common.ResponseWithCodeData(c, common.CodeBadRequest, nil, err.Error())
+ operationLog.Code = int(common.CodeBadRequest)
+ operationLog.Error = err
return
}
@@ -406,27 +458,48 @@ type MoveFileRequest struct {
// @Success 200 {object} map[string]interface{}
// @Router /api/v1/files/move [post]
func (h *FileHandler) MoveFiles(c *gin.Context) {
+ startAt := time.Now()
+ operationLog := &common.OperationLog{
+ EventTime: startAt,
+ Operation: "move_files",
+ APIPath: c.FullPath(),
+ HTTPMethod: c.Request.Method,
+ IPAddress: c.ClientIP(),
+ }
+ defer func() {
+ operationLog.DurationMS = time.Since(startAt).Milliseconds()
+ common.Audit(operationLog)
+ }()
+
user, errorCode, errorMessage := GetUser(c)
if errorCode != common.CodeSuccess {
common.ErrorWithCode(c, errorCode, errorMessage)
+ operationLog.Code = int(errorCode)
+ operationLog.Error = errors.New(errorMessage)
return
}
var req MoveFileRequest
if err := c.ShouldBindJSON(&req); err != nil {
common.ErrorWithCode(c, common.CodeBadRequest, err.Error())
+ operationLog.Code = int(common.CodeBadRequest)
+ operationLog.Error = err
return
}
// Validate: at least one of dest_file_id or new_name must be provided
if req.DestFileID == "" && req.NewName == "" {
- common.ResponseWithCodeData(c, common.CodeParamError, nil, "At least one of dest_file_id or new_name must be provided")
+ common.ResponseWithCodeData(c, common.CodeParamError, nil, "at least one of dest_file_id or new_name must be provided")
+ operationLog.Code = int(common.CodeParamError)
+ operationLog.Error = errors.New("at least one of dest_file_id or new_name must be provided")
return
}
// Validate: new_name can only be used with a single file
if req.NewName != "" && len(req.SrcFileIDs) > 1 {
common.ResponseWithCodeData(c, common.CodeParamError, nil, "new_name can only be used with a single file")
+ operationLog.Code = int(common.CodeParamError)
+ operationLog.Error = errors.New("new_name can only be used with a single file")
return
}
@@ -436,6 +509,8 @@ func (h *FileHandler) MoveFiles(c *gin.Context) {
return
}
common.ResponseWithCodeData(c, common.CodeBadRequest, nil, err.Error())
+ operationLog.Code = int(common.CodeBadRequest)
+ operationLog.Error = err
return
}
@@ -452,9 +527,23 @@ func (h *FileHandler) MoveFiles(c *gin.Context) {
// @Success 200 {file} binary "File stream"
// @Router /api/v1/files/{file_id} [get]
func (h *FileHandler) Download(c *gin.Context) {
+ startAt := time.Now()
+ operationLog := &common.OperationLog{
+ EventTime: startAt,
+ Operation: "download_file",
+ APIPath: c.FullPath(),
+ HTTPMethod: c.Request.Method,
+ IPAddress: c.ClientIP(),
+ }
+ defer func() {
+ operationLog.DurationMS = time.Since(startAt).Milliseconds()
+ common.Audit(operationLog)
+ }()
user, errorCode, errorMessage := GetUser(c)
if errorCode != common.CodeSuccess {
common.ErrorWithCode(c, errorCode, errorMessage)
+ operationLog.Code = int(errorCode)
+ operationLog.Error = errors.New(errorMessage)
return
}
userID := user.ID
@@ -462,17 +551,23 @@ func (h *FileHandler) Download(c *gin.Context) {
fileID := c.Param("id")
if fileID == "" {
common.ResponseWithCodeData(c, common.CodeParamError, nil, "id is required")
+ operationLog.Code = int(common.CodeParamError)
+ operationLog.Error = errors.New("id is required")
return
}
ctx := c.Request.Context()
// Get file metadata and check permission
- file, err := h.fileService.GetFileContent(ctx, userID, fileID)
+ fileContent, err := h.fileService.GetFileContent(ctx, userID, fileID)
if err != nil {
if respondPermissionErrorIf(c, err, true) {
+ operationLog.Code = int(common.CodePermissionError)
+ operationLog.Error = err
return
}
common.ResponseWithCodeData(c, common.CodeUnauthorized, nil, err.Error())
+ operationLog.Code = int(common.CodeUnauthorized)
+ operationLog.Error = err
return
}
@@ -480,22 +575,26 @@ func (h *FileHandler) Download(c *gin.Context) {
storageImpl := storage.GetStorageFactory().GetStorage()
if storageImpl == nil {
common.ResponseWithCodeData(c, common.CodeServerError, nil, "storage not initialized")
+ operationLog.Code = int(common.CodeServerError)
+ operationLog.Error = errors.New("storage not initialized")
return
}
// Try to get file blob from primary location (parent_id, location)
var blob []byte
var getErr error
- if file.Location != nil && *file.Location != "" {
- blob, getErr = storageImpl.Get(ctx, file.ParentID, *file.Location)
+ if fileContent.Location != nil && *fileContent.Location != "" {
+ blob, getErr = storageImpl.Get(ctx, fileContent.ParentID, *fileContent.Location)
}
// If blob is empty, try fallback via file2document
if len(blob) == 0 {
- ctx := c.Request.Context()
- storageAddr, err := h.fileService.GetStorageAddress(ctx, fileID)
+ var storageAddr *file.StorageAddress
+ storageAddr, err = h.fileService.GetStorageAddress(ctx, fileID)
if err != nil {
common.ResponseWithCodeData(c, common.CodeServerError, nil, "Failed to get file storage address: "+err.Error())
+ operationLog.Code = int(common.CodeServerError)
+ operationLog.Error = errors.New("failed to get file storage address")
return
}
blob, getErr = storageImpl.Get(ctx, storageAddr.Bucket, storageAddr.Name)
@@ -508,16 +607,18 @@ func (h *FileHandler) Download(c *gin.Context) {
errMsg += ": " + getErr.Error()
}
common.ResponseWithCodeData(c, common.CodeServerError, nil, errMsg)
+ operationLog.Code = int(common.CodeServerError)
+ operationLog.Error = errors.New(errMsg)
return
}
// Extract file extension
- ext := utility.GetFileExtension(file.Name)
+ ext := utility.GetFileExtension(fileContent.Name)
// Determine content type based on extension and file type
- contentType := utility.GetContentType(ext, file.Type)
+ contentType := utility.GetContentType(ext, fileContent.Type)
- utility.SetDownloadFileResponseHeaders(c.Writer.Header(), contentType, ext, file.Name)
+ utility.SetDownloadFileResponseHeaders(c.Writer.Header(), contentType, ext, fileContent.Name)
// Send file data
c.Data(http.StatusOK, contentType, blob)
@@ -536,9 +637,24 @@ func (h *FileHandler) Download(c *gin.Context) {
// @Success 200 {object} map[string]interface{}
// @Router /api/v1/files/link-to-datasets [post]
func (h *FileHandler) LinkToDatasets(c *gin.Context) {
+ startAt := time.Now()
+ operationLog := &common.OperationLog{
+ EventTime: startAt,
+ Operation: "link-to-datasets",
+ APIPath: c.FullPath(),
+ HTTPMethod: c.Request.Method,
+ IPAddress: c.ClientIP(),
+ }
+ defer func() {
+ operationLog.DurationMS = time.Since(startAt).Milliseconds()
+ common.Audit(operationLog)
+ }()
+
user, errorCode, errorMessage := GetUser(c)
if errorCode != common.CodeSuccess {
common.ErrorWithCode(c, errorCode, errorMessage)
+ operationLog.Code = int(errorCode)
+ operationLog.Error = errors.New(errorMessage)
return
}
@@ -560,18 +676,27 @@ func (h *FileHandler) LinkToDatasets(c *gin.Context) {
missing = append(missing, "kb_ids")
}
if len(missing) > 0 {
- common.ResponseWithCodeData(c, common.CodeArgumentError, nil, fmt.Sprintf("required argument are missing: %s; ", strings.Join(missing, ",")))
+ errorMessage = fmt.Sprintf("required argument are missing: %s; ", strings.Join(missing, ","))
+ common.ResponseWithCodeData(c, common.CodeArgumentError, nil, errorMessage)
+ operationLog.Code = int(common.CodeArgumentError)
+ operationLog.Error = errors.New(errorMessage)
return
}
mode := strings.ToLower(c.DefaultQuery("mode", "replace"))
if mode != "add" && mode != "replace" {
- common.ResponseWithCodeData(c, common.CodeArgumentError, nil, "mode must be 'add' or 'replace'")
+ errorMessage = "mode must be 'add' or 'replace'"
+ common.ResponseWithCodeData(c, common.CodeArgumentError, nil, errorMessage)
+ operationLog.Code = int(common.CodeArgumentError)
+ operationLog.Error = errors.New(errorMessage)
return
}
ctx := c.Request.Context()
if err := h.file2DocumentService.LinkToDatasets(ctx, user.ID, &req, mode); err != nil {
- common.ResponseWithCodeData(c, linkToDatasetsErrorCode(err), nil, err.Error())
+ errorCode = linkToDatasetsErrorCode(err)
+ common.ErrorWithCode(c, errorCode, err.Error())
+ operationLog.Code = int(errorCode)
+ operationLog.Error = err
return
}