| package oci |
|
|
| import ( |
| "context" |
| "encoding/json" |
| "fmt" |
| "io" |
| "net/http" |
|
|
| ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| ) |
|
|
| |
| type Manifest struct { |
| SchemaVersion int `json:"schemaVersion"` |
| MediaType string `json:"mediaType"` |
| Config Config `json:"config"` |
| Layers []LayerDetail `json:"layers"` |
| } |
|
|
| |
| type Config struct { |
| Digest string `json:"digest"` |
| MediaType string `json:"mediaType"` |
| Size int `json:"size"` |
| } |
|
|
| |
| type LayerDetail struct { |
| Digest string `json:"digest"` |
| MediaType string `json:"mediaType"` |
| Size int `json:"size"` |
| } |
|
|
| func OllamaModelManifest(image string) (*Manifest, error) { |
| |
|
|
| |
| |
| tag, repository, image := ParseImageParts(image) |
|
|
| |
| req, err := http.NewRequest("GET", "https://registry.ollama.ai/v2/"+repository+"/"+image+"/manifests/"+tag, nil) |
| if err != nil { |
| return nil, err |
| } |
| req.Header.Set("Accept", "application/vnd.docker.distribution.manifest.v2+json") |
| client := &http.Client{} |
| resp, err := client.Do(req) |
| if err != nil { |
| return nil, err |
| } |
|
|
| |
| var manifest Manifest |
| err = json.NewDecoder(resp.Body).Decode(&manifest) |
| if err != nil { |
| return nil, err |
| } |
|
|
| return &manifest, nil |
| } |
|
|
| func OllamaModelBlob(image string) (string, error) { |
| manifest, err := OllamaModelManifest(image) |
| if err != nil { |
| return "", err |
| } |
| |
|
|
| for _, layer := range manifest.Layers { |
| if layer.MediaType == "application/vnd.ollama.image.model" { |
| return layer.Digest, nil |
| } |
| } |
|
|
| return "", nil |
| } |
|
|
| func OllamaFetchModel(ctx context.Context, image string, output string, statusWriter func(ocispec.Descriptor) io.Writer) error { |
| _, repository, imageNoTag := ParseImageParts(image) |
|
|
| blobID, err := OllamaModelBlob(image) |
| if err != nil { |
| return err |
| } |
|
|
| return FetchImageBlob(ctx, fmt.Sprintf("registry.ollama.ai/%s/%s", repository, imageNoTag), blobID, output, statusWriter) |
| } |
|
|