feat(openai): retrieve uploaded file metadata

This commit is contained in:
NgoQuocViet2001
2026-05-02 14:33:42 +07:00
parent 3d52040b3b
commit 36d0239dc6
10 changed files with 133 additions and 9 deletions

View File

@@ -1,11 +1,15 @@
package files
import (
"context"
"errors"
"io"
"net/http"
"strings"
"time"
"github.com/go-chi/chi/v5"
"ds2api/internal/auth"
"ds2api/internal/chathistory"
"ds2api/internal/config"
@@ -22,6 +26,10 @@ type Handler struct {
ChatHistory *chathistory.Store
}
type fileFetcher interface {
FetchUploadedFile(ctx context.Context, a *auth.RequestAuth, fileID string) (*dsclient.UploadFileResult, error)
}
func (h *Handler) UploadFile(w http.ResponseWriter, r *http.Request) {
a, err := h.Auth.Determine(r)
if err != nil {
@@ -85,6 +93,44 @@ func (h *Handler) UploadFile(w http.ResponseWriter, r *http.Request) {
shared.WriteJSON(w, http.StatusOK, buildOpenAIFileObject(result))
}
func (h *Handler) RetrieveFile(w http.ResponseWriter, r *http.Request) {
a, err := h.Auth.Determine(r)
if err != nil {
status := http.StatusUnauthorized
detail := err.Error()
if err == auth.ErrNoAccount {
status = http.StatusTooManyRequests
}
shared.WriteOpenAIError(w, status, detail)
return
}
defer h.Auth.Release(a)
fileID := strings.TrimSpace(chi.URLParam(r, "file_id"))
if fileID == "" {
shared.WriteOpenAIError(w, http.StatusBadRequest, "file_id is required")
return
}
fetcher, ok := h.DS.(fileFetcher)
if !ok {
shared.WriteOpenAIError(w, http.StatusNotImplemented, "file retrieval is not available")
return
}
result, err := fetcher.FetchUploadedFile(r.Context(), a, fileID)
if err != nil {
if errors.Is(err, dsclient.ErrUploadFileNotFound) {
shared.WriteOpenAIError(w, http.StatusNotFound, "file not found")
return
}
shared.WriteOpenAIError(w, http.StatusInternalServerError, "Failed to retrieve file.")
return
}
if result != nil && result.AccountID == "" {
result.AccountID = a.AccountID
}
shared.WriteJSON(w, http.StatusOK, buildOpenAIFileObject(result))
}
func resolveUploadModelType(store shared.ConfigReader, r *http.Request) string {
for _, candidate := range []string{r.FormValue("model_type"), r.Header.Get("X-Model-Type")} {
if modelType := normalizeUploadModelType(candidate); modelType != "" {