|
| 1 | +package artifact |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/hex" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + |
| 10 | + "github.com/flanksource/duty/models" |
| 11 | + "github.com/google/uuid" |
| 12 | + "gorm.io/gorm" |
| 13 | +) |
| 14 | + |
| 15 | +type BlobStore interface { |
| 16 | + Write(data Data, artifact *models.Artifact) (*models.Artifact, error) |
| 17 | + Read(artifactID uuid.UUID) (*Data, error) |
| 18 | + io.Closer |
| 19 | +} |
| 20 | + |
| 21 | +type blobStore struct { |
| 22 | + fs FilesystemRW |
| 23 | + db *gorm.DB |
| 24 | + backend string |
| 25 | +} |
| 26 | + |
| 27 | +func NewBlobStore(fs FilesystemRW, db *gorm.DB, backend string) BlobStore { |
| 28 | + return &blobStore{fs: fs, db: db, backend: backend} |
| 29 | +} |
| 30 | + |
| 31 | +func (s *blobStore) Write(data Data, a *models.Artifact) (*models.Artifact, error) { |
| 32 | + if data.Content == nil { |
| 33 | + return nil, fmt.Errorf("artifact data content is nil") |
| 34 | + } |
| 35 | + defer func() { _ = data.Content.Close() }() |
| 36 | + |
| 37 | + checksum := sha256.New() |
| 38 | + mimeReader := io.TeeReader(data.Content, checksum) |
| 39 | + |
| 40 | + mw := &mimeWriter{Max: maxBytesForMimeDetection} |
| 41 | + fileReader := io.TeeReader(mimeReader, mw) |
| 42 | + |
| 43 | + info, err := s.fs.Write(s.db.Statement.Context, data.Filename, fileReader) |
| 44 | + if err != nil { |
| 45 | + return nil, fmt.Errorf("writing artifact %s: %w", data.Filename, err) |
| 46 | + } |
| 47 | + |
| 48 | + if data.ContentType == "" { |
| 49 | + data.ContentType = mw.Detect().String() |
| 50 | + } |
| 51 | + |
| 52 | + // For inline store, the artifact already has content set |
| 53 | + if inlineArt := InlineArtifact(info); inlineArt != nil { |
| 54 | + a.Content = inlineArt.Content |
| 55 | + a.CompressionType = inlineArt.CompressionType |
| 56 | + } |
| 57 | + |
| 58 | + a.Path = data.Filename |
| 59 | + a.Filename = info.Name() |
| 60 | + a.Size = info.Size() |
| 61 | + a.ContentType = data.ContentType |
| 62 | + a.Checksum = hex.EncodeToString(checksum.Sum(nil)) |
| 63 | + |
| 64 | + if err := s.db.Create(a).Error; err != nil { |
| 65 | + return nil, fmt.Errorf("saving artifact to db: %w", err) |
| 66 | + } |
| 67 | + |
| 68 | + return a, nil |
| 69 | +} |
| 70 | + |
| 71 | +func (s *blobStore) Read(artifactID uuid.UUID) (*Data, error) { |
| 72 | + var a models.Artifact |
| 73 | + if err := s.db.Where("id = ?", artifactID).First(&a).Error; err != nil { |
| 74 | + return nil, fmt.Errorf("finding artifact %s: %w", artifactID, err) |
| 75 | + } |
| 76 | + |
| 77 | + if a.IsInline() { |
| 78 | + content, err := a.GetContent() |
| 79 | + if err != nil { |
| 80 | + return nil, fmt.Errorf("decompressing inline artifact %s: %w", artifactID, err) |
| 81 | + } |
| 82 | + return &Data{ |
| 83 | + Content: io.NopCloser(bytes.NewReader(content)), |
| 84 | + ContentLength: a.Size, |
| 85 | + Checksum: a.Checksum, |
| 86 | + ContentType: a.ContentType, |
| 87 | + Filename: a.Filename, |
| 88 | + }, nil |
| 89 | + } |
| 90 | + |
| 91 | + r, err := s.fs.Read(s.db.Statement.Context, a.Path) |
| 92 | + if err != nil { |
| 93 | + return nil, fmt.Errorf("reading artifact %s from %s: %w", artifactID, a.Path, err) |
| 94 | + } |
| 95 | + |
| 96 | + return &Data{ |
| 97 | + Content: r, |
| 98 | + ContentLength: a.Size, |
| 99 | + Checksum: a.Checksum, |
| 100 | + ContentType: a.ContentType, |
| 101 | + Filename: a.Filename, |
| 102 | + }, nil |
| 103 | +} |
| 104 | + |
| 105 | +func (s *blobStore) Close() error { |
| 106 | + return s.fs.Close() |
| 107 | +} |
0 commit comments