From 62e22d0d39104ed418e3eb773185298960d137c8 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Tue, 16 Jan 2024 00:40:50 -0500 Subject: [PATCH] feat: add CIDExists --- interfaces/storage.go | 3 +++ storage/storage.go | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/interfaces/storage.go b/interfaces/storage.go index 095ceee..be15e65 100644 --- a/interfaces/storage.go +++ b/interfaces/storage.go @@ -5,4 +5,7 @@ import "io" type StorageService interface { Init() PutFile(file io.ReadSeeker, bucket string, generateProof bool) ([]byte, error) + CIDExists(cid interface { + ToString() (string, error) + }) bool } diff --git a/storage/storage.go b/storage/storage.go index 194ec8d..ec508a3 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -3,6 +3,7 @@ package storage import ( "bytes" "git.lumeweb.com/LumeWeb/libs5-go/encoding" + "git.lumeweb.com/LumeWeb/portal/db/models" "git.lumeweb.com/LumeWeb/portal/interfaces" "github.com/go-resty/resty/v2" "go.uber.org/zap" @@ -105,3 +106,17 @@ 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 + } + + var count int64 + s.portal.Db().Model(&models.Upload{}).Where(&models.Upload{CID: cidStr}).Count(&count) + + return count > 0 +}