feat(files): enhance file handling with support for HTTP uploads and direct binary access
This commit is contained in:
@@ -178,7 +178,8 @@ func routes(logger *slog.Logger, cfg *config.Config, db *store.DB, provider ai.P
|
||||
|
||||
mcpHandler := mcpserver.New(cfg.MCP, toolSet)
|
||||
mux.Handle(cfg.MCP.Path, authMiddleware(mcpHandler))
|
||||
mux.Handle("/files", authMiddleware(fileUploadHandler(filesTool)))
|
||||
mux.Handle("/files", authMiddleware(fileHandler(filesTool)))
|
||||
mux.Handle("/files/{id}", authMiddleware(fileHandler(filesTool)))
|
||||
if oauthRegistry != nil && tokenStore != nil {
|
||||
mux.HandleFunc("/.well-known/oauth-authorization-server", oauthMetadataHandler())
|
||||
mux.HandleFunc("/oauth-authorization-server", oauthMetadataHandler())
|
||||
|
||||
@@ -16,8 +16,14 @@ const (
|
||||
multipartFormMemory = 32 << 20
|
||||
)
|
||||
|
||||
func fileUploadHandler(files *tools.FilesTool) http.Handler {
|
||||
func fileHandler(files *tools.FilesTool) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
if id != "" {
|
||||
fileDownloadHandler(files, id, w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if r.Method != http.MethodPost {
|
||||
w.Header().Set("Allow", http.MethodPost)
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
@@ -44,6 +50,29 @@ func fileUploadHandler(files *tools.FilesTool) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
func fileDownloadHandler(files *tools.FilesTool, id string, w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
||||
w.Header().Set("Allow", "GET, HEAD")
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
file, err := files.GetRaw(r.Context(), id)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", file.MediaType)
|
||||
w.Header().Set("Content-Disposition", "attachment; filename="+file.Name)
|
||||
w.Header().Set("X-File-Kind", file.Kind)
|
||||
w.Header().Set("X-File-SHA256", file.SHA256)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if r.Method != http.MethodHead {
|
||||
_, _ = w.Write(file.Content)
|
||||
}
|
||||
}
|
||||
|
||||
func parseUploadRequest(r *http.Request) (tools.SaveFileDecodedInput, error) {
|
||||
contentType := r.Header.Get("Content-Type")
|
||||
mediaType, _, _ := mime.ParseMediaType(contentType)
|
||||
|
||||
Reference in New Issue
Block a user