MCPcopy Index your code
hub / github.com/53AI/53AIHub / PreviewRawFileContent

Function PreviewRawFileContent

api/controller/file_preview.go:42–265  ·  view source on GitHub ↗

PreviewRawFileContent godoc @Summary 预览原始文件内容 @Description 预览原始文件内容接口,支持文本文件以及上传的原始文件 @Tags 文件管理 @Accept json @Produce json @Security BearerAuth @Param file_id path int true "文件ID" @Success 200 {object} string "文件内容" @Router /api/files/{file_id}/preview_raw [get]

(c *gin.Context)

Source from the content-addressed store, hash-verified

40// @Success 200 {object} string "文件内容"
41// @Router /api/files/{file_id}/preview_raw [get]
42func PreviewRawFileContent(c *gin.Context) {
43 // eid := config.GetEID(c)
44
45 idStr := c.Param("file_id")
46 if idStr == "" {
47 c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("文件ID不能为空")))
48 return
49 }
50
51 fileID, err := strconv.ParseInt(idStr, 10, 64)
52 if err != nil {
53 c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("无效的文件ID")))
54 return
55 }
56
57 // 获取文件信息
58 file, err := model.GetFileByIDOlny(fileID)
59 if err != nil {
60 c.JSON(http.StatusNotFound, model.NotFound.ToResponse(err))
61 return
62 }
63
64 // 处理 file.Path 的扩展名提取
65 // 转换文件: readme.pdf.md -> 提取 .pdf 作为原始扩展名
66 // 原始 md 文件: readme.md -> 保留 .md 扩展名
67 // 版本号格式: v0.3.1.md -> 保留 .md 扩展名(不把 .1 当作扩展名)
68 filePath := file.Path
69 ext := strings.ToLower(filepath.Ext(filePath))
70 validSourceExts := map[string]bool{
71 ".pdf": true, ".docx": true, ".doc": true,
72 ".html": true, ".htm": true, ".txt": true,
73 }
74 if ext == ".md" {
75 withoutMd := strings.TrimSuffix(filePath, ".md")
76 innerExt := strings.ToLower(filepath.Ext(withoutMd))
77 if validSourceExts[innerExt] {
78 filePath = withoutMd
79 ext = innerExt
80 }
81 }
82
83 if file.OriginType == model.FileOriginTypeAIGenerated && file.OriginRefID > 0 {
84 if err := file.LoadUploadFile(); err != nil || file.UploadFile == nil {
85 if err == nil {
86 err = errors.New("未找到上传文件")
87 }
88 c.JSON(http.StatusNotFound, model.NotFound.ToResponse(err))
89 return
90 }
91 if err := serveUploadFilePreview(c, fileID, file.UploadFile, ext); err != nil {
92 return
93 }
94 return
95 }
96
97 // 纯文本文件(无原始上传文件)从 FileBody 读取最新内容
98 // 转换文件(xxx.html.md)和原始文本文件(有 UploadFileID)从 uploadFile 读取
99 // 非文本文件(pdf/docx 等)从原始 uploadFile 读取

Callers

nothing calls this directly

Calls 14

serveUploadFilePreviewFunction · 0.85
ToResponseMethod · 0.80
LoadUploadFileMethod · 0.80
StatusMethod · 0.80
GetBucketMethod · 0.80
ErrorfMethod · 0.80
InfofMethod · 0.80
StatMethod · 0.80
NewMethod · 0.45
LoadContentMethod · 0.45
HeaderMethod · 0.45

Tested by

no test coverage detected