From 866d1050283bcc51986d7e4f8580fb2444dcd870 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Tue, 16 Jan 2024 00:58:51 -0500 Subject: [PATCH] refactor: rename CIDExists to FileExists and have it work on hashes and check in hex format --- interfaces/storage.go | 4 +--- storage/storage.go | 12 ++++-------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/interfaces/storage.go b/interfaces/storage.go index 974bf94..b43020d 100644 --- a/interfaces/storage.go +++ b/interfaces/storage.go @@ -5,8 +5,6 @@ import "io" type StorageService interface { Init() PutFile(file io.ReadSeeker, bucket string, generateProof bool) ([]byte, error) - CIDExists(cid interface { - ToString() (string, error) - }) bool + FileExists(hash []byte) bool GetHash(file io.ReadSeeker) ([]byte, error) } diff --git a/storage/storage.go b/storage/storage.go index dd63094..a7579c1 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -2,6 +2,7 @@ package storage import ( "bytes" + "encoding/hex" "git.lumeweb.com/LumeWeb/libs5-go/encoding" "git.lumeweb.com/LumeWeb/portal/db/models" "git.lumeweb.com/LumeWeb/portal/interfaces" @@ -104,16 +105,11 @@ func (s *StorageServiceImpl) createBucketIfNotExists(bucket string) error { return nil } -func (s *StorageServiceImpl) CIDExists(cid interface { - ToString() (string, error) -}) bool { - cidStr, err := cid.ToString() - if err != nil { - return false - } +func (s *StorageServiceImpl) FileExists(hash []byte) bool { + hashStr := hex.EncodeToString(hash) var count int64 - s.portal.Db().Model(&models.Upload{}).Where(&models.Upload{CID: cidStr}).Count(&count) + s.portal.Db().Model(&models.Upload{}).Where(&models.Upload{Hash: hashStr}).Count(&count) return count > 0 }