This repository has been archived by the owner on Dec 26, 2023. It is now read-only.
/
ipdr_src.go
84 lines (72 loc) · 2.11 KB
/
ipdr_src.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// SPDX-License-Identifier: GPL-3.0
package ipdr
import (
"context"
"errors"
"io"
"github.com/containers/image/v5/manifest"
"github.com/containers/image/v5/types"
iface "github.com/ipfs/boxo/coreiface"
"github.com/ipfs/boxo/coreiface/path"
"github.com/ipfs/boxo/files"
"github.com/opencontainers/go-digest"
)
type ipdrImageSource struct {
reference *ipdrImageReference
ipfs iface.CoreAPI
}
func sizeOrMinusOne(n files.Node) int64 {
size, err := n.Size()
if err != nil {
size = -1
}
return size
}
func (s *ipdrImageSource) Reference() types.ImageReference {
return s.reference
}
func (s *ipdrImageSource) Close() error {
return nil
}
func (s *ipdrImageSource) GetManifest(ctx context.Context, instanceDigest *digest.Digest) ([]byte, string, error) {
var p path.Path
if instanceDigest == nil {
p = path.Join(s.reference.path, "manifests", s.reference.tag)
} else {
p = path.Join(s.reference.path, "manifests", instanceDigest.String())
}
manifestNode, err := s.ipfs.Unixfs().Get(ctx, p)
if err != nil {
return nil, "", err
}
manifestFile := files.ToFile(manifestNode)
if manifestFile == nil {
return nil, "", errors.New("manifest.json expected file")
}
bytes, err := io.ReadAll(manifestFile)
if err != nil {
return nil, "", err
}
return bytes, manifest.GuessMIMEType(bytes), nil
}
func (s *ipdrImageSource) GetBlob(ctx context.Context, blobInfo types.BlobInfo, _ types.BlobInfoCache) (io.ReadCloser, int64, error) {
p := path.Join(s.reference.path, "blobs", blobInfo.Digest.String())
blobNode, err := s.ipfs.Unixfs().Get(ctx, p)
if err != nil {
return nil, 0, err
}
blobFile := files.ToFile(blobNode)
if blobFile == nil {
return nil, -1, errors.New("blob expected file")
}
return blobFile, sizeOrMinusOne(blobFile), nil
}
func (s *ipdrImageSource) HasThreadSafeGetBlob() bool {
return true
}
func (s *ipdrImageSource) GetSignatures(ctx context.Context, instanceDigest *digest.Digest) ([][]byte, error) {
return nil, nil // TODO, not yet supported
}
func (s *ipdrImageSource) LayerInfosForCopy(ctx context.Context, instanceDigest *digest.Digest) ([]types.BlobInfo, error) {
return nil, nil
}