GetFileBodyContent godoc @Summary 获取文件版本内容 @Description 根据file_body_id获取文件内容,从content_path读取完整内容 @Tags 文件内容管理 @Accept json @Produce json @Security BearerAuth @Param file_body_id path int true "文件内容ID" @Param filename path string false "文件名(可选)" @Success 200 {object} string "文件内容" @Router /api/file-v
(c *gin.Context)
| 431 | // @Success 200 {object} string "文件内容" |
| 432 | // @Router /api/file-version/{file_body_id} [get] |
| 433 | func GetFileBodyContent(c *gin.Context) { |
| 434 | idStr := c.Param("file_body_id") |
| 435 | if idStr == "" { |
| 436 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("文件内容ID不能为空"))) |
| 437 | return |
| 438 | } |
| 439 | |
| 440 | fileBodyID, err := strconv.ParseInt(idStr, 10, 64) |
| 441 | if err != nil { |
| 442 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("无效的文件内容ID"))) |
| 443 | return |
| 444 | } |
| 445 | |
| 446 | // 获取文件内容记录 |
| 447 | fileBody, err := model.GetFileBodyByID(fileBodyID) |
| 448 | if err != nil { |
| 449 | c.JSON(http.StatusNotFound, model.NotFound.ToResponse(err)) |
| 450 | return |
| 451 | } |
| 452 | |
| 453 | // 加载完整内容(从content_path读取) |
| 454 | if err := fileBody.LoadContent(); err != nil { |
| 455 | c.JSON(http.StatusInternalServerError, model.FileError.ToResponse(err)) |
| 456 | return |
| 457 | } |
| 458 | |
| 459 | // 获取关联文件信息以确定Content-Type |
| 460 | file, err := model.GetFileByIDOlny(fileBody.FileID) |
| 461 | if err != nil { |
| 462 | c.JSON(http.StatusNotFound, model.NotFound.ToResponse(err)) |
| 463 | return |
| 464 | } |
| 465 | |
| 466 | filePath := strings.TrimSuffix(file.Path, ".md") |
| 467 | ext := strings.ToLower(filepath.Ext(filePath)) |
| 468 | |
| 469 | contentType := "text/markdown; charset=utf-8" |
| 470 | switch ext { |
| 471 | case ".txt": |
| 472 | contentType = "text/plain; charset=utf-8" |
| 473 | case ".html", ".htm": |
| 474 | contentType = "text/html; charset=utf-8" |
| 475 | } |
| 476 | |
| 477 | c.Header("Content-Type", contentType) |
| 478 | c.String(http.StatusOK, fileBody.Content) |
| 479 | } |
nothing calls this directly
no test coverage detected