code.gitea.io/gitea@v1.19.3/modules/packages/content_store.go (about) 1 // Copyright 2021 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package packages 5 6 import ( 7 "io" 8 "path" 9 "strings" 10 11 "code.gitea.io/gitea/modules/storage" 12 "code.gitea.io/gitea/modules/util" 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 // FIXME: Workaround to be removed in v1.20 35 // https://github.com/go-gitea/gitea/issues/19586 36 func (s *ContentStore) Has(key BlobHash256Key) error { 37 _, err := s.store.Stat(KeyToRelativePath(key)) 38 return err 39 } 40 41 // Save stores a package blob 42 func (s *ContentStore) Save(key BlobHash256Key, r io.Reader, size int64) error { 43 _, err := s.store.Save(KeyToRelativePath(key), r, size) 44 return err 45 } 46 47 // Delete deletes a package blob 48 func (s *ContentStore) Delete(key BlobHash256Key) error { 49 return s.store.Delete(KeyToRelativePath(key)) 50 } 51 52 // KeyToRelativePath converts the sha256 key aabb000000... to aa/bb/aabb000000... 53 func KeyToRelativePath(key BlobHash256Key) string { 54 return path.Join(string(key)[0:2], string(key)[2:4], string(key)) 55 } 56 57 // RelativePathToKey converts a relative path aa/bb/aabb000000... to the sha256 key aabb000000... 58 func RelativePathToKey(relativePath string) (BlobHash256Key, error) { 59 parts := strings.SplitN(relativePath, "/", 3) 60 if len(parts) != 3 || len(parts[0]) != 2 || len(parts[1]) != 2 || len(parts[2]) < 4 || parts[0]+parts[1] != parts[2][0:4] { 61 return "", util.ErrInvalidArgument 62 } 63 64 return BlobHash256Key(parts[2]), nil 65 }