AI: Refactor API client in vision package #127 #1090

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer
2025-04-10 21:16:03 +02:00
parent 0304ed37c3
commit 627f4f8d21
10 changed files with 113 additions and 113 deletions

View File

@@ -2,14 +2,7 @@ package vision
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/photoprism/photoprism/internal/api/download"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/media"
"github.com/photoprism/photoprism/pkg/media/http/scheme"
"github.com/photoprism/photoprism/pkg/rnd"
)
@@ -22,41 +15,6 @@ type ApiRequest struct {
Images Files `form:"images" yaml:"Images,omitempty" json:"images,omitempty"`
}
// NewClientRequest returns a new Vision API request with the specified file payload and scheme.
func NewClientRequest(images Files, fileScheme string) (*ApiRequest, error) {
imageUrls := make(Files, len(images))
if fileScheme == scheme.Https && !strings.HasPrefix(DownloadUrl, "https://") {
log.Tracef("vision: file request scheme changed from https to data because https is not configured")
fileScheme = scheme.Data
}
for i := range images {
switch fileScheme {
case scheme.Https:
if id, err := download.Register(images[i]); err != nil {
return nil, fmt.Errorf("%s (register download)", err)
} else {
imageUrls[i] = fmt.Sprintf("%s/%s", DownloadUrl, id)
}
case scheme.Data:
if file, err := os.Open(images[i]); err != nil {
return nil, fmt.Errorf("%s (create data url)", err)
} else {
imageUrls[i] = media.DataUrl(file)
}
default:
return nil, fmt.Errorf("invalid file scheme %s", clean.Log(fileScheme))
}
}
return &ApiRequest{
Id: rnd.UUID(),
Model: "",
Images: imageUrls,
}, nil
}
// GetId returns the request ID string and generates a random ID if none was set.
func (r *ApiRequest) GetId() string {
if r.Id == "" {