refactor: rename CIDExists to FileExists and have it work on hashes and check in hex format

This commit is contained in:
Derrick Hammer 2024-01-16 00:58:51 -05:00
parent 48f03c0f47
commit 866d105028
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 5 additions and 11 deletions

View File

@ -5,8 +5,6 @@ import "io"
type StorageService interface { type StorageService interface {
Init() Init()
PutFile(file io.ReadSeeker, bucket string, generateProof bool) ([]byte, error) PutFile(file io.ReadSeeker, bucket string, generateProof bool) ([]byte, error)
CIDExists(cid interface { FileExists(hash []byte) bool
ToString() (string, error)
}) bool
GetHash(file io.ReadSeeker) ([]byte, error) GetHash(file io.ReadSeeker) ([]byte, error)
} }

View File

@ -2,6 +2,7 @@ package storage
import ( import (
"bytes" "bytes"
"encoding/hex"
"git.lumeweb.com/LumeWeb/libs5-go/encoding" "git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/portal/db/models" "git.lumeweb.com/LumeWeb/portal/db/models"
"git.lumeweb.com/LumeWeb/portal/interfaces" "git.lumeweb.com/LumeWeb/portal/interfaces"
@ -104,16 +105,11 @@ func (s *StorageServiceImpl) createBucketIfNotExists(bucket string) error {
return nil return nil
} }
func (s *StorageServiceImpl) CIDExists(cid interface { func (s *StorageServiceImpl) FileExists(hash []byte) bool {
ToString() (string, error) hashStr := hex.EncodeToString(hash)
}) bool {
cidStr, err := cid.ToString()
if err != nil {
return false
}
var count int64 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 return count > 0
} }