MCPcopy Index your code
hub / github.com/coder/coder / chatFileByID

Method chatFileByID

coderd/exp_chats.go:6162–6203  ·  view source on GitHub ↗

EXPERIMENTAL: this endpoint is experimental and is subject to change. @Summary Get chat file @ID get-chat-file @Security CoderSessionToken @Tags Chats @Produce image/png,image/jpeg,image/gif,image/webp,text/plain,text/markdown,text/csv,application/json,application/pdf @Param file path string true "

(rw http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

6160// @Router /api/experimental/chats/files/{file} [get]
6161// @Description Experimental: this endpoint is subject to change.
6162func (api *API) chatFileByID(rw http.ResponseWriter, r *http.Request) {
6163 ctx := r.Context()
6164
6165 fileIDStr := chi.URLParam(r, "file")
6166 fileID, err := uuid.Parse(fileIDStr)
6167 if err != nil {
6168 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
6169 Message: "Invalid file ID.",
6170 })
6171 return
6172 }
6173
6174 chatFile, err := api.Database.GetChatFileByID(ctx, fileID)
6175 if err != nil {
6176 if httpapi.Is404Error(err) {
6177 httpapi.ResourceNotFound(rw)
6178 return
6179 }
6180 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
6181 Message: "Failed to get chat file.",
6182 Detail: err.Error(),
6183 })
6184 return
6185 }
6186
6187 rw.Header().Set("Content-Type", chatFile.Mimetype)
6188 disposition := "attachment"
6189 if chatfiles.IsInlineRenderableStoredMediaType(chatFile.Mimetype) {
6190 disposition = "inline"
6191 }
6192 if chatFile.Name != "" {
6193 rw.Header().Set("Content-Disposition", mime.FormatMediaType(disposition, map[string]string{"filename": chatFile.Name}))
6194 } else {
6195 rw.Header().Set("Content-Disposition", disposition)
6196 }
6197 rw.Header().Set("Cache-Control", "private, max-age=31536000, immutable")
6198 rw.Header().Set("Content-Length", strconv.Itoa(len(chatFile.Data)))
6199 rw.WriteHeader(http.StatusOK)
6200 if _, err := rw.Write(chatFile.Data); err != nil {
6201 api.Logger.Debug(ctx, "failed to write chat file response", slog.Error(err))
6202 }
6203}
6204
6205func createChatInputFromRequest(ctx context.Context, db database.Store, req codersdk.CreateChatRequest) (
6206 []codersdk.ChatMessagePart,

Callers

nothing calls this directly

Calls 12

WriteFunction · 0.92
Is404ErrorFunction · 0.92
ResourceNotFoundFunction · 0.92
ContextMethod · 0.65
ParseMethod · 0.65
GetChatFileByIDMethod · 0.65
SetMethod · 0.65
WriteMethod · 0.65
ErrorMethod · 0.45
HeaderMethod · 0.45
WriteHeaderMethod · 0.45

Tested by

no test coverage detected