From a4e0e1fa585ed416d06dfac51c734b5d98735d74 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Tue, 16 Jan 2024 01:01:57 -0500 Subject: [PATCH] refactor: have FileExists return the upload model if it exists --- interfaces/storage.go | 7 +++++-- storage/storage.go | 8 ++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/interfaces/storage.go b/interfaces/storage.go index b43020d..80a37a9 100644 --- a/interfaces/storage.go +++ b/interfaces/storage.go @@ -1,10 +1,13 @@ package interfaces -import "io" +import ( + "git.lumeweb.com/LumeWeb/portal/db/models" + "io" +) type StorageService interface { Init() PutFile(file io.ReadSeeker, bucket string, generateProof bool) ([]byte, error) - FileExists(hash []byte) bool + FileExists(hash []byte) (bool, models.Upload) GetHash(file io.ReadSeeker) ([]byte, error) } diff --git a/storage/storage.go b/storage/storage.go index a7579c1..1c79cc8 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -105,13 +105,13 @@ func (s *StorageServiceImpl) createBucketIfNotExists(bucket string) error { return nil } -func (s *StorageServiceImpl) FileExists(hash []byte) bool { +func (s *StorageServiceImpl) FileExists(hash []byte) (bool, models.Upload) { hashStr := hex.EncodeToString(hash) - var count int64 - s.portal.Db().Model(&models.Upload{}).Where(&models.Upload{Hash: hashStr}).Count(&count) + var upload models.Upload + result := s.portal.Db().Model(&models.Upload{}).Where(&models.Upload{Hash: hashStr}).First(&upload) - return count > 0 + return result.RowsAffected > 0, upload } func (s *StorageServiceImpl) GetHash(file io.ReadSeeker) ([]byte, error) {