feat: initial storage service
This commit is contained in:
parent
27773a7909
commit
8afc157d4f
|
@ -6,6 +6,7 @@ import (
|
||||||
"git.lumeweb.com/LumeWeb/portal/config"
|
"git.lumeweb.com/LumeWeb/portal/config"
|
||||||
"git.lumeweb.com/LumeWeb/portal/interfaces"
|
"git.lumeweb.com/LumeWeb/portal/interfaces"
|
||||||
"git.lumeweb.com/LumeWeb/portal/protocols"
|
"git.lumeweb.com/LumeWeb/portal/protocols"
|
||||||
|
"git.lumeweb.com/LumeWeb/portal/storage"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"go.sia.tech/core/wallet"
|
"go.sia.tech/core/wallet"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
@ -23,15 +24,23 @@ type PortalImpl struct {
|
||||||
protocolRegistry protocols.ProtocolRegistry
|
protocolRegistry protocols.ProtocolRegistry
|
||||||
logger *zap.Logger
|
logger *zap.Logger
|
||||||
identity ed25519.PrivateKey
|
identity ed25519.PrivateKey
|
||||||
|
storage interfaces.StorageService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPortal() interfaces.Portal {
|
func NewPortal() interfaces.Portal {
|
||||||
logger, _ := zap.NewDevelopment()
|
logger, _ := zap.NewDevelopment()
|
||||||
return &PortalImpl{
|
|
||||||
|
portal := &PortalImpl{
|
||||||
apiRegistry: api.NewRegistry(),
|
apiRegistry: api.NewRegistry(),
|
||||||
protocolRegistry: protocols.NewProtocolRegistry(),
|
protocolRegistry: protocols.NewProtocolRegistry(),
|
||||||
logger: logger,
|
logger: logger,
|
||||||
|
storage: nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
storageServ := storage.NewStorageService(portal)
|
||||||
|
portal.storage = storageServ
|
||||||
|
|
||||||
|
return portal
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PortalImpl) Initialize() error {
|
func (p *PortalImpl) Initialize() error {
|
||||||
|
@ -143,6 +152,10 @@ func (p *PortalImpl) getInitFuncs() []func() error {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func (p *PortalImpl) Storage() interfaces.StorageService {
|
||||||
|
return p.storage
|
||||||
|
}
|
||||||
|
|
||||||
func (p *PortalImpl) getStartFuncs() []func() error {
|
func (p *PortalImpl) getStartFuncs() []func() error {
|
||||||
return []func() error{
|
return []func() error{
|
||||||
func() error {
|
func() error {
|
||||||
|
|
|
@ -15,4 +15,5 @@ type Portal interface {
|
||||||
Db() *gorm.DB
|
Db() *gorm.DB
|
||||||
ApiRegistry() APIRegistry
|
ApiRegistry() APIRegistry
|
||||||
Identity() ed25519.PrivateKey
|
Identity() ed25519.PrivateKey
|
||||||
|
Storage() StorageService
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
package interfaces
|
||||||
|
|
||||||
|
import "io"
|
||||||
|
|
||||||
|
type StorageService interface {
|
||||||
|
PutFile(file io.ReadSeeker, bucket string, generateProof bool) ([]byte, error)
|
||||||
|
}
|
|
@ -0,0 +1,64 @@
|
||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
||||||
|
"git.lumeweb.com/LumeWeb/portal/interfaces"
|
||||||
|
"github.com/go-resty/resty/v2"
|
||||||
|
"io"
|
||||||
|
"lukechampine.com/blake3"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ interfaces.StorageService = (*StorageServiceImpl)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
|
type StorageServiceImpl struct {
|
||||||
|
portal interfaces.Portal
|
||||||
|
httpApi *resty.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewStorageService(portal interfaces.Portal) interfaces.StorageService {
|
||||||
|
client := resty.New()
|
||||||
|
|
||||||
|
client.SetBaseURL(portal.Config().GetString("core.sia.url"))
|
||||||
|
client.SetBasicAuth("", portal.Config().GetString("core.sia.key"))
|
||||||
|
|
||||||
|
return &StorageServiceImpl{
|
||||||
|
portal: portal,
|
||||||
|
httpApi: client,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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())
|
||||||
|
hashStr, err := encoding.NewMultihash(hash[:]).ToBase64Url()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
buf.Reset()
|
||||||
|
|
||||||
|
resp, err := s.httpApi.R().
|
||||||
|
SetPathParam("path", hashStr).
|
||||||
|
SetFormData(map[string]string{
|
||||||
|
"bucket": bucket,
|
||||||
|
}).
|
||||||
|
SetBody(buf).Put("/api/worker/{path}")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.IsError() {
|
||||||
|
return nil, resp.Error().(error)
|
||||||
|
}
|
||||||
|
|
||||||
|
return hash[:], nil
|
||||||
|
}
|
Loading…
Reference in New Issue