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

Function PreviewFile

api/controller/upload.go:150–194  ·  view source on GitHub ↗

PreviewFile @Summary Preview a file @Description Preview a file @Tags Upload @Accept json @Produce octet-stream @Param key path string true "file key" @Success 200 {object} []byte "file content" 修改路由定义,使用路径参数 @Router /api/preview/{key} [get]

(c *gin.Context)

Source from the content-addressed store, hash-verified

148// 修改路由定义,使用路径参数
149// @Router /api/preview/{key} [get]
150func PreviewFile(c *gin.Context) {
151 // 从路径参数中获取 key
152 key := c.Param("key")
153 if key == "" {
154 c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(nil))
155 return
156 }
157
158 // uploadFile, err := model.GetUploadFileByEidAndPreviewKey(config.GetEID(c), key)
159 // if err != nil {
160 // c.JSON(http.StatusBadRequest, model.NotFound.ToResponse(err))
161 // return
162 // }
163 uploadFile, err := model.GetNoAuthUploadFileByEidAndPreviewKey(key)
164 if err != nil {
165 c.JSON(http.StatusBadRequest, model.NotFound.ToResponse(err))
166 return
167 }
168
169 filename := uploadFile.FileName
170 encodedFilename := url.QueryEscape(filename)
171
172 c.Header("Content-Disposition", `inline; filename="`+filename+`"; filename*=UTF-8''`+encodedFilename)
173 c.Header("Content-Type", uploadFile.MimeType)
174 c.Header("Content-Length", fmt.Sprintf("%d", uploadFile.Size))
175
176 if _, ok := storage.StorageInstance.(*storage.LocalStorage); ok {
177 file, err := os.Open(uploadFile.Key)
178 if err != nil {
179 c.JSON(http.StatusBadRequest, model.FileError.ToResponse(err))
180 return
181 }
182 defer file.Close()
183 c.Status(http.StatusOK)
184 _, _ = io.Copy(c.Writer, file)
185 return
186 }
187
188 fileContent, err := storage.StorageInstance.Load(uploadFile.Key)
189 if err != nil {
190 c.JSON(http.StatusBadRequest, model.FileError.ToResponse(err))
191 return
192 }
193 c.Data(http.StatusOK, uploadFile.MimeType, fileContent)
194}

Callers

nothing calls this directly

Calls 5

ToResponseMethod · 0.80
StatusMethod · 0.80
LoadMethod · 0.65
HeaderMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected