github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/packages/content_store.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package packages
     7  
     8  import (
     9  	"io"
    10  	"path"
    11  
    12  	"github.com/gitbundle/modules/storage"
    13  )
    14  
    15  // BlobHash256Key is the key to address a blob content
    16  type BlobHash256Key string
    17  
    18  // ContentStore is a wrapper around ObjectStorage
    19  type ContentStore struct {
    20  	store storage.ObjectStorage
    21  }
    22  
    23  // NewContentStore creates the default package store
    24  func NewContentStore() *ContentStore {
    25  	contentStore := &ContentStore{storage.Packages}
    26  	return contentStore
    27  }
    28  
    29  // Get gets a package blob
    30  func (s *ContentStore) Get(key BlobHash256Key) (storage.Object, error) {
    31  	return s.store.Open(keyToRelativePath(key))
    32  }
    33  
    34  // Save stores a package blob
    35  func (s *ContentStore) Save(key BlobHash256Key, r io.Reader, size int64) error {
    36  	_, err := s.store.Save(keyToRelativePath(key), r, size)
    37  	return err
    38  }
    39  
    40  // Delete deletes a package blob
    41  func (s *ContentStore) Delete(key BlobHash256Key) error {
    42  	return s.store.Delete(keyToRelativePath(key))
    43  }
    44  
    45  // keyToRelativePath converts the sha256 key aabb000000... to aa/bb/aabb000000...
    46  func keyToRelativePath(key BlobHash256Key) string {
    47  	return path.Join(string(key)[0:2], string(key)[2:4], string(key))
    48  }