feat: add GetHash

This commit is contained in:
Derrick Hammer 2024-01-16 00:48:06 -05:00
parent 62e22d0d39
commit 61696f42b8
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 20 additions and 9 deletions

View File

@ -8,4 +8,5 @@ type StorageService interface {
CIDExists(cid interface {
ToString() (string, error)
}) bool
GetHash(file io.ReadSeeker) ([]byte, error)
}

View File

@ -28,20 +28,17 @@ func NewStorageService(portal interfaces.Portal) interfaces.StorageService {
}
func (s StorageServiceImpl) PutFile(file io.ReadSeeker, bucket string, generateProof bool) ([]byte, error) {
buf := bytes.NewBuffer(nil)
_, err := io.Copy(buf, file)
if err != nil {
return nil, err
}
hash := blake3.Sum512(buf.Bytes())
hash, err := s.GetHash(file)
hashStr, err := encoding.NewMultihash(hash[:]).ToBase64Url()
if err != nil {
return nil, err
}
buf.Reset()
_, err = file.Seek(0, io.SeekStart)
if err != nil {
return nil, err
}
err = s.createBucketIfNotExists(bucket)
if err != nil {
@ -53,7 +50,7 @@ func (s StorageServiceImpl) PutFile(file io.ReadSeeker, bucket string, generateP
SetFormData(map[string]string{
"bucket": bucket,
}).
SetBody(buf).Put("/api/worker/objects/{path}")
SetBody(file).Put("/api/worker/objects/{path}")
if err != nil {
return nil, err
}
@ -120,3 +117,16 @@ func (s *StorageServiceImpl) CIDExists(cid interface {
return count > 0
}
func (s *StorageServiceImpl) GetHash(file io.ReadSeeker) ([]byte, error) {
buf := bytes.NewBuffer(nil)
_, err := io.Copy(buf, file)
if err != nil {
return nil, err
}
hash := blake3.Sum512(buf.Bytes())
return hash[:], nil
}