refactor: change struct naming convention from "impl" to "default"
This commit is contained in:
parent
8449b13a4a
commit
6d34f5b683
|
@ -22,24 +22,24 @@ var Module = fx.Module("account",
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
type AccountServiceImpl struct {
|
type AccountServiceDefault struct {
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
config *viper.Viper
|
config *viper.Viper
|
||||||
identity ed25519.PrivateKey
|
identity ed25519.PrivateKey
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAccountService(params AccountServiceParams) *AccountServiceImpl {
|
func NewAccountService(params AccountServiceParams) *AccountServiceDefault {
|
||||||
return &AccountServiceImpl{db: params.Db, config: params.Config, identity: params.Identity}
|
return &AccountServiceDefault{db: params.Db, config: params.Config, identity: params.Identity}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s AccountServiceImpl) EmailExists(email string) (bool, models.User) {
|
func (s AccountServiceDefault) EmailExists(email string) (bool, models.User) {
|
||||||
var user models.User
|
var user models.User
|
||||||
|
|
||||||
result := s.db.Model(&models.User{}).Where(&models.User{Email: email}).First(&user)
|
result := s.db.Model(&models.User{}).Where(&models.User{Email: email}).First(&user)
|
||||||
|
|
||||||
return result.RowsAffected > 0, user
|
return result.RowsAffected > 0, user
|
||||||
}
|
}
|
||||||
func (s AccountServiceImpl) PubkeyExists(pubkey string) (bool, models.PublicKey) {
|
func (s AccountServiceDefault) PubkeyExists(pubkey string) (bool, models.PublicKey) {
|
||||||
var model models.PublicKey
|
var model models.PublicKey
|
||||||
|
|
||||||
result := s.db.Model(&models.PublicKey{}).Where(&models.PublicKey{Key: pubkey}).First(&model)
|
result := s.db.Model(&models.PublicKey{}).Where(&models.PublicKey{Key: pubkey}).First(&model)
|
||||||
|
@ -47,14 +47,14 @@ func (s AccountServiceImpl) PubkeyExists(pubkey string) (bool, models.PublicKey)
|
||||||
return result.RowsAffected > 0, model
|
return result.RowsAffected > 0, model
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s AccountServiceImpl) AccountExists(id uint64) (bool, models.User) {
|
func (s AccountServiceDefault) AccountExists(id uint64) (bool, models.User) {
|
||||||
var model models.User
|
var model models.User
|
||||||
|
|
||||||
result := s.db.Model(&models.User{}).First(&model, id)
|
result := s.db.Model(&models.User{}).First(&model, id)
|
||||||
|
|
||||||
return result.RowsAffected > 0, model
|
return result.RowsAffected > 0, model
|
||||||
}
|
}
|
||||||
func (s AccountServiceImpl) CreateAccount(email string, password string) (*models.User, error) {
|
func (s AccountServiceDefault) CreateAccount(email string, password string) (*models.User, error) {
|
||||||
var user models.User
|
var user models.User
|
||||||
|
|
||||||
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||||
|
@ -73,7 +73,7 @@ func (s AccountServiceImpl) CreateAccount(email string, password string) (*model
|
||||||
|
|
||||||
return &user, nil
|
return &user, nil
|
||||||
}
|
}
|
||||||
func (s AccountServiceImpl) AddPubkeyToAccount(user models.User, pubkey string) error {
|
func (s AccountServiceDefault) AddPubkeyToAccount(user models.User, pubkey string) error {
|
||||||
var model models.PublicKey
|
var model models.PublicKey
|
||||||
|
|
||||||
model.Key = pubkey
|
model.Key = pubkey
|
||||||
|
@ -87,7 +87,7 @@ func (s AccountServiceImpl) AddPubkeyToAccount(user models.User, pubkey string)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (s AccountServiceImpl) LoginPassword(email string, password string) (string, error) {
|
func (s AccountServiceDefault) LoginPassword(email string, password string) (string, error) {
|
||||||
var user models.User
|
var user models.User
|
||||||
|
|
||||||
result := s.db.Model(&models.User{}).Where(&models.User{Email: email}).First(&user)
|
result := s.db.Model(&models.User{}).Where(&models.User{Email: email}).First(&user)
|
||||||
|
@ -109,7 +109,7 @@ func (s AccountServiceImpl) LoginPassword(email string, password string) (string
|
||||||
return token, nil
|
return token, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s AccountServiceImpl) LoginPubkey(pubkey string) (string, error) {
|
func (s AccountServiceDefault) LoginPubkey(pubkey string) (string, error) {
|
||||||
var model models.PublicKey
|
var model models.PublicKey
|
||||||
|
|
||||||
result := s.db.Model(&models.PublicKey{}).Where(&models.PublicKey{Key: pubkey}).First(&model)
|
result := s.db.Model(&models.PublicKey{}).Where(&models.PublicKey{Key: pubkey}).First(&model)
|
||||||
|
@ -126,7 +126,7 @@ func (s AccountServiceImpl) LoginPubkey(pubkey string) (string, error) {
|
||||||
return token, nil
|
return token, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s AccountServiceImpl) AccountPins(id uint64, createdAfter uint64) ([]models.Pin, error) {
|
func (s AccountServiceDefault) AccountPins(id uint64, createdAfter uint64) ([]models.Pin, error) {
|
||||||
var pins []models.Pin
|
var pins []models.Pin
|
||||||
|
|
||||||
result := s.db.Model(&models.Pin{}).
|
result := s.db.Model(&models.Pin{}).
|
||||||
|
@ -143,7 +143,7 @@ func (s AccountServiceImpl) AccountPins(id uint64, createdAfter uint64) ([]model
|
||||||
return pins, nil
|
return pins, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s AccountServiceImpl) DeletePinByHash(hash string, accountID uint) error {
|
func (s AccountServiceDefault) DeletePinByHash(hash string, accountID uint) error {
|
||||||
// Define a struct for the query condition
|
// Define a struct for the query condition
|
||||||
uploadQuery := models.Upload{Hash: hash}
|
uploadQuery := models.Upload{Hash: hash}
|
||||||
|
|
||||||
|
@ -175,7 +175,7 @@ func (s AccountServiceImpl) DeletePinByHash(hash string, accountID uint) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (s AccountServiceImpl) PinByHash(hash string, accountID uint) error {
|
func (s AccountServiceDefault) PinByHash(hash string, accountID uint) error {
|
||||||
// Define a struct for the query condition
|
// Define a struct for the query condition
|
||||||
uploadQuery := models.Upload{Hash: hash}
|
uploadQuery := models.Upload{Hash: hash}
|
||||||
|
|
||||||
|
@ -193,7 +193,7 @@ func (s AccountServiceImpl) PinByHash(hash string, accountID uint) error {
|
||||||
return s.PinByID(uploadID, accountID)
|
return s.PinByID(uploadID, accountID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s AccountServiceImpl) PinByID(uploadId uint, accountID uint) error {
|
func (s AccountServiceDefault) PinByID(uploadId uint, accountID uint) error {
|
||||||
result := s.db.Model(&models.Pin{}).Where(&models.Pin{UploadID: uploadId, UserID: accountID}).First(&models.Pin{})
|
result := s.db.Model(&models.Pin{}).Where(&models.Pin{UploadID: uploadId, UserID: accountID}).First(&models.Pin{})
|
||||||
|
|
||||||
if result.Error != nil && result.Error != gorm.ErrRecordNotFound {
|
if result.Error != nil && result.Error != gorm.ErrRecordNotFound {
|
||||||
|
|
|
@ -43,7 +43,7 @@ func ParseAuthTokenHeader(headers http.Header) string {
|
||||||
return authHeader
|
return authHeader
|
||||||
}
|
}
|
||||||
|
|
||||||
func AuthMiddleware(identity ed25519.PrivateKey, accounts *account.AccountServiceImpl) func(http.Handler) http.Handler {
|
func AuthMiddleware(identity ed25519.PrivateKey, accounts *account.AccountServiceDefault) func(http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
authToken := FindAuthToken(r)
|
authToken := FindAuthToken(r)
|
||||||
|
|
|
@ -82,9 +82,9 @@ type HttpHandler struct {
|
||||||
verifier *emailverifier.Verifier
|
verifier *emailverifier.Verifier
|
||||||
config *viper.Viper
|
config *viper.Viper
|
||||||
logger *zap.Logger
|
logger *zap.Logger
|
||||||
storage *storage.StorageServiceImpl
|
storage *storage.StorageServiceDefault
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
accounts *account.AccountServiceImpl
|
accounts *account.AccountServiceDefault
|
||||||
protocol *s5.S5Protocol
|
protocol *s5.S5Protocol
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,9 +93,9 @@ type HttpHandlerParams struct {
|
||||||
|
|
||||||
Config *viper.Viper
|
Config *viper.Viper
|
||||||
Logger *zap.Logger
|
Logger *zap.Logger
|
||||||
Storage *storage.StorageServiceImpl
|
Storage *storage.StorageServiceDefault
|
||||||
Db *gorm.DB
|
Db *gorm.DB
|
||||||
Accounts *account.AccountServiceImpl
|
Accounts *account.AccountServiceDefault
|
||||||
Protocol *s5.S5Protocol
|
Protocol *s5.S5Protocol
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
api/s5/s5.go
10
api/s5/s5.go
|
@ -25,8 +25,8 @@ var (
|
||||||
type S5API struct {
|
type S5API struct {
|
||||||
config *viper.Viper
|
config *viper.Viper
|
||||||
identity ed25519.PrivateKey
|
identity ed25519.PrivateKey
|
||||||
accounts *account.AccountServiceImpl
|
accounts *account.AccountServiceDefault
|
||||||
storage *storage.StorageServiceImpl
|
storage *storage.StorageServiceDefault
|
||||||
protocols []protoRegistry.Protocol
|
protocols []protoRegistry.Protocol
|
||||||
httpHandler HttpHandler
|
httpHandler HttpHandler
|
||||||
protocol *s5.S5Protocol
|
protocol *s5.S5Protocol
|
||||||
|
@ -36,8 +36,8 @@ type APIParams struct {
|
||||||
fx.In
|
fx.In
|
||||||
Config *viper.Viper
|
Config *viper.Viper
|
||||||
Identity ed25519.PrivateKey
|
Identity ed25519.PrivateKey
|
||||||
Accounts *account.AccountServiceImpl
|
Accounts *account.AccountServiceDefault
|
||||||
Storage *storage.StorageServiceImpl
|
Storage *storage.StorageServiceDefault
|
||||||
Protocols []protoRegistry.Protocol `group:"protocol"`
|
Protocols []protoRegistry.Protocol `group:"protocol"`
|
||||||
HttpHandler HttpHandler
|
HttpHandler HttpHandler
|
||||||
}
|
}
|
||||||
|
@ -168,7 +168,7 @@ func (w *s5TusJwtResponseWriter) WriteHeader(statusCode int) {
|
||||||
w.ResponseWriter.WriteHeader(statusCode)
|
w.ResponseWriter.WriteHeader(statusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func BuildS5TusApi(identity ed25519.PrivateKey, accounts *account.AccountServiceImpl, storage *storage.StorageServiceImpl) jape.Handler {
|
func BuildS5TusApi(identity ed25519.PrivateKey, accounts *account.AccountServiceDefault, storage *storage.StorageServiceDefault) jape.Handler {
|
||||||
// Create a jape.Handler for your tusHandler
|
// Create a jape.Handler for your tusHandler
|
||||||
tusJapeHandler := func(c jape.Context) {
|
tusJapeHandler := func(c jape.Context) {
|
||||||
tusHandler := storage.Tus()
|
tusHandler := storage.Tus()
|
||||||
|
|
18
cron/cron.go
18
cron/cron.go
|
@ -32,7 +32,7 @@ var Module = fx.Module("cron",
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
type CronServiceImpl struct {
|
type CronServiceDefault struct {
|
||||||
scheduler gocron.Scheduler
|
scheduler gocron.Scheduler
|
||||||
services []CronableService
|
services []CronableService
|
||||||
logger *zap.Logger
|
logger *zap.Logger
|
||||||
|
@ -56,12 +56,12 @@ type CronJob struct {
|
||||||
Options []gocron.JobOption
|
Options []gocron.JobOption
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CronServiceImpl) Scheduler() gocron.Scheduler {
|
func (c *CronServiceDefault) Scheduler() gocron.Scheduler {
|
||||||
return c.scheduler
|
return c.scheduler
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCronService(lc fx.Lifecycle, params CronServiceParams) *CronServiceImpl {
|
func NewCronService(lc fx.Lifecycle, params CronServiceParams) *CronServiceDefault {
|
||||||
sc := &CronServiceImpl{
|
sc := &CronServiceDefault{
|
||||||
logger: params.Logger,
|
logger: params.Logger,
|
||||||
scheduler: params.Scheduler,
|
scheduler: params.Scheduler,
|
||||||
}
|
}
|
||||||
|
@ -75,7 +75,7 @@ func NewCronService(lc fx.Lifecycle, params CronServiceParams) *CronServiceImpl
|
||||||
return sc
|
return sc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CronServiceImpl) start() error {
|
func (c *CronServiceDefault) start() error {
|
||||||
for _, service := range c.services {
|
for _, service := range c.services {
|
||||||
err := service.LoadInitialTasks(c)
|
err := service.LoadInitialTasks(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -88,11 +88,11 @@ func (c *CronServiceImpl) start() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CronServiceImpl) RegisterService(service CronableService) {
|
func (c *CronServiceDefault) RegisterService(service CronableService) {
|
||||||
c.services = append(c.services, service)
|
c.services = append(c.services, service)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CronServiceImpl) RetryableTask(params RetryableTaskParams) CronJob {
|
func (c *CronServiceDefault) RetryableTask(params RetryableTaskParams) CronJob {
|
||||||
job := gocron.OneTimeJob(gocron.OneTimeJobStartImmediately())
|
job := gocron.OneTimeJob(gocron.OneTimeJobStartImmediately())
|
||||||
|
|
||||||
if params.Attempt > 0 {
|
if params.Attempt > 0 {
|
||||||
|
@ -145,7 +145,7 @@ func (c *CronServiceImpl) RetryableTask(params RetryableTaskParams) CronJob {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CronServiceImpl) CreateJob(job CronJob) (gocron.Job, error) {
|
func (c *CronServiceDefault) CreateJob(job CronJob) (gocron.Job, error) {
|
||||||
ret, err := c.Scheduler().NewJob(job.Job, job.Task, job.Options...)
|
ret, err := c.Scheduler().NewJob(job.Job, job.Task, job.Options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -153,7 +153,7 @@ func (c *CronServiceImpl) CreateJob(job CronJob) (gocron.Job, error) {
|
||||||
|
|
||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
func (c *CronServiceImpl) RerunJob(job CronJob) (gocron.Job, error) {
|
func (c *CronServiceDefault) RerunJob(job CronJob) (gocron.Job, error) {
|
||||||
ret, err := c.Scheduler().Update(job.JobId, job.Job, job.Task, job.Options...)
|
ret, err := c.Scheduler().Update(job.JobId, job.Job, job.Task, job.Options...)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -29,7 +29,7 @@ var (
|
||||||
type S5Protocol struct {
|
type S5Protocol struct {
|
||||||
config *viper.Viper
|
config *viper.Viper
|
||||||
logger *zap.Logger
|
logger *zap.Logger
|
||||||
storage *storage.StorageServiceImpl
|
storage *storage.StorageServiceDefault
|
||||||
identity ed25519.PrivateKey
|
identity ed25519.PrivateKey
|
||||||
node *s5node.Node
|
node *s5node.Node
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ type S5ProtocolParams struct {
|
||||||
fx.In
|
fx.In
|
||||||
Config *viper.Viper
|
Config *viper.Viper
|
||||||
Logger *zap.Logger
|
Logger *zap.Logger
|
||||||
Storage *storage.StorageServiceImpl
|
Storage *storage.StorageServiceDefault
|
||||||
Identity ed25519.PrivateKey
|
Identity ed25519.PrivateKey
|
||||||
ProviderStore *S5ProviderStore
|
ProviderStore *S5ProviderStore
|
||||||
}
|
}
|
||||||
|
@ -142,7 +142,7 @@ func ConfigureS5Protocol(params S5ProtocolParams) (*s5config.NodeConfig, error)
|
||||||
return cfg, nil
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewS5ProviderStore(config *viper.Viper, logger *zap.Logger, storage *storage.StorageServiceImpl) *S5ProviderStore {
|
func NewS5ProviderStore(config *viper.Viper, logger *zap.Logger, storage *storage.StorageServiceDefault) *S5ProviderStore {
|
||||||
return &S5ProviderStore{
|
return &S5ProviderStore{
|
||||||
config: config,
|
config: config,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
|
@ -205,7 +205,7 @@ func (s *S5Protocol) Stop(ctx context.Context) error {
|
||||||
type S5ProviderStore struct {
|
type S5ProviderStore struct {
|
||||||
config *viper.Viper
|
config *viper.Viper
|
||||||
logger *zap.Logger
|
logger *zap.Logger
|
||||||
storage *storage.StorageServiceImpl
|
storage *storage.StorageServiceDefault
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s S5ProviderStore) CanProvide(hash *encoding.Multihash, kind []types.StorageLocationType) bool {
|
func (s S5ProviderStore) CanProvide(hash *encoding.Multihash, kind []types.StorageLocationType) bool {
|
||||||
|
|
|
@ -1,25 +1,25 @@
|
||||||
package storage
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
||||||
"git.lumeweb.com/LumeWeb/portal/db/models"
|
"git.lumeweb.com/LumeWeb/portal/db/models"
|
||||||
"io"
|
"io"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FileImpl struct {
|
type FileImpl struct {
|
||||||
reader io.ReadCloser
|
reader io.ReadCloser
|
||||||
hash []byte
|
hash []byte
|
||||||
storage *StorageServiceImpl
|
storage *StorageServiceDefault
|
||||||
record *models.Upload
|
record *models.Upload
|
||||||
cid *encoding.CID
|
cid *encoding.CID
|
||||||
read bool
|
read bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFile(hash []byte, storage *StorageServiceImpl) *FileImpl {
|
func NewFile(hash []byte, storage *StorageServiceDefault) *FileImpl {
|
||||||
return &FileImpl{hash: hash, storage: storage, read: false}
|
return &FileImpl{hash: hash, storage: storage, read: false}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
type MySQLLocker struct {
|
type MySQLLocker struct {
|
||||||
storage *StorageServiceImpl
|
storage *StorageServiceDefault
|
||||||
AcquirerPollInterval time.Duration
|
AcquirerPollInterval time.Duration
|
||||||
HolderPollInterval time.Duration
|
HolderPollInterval time.Duration
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
|
|
|
@ -43,8 +43,8 @@ type StorageServiceParams struct {
|
||||||
Config *viper.Viper
|
Config *viper.Viper
|
||||||
Logger *zap.Logger
|
Logger *zap.Logger
|
||||||
Db *gorm.DB
|
Db *gorm.DB
|
||||||
Accounts *account.AccountServiceImpl
|
Accounts *account.AccountServiceDefault
|
||||||
Cron *cron.CronServiceImpl
|
Cron *cron.CronServiceDefault
|
||||||
}
|
}
|
||||||
|
|
||||||
var Module = fx.Module("storage",
|
var Module = fx.Module("storage",
|
||||||
|
@ -53,7 +53,7 @@ var Module = fx.Module("storage",
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
type StorageServiceImpl struct {
|
type StorageServiceDefault struct {
|
||||||
busClient *busClient.Client
|
busClient *busClient.Client
|
||||||
workerClient *workerClient.Client
|
workerClient *workerClient.Client
|
||||||
tus *tusd.Handler
|
tus *tusd.Handler
|
||||||
|
@ -62,20 +62,20 @@ type StorageServiceImpl struct {
|
||||||
config *viper.Viper
|
config *viper.Viper
|
||||||
logger *zap.Logger
|
logger *zap.Logger
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
accounts *account.AccountServiceImpl
|
accounts *account.AccountServiceDefault
|
||||||
cron *cron.CronServiceImpl
|
cron *cron.CronServiceDefault
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StorageServiceImpl) Tus() *tusd.Handler {
|
func (s *StorageServiceDefault) Tus() *tusd.Handler {
|
||||||
return s.tus
|
return s.tus
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StorageServiceImpl) Start() error {
|
func (s *StorageServiceDefault) Start() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStorageService(params StorageServiceParams) *StorageServiceImpl {
|
func NewStorageService(params StorageServiceParams) *StorageServiceDefault {
|
||||||
return &StorageServiceImpl{
|
return &StorageServiceDefault{
|
||||||
config: params.Config,
|
config: params.Config,
|
||||||
logger: params.Logger,
|
logger: params.Logger,
|
||||||
db: params.Db,
|
db: params.Db,
|
||||||
|
@ -84,7 +84,7 @@ func NewStorageService(params StorageServiceParams) *StorageServiceImpl {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s StorageServiceImpl) PutFileSmall(file io.ReadSeeker, bucket string, generateProof bool) ([]byte, error) {
|
func (s StorageServiceDefault) PutFileSmall(file io.ReadSeeker, bucket string, generateProof bool) ([]byte, error) {
|
||||||
hash, err := s.GetHashSmall(file)
|
hash, err := s.GetHashSmall(file)
|
||||||
hashStr, err := encoding.NewMultihash(s.getPrefixedHash(hash)).ToBase64Url()
|
hashStr, err := encoding.NewMultihash(s.getPrefixedHash(hash)).ToBase64Url()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -109,7 +109,7 @@ func (s StorageServiceImpl) PutFileSmall(file io.ReadSeeker, bucket string, gene
|
||||||
|
|
||||||
return hash[:], nil
|
return hash[:], nil
|
||||||
}
|
}
|
||||||
func (s StorageServiceImpl) PutFile(file io.Reader, bucket string, hash []byte) error {
|
func (s StorageServiceDefault) PutFile(file io.Reader, bucket string, hash []byte) error {
|
||||||
hashStr, err := encoding.NewMultihash(s.getPrefixedHash(hash)).ToBase64Url()
|
hashStr, err := encoding.NewMultihash(s.getPrefixedHash(hash)).ToBase64Url()
|
||||||
err = s.createBucketIfNotExists(bucket)
|
err = s.createBucketIfNotExists(bucket)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -124,7 +124,7 @@ func (s StorageServiceImpl) PutFile(file io.Reader, bucket string, hash []byte)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StorageServiceImpl) BuildUploadBufferTus(basePath string, preUploadCb TusPreUploadCreateCallback, preFinishCb TusPreFinishResponseCallback) (*tusd.Handler, tusd.DataStore, *s3.Client, error) {
|
func (s *StorageServiceDefault) BuildUploadBufferTus(basePath string, preUploadCb TusPreUploadCreateCallback, preFinishCb TusPreFinishResponseCallback) (*tusd.Handler, tusd.DataStore, *s3.Client, error) {
|
||||||
customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
|
customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
|
||||||
if service == s3.ServiceID {
|
if service == s3.ServiceID {
|
||||||
return aws.Endpoint{
|
return aws.Endpoint{
|
||||||
|
@ -172,7 +172,7 @@ func (s *StorageServiceImpl) BuildUploadBufferTus(basePath string, preUploadCb T
|
||||||
return handler, store, s3Client, err
|
return handler, store, s3Client, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StorageServiceImpl) init() error {
|
func (s *StorageServiceDefault) init() error {
|
||||||
|
|
||||||
addr := s.config.GetString("core.sia.url")
|
addr := s.config.GetString("core.sia.url")
|
||||||
passwd := s.config.GetString("core.sia.key")
|
passwd := s.config.GetString("core.sia.key")
|
||||||
|
@ -237,11 +237,11 @@ func (s *StorageServiceImpl) init() error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (s *StorageServiceImpl) LoadInitialTasks(cron cron.CronService) error {
|
func (s *StorageServiceDefault) LoadInitialTasks(cron cron.CronService) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StorageServiceImpl) createBucketIfNotExists(bucket string) error {
|
func (s *StorageServiceDefault) createBucketIfNotExists(bucket string) error {
|
||||||
|
|
||||||
_, err := s.busClient.Bucket(context.Background(), bucket)
|
_, err := s.busClient.Bucket(context.Background(), bucket)
|
||||||
|
|
||||||
|
@ -267,7 +267,7 @@ func (s *StorageServiceImpl) createBucketIfNotExists(bucket string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StorageServiceImpl) FileExists(hash []byte) (bool, models.Upload) {
|
func (s *StorageServiceDefault) FileExists(hash []byte) (bool, models.Upload) {
|
||||||
hashStr := hex.EncodeToString(hash)
|
hashStr := hex.EncodeToString(hash)
|
||||||
|
|
||||||
var upload models.Upload
|
var upload models.Upload
|
||||||
|
@ -276,7 +276,7 @@ func (s *StorageServiceImpl) FileExists(hash []byte) (bool, models.Upload) {
|
||||||
return result.RowsAffected > 0, upload
|
return result.RowsAffected > 0, upload
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StorageServiceImpl) GetHashSmall(file io.ReadSeeker) ([]byte, error) {
|
func (s *StorageServiceDefault) GetHashSmall(file io.ReadSeeker) ([]byte, error) {
|
||||||
buf := bytes.NewBuffer(nil)
|
buf := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
_, err := io.Copy(buf, file)
|
_, err := io.Copy(buf, file)
|
||||||
|
@ -288,7 +288,7 @@ func (s *StorageServiceImpl) GetHashSmall(file io.ReadSeeker) ([]byte, error) {
|
||||||
|
|
||||||
return hash[:], nil
|
return hash[:], nil
|
||||||
}
|
}
|
||||||
func (s *StorageServiceImpl) GetHash(file io.Reader) ([]byte, int64, error) {
|
func (s *StorageServiceDefault) GetHash(file io.Reader) ([]byte, int64, error) {
|
||||||
hasher := blake3.New(64, nil)
|
hasher := blake3.New(64, nil)
|
||||||
|
|
||||||
totalBytes, err := io.Copy(hasher, file)
|
totalBytes, err := io.Copy(hasher, file)
|
||||||
|
@ -302,7 +302,7 @@ func (s *StorageServiceImpl) GetHash(file io.Reader) ([]byte, int64, error) {
|
||||||
return hash[:32], totalBytes, nil
|
return hash[:32], totalBytes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StorageServiceImpl) CreateUpload(hash []byte, mime string, uploaderID uint, uploaderIP string, size uint64, protocol string) (*models.Upload, error) {
|
func (s *StorageServiceDefault) CreateUpload(hash []byte, mime string, uploaderID uint, uploaderIP string, size uint64, protocol string) (*models.Upload, error) {
|
||||||
hashStr := hex.EncodeToString(hash)
|
hashStr := hex.EncodeToString(hash)
|
||||||
|
|
||||||
upload := &models.Upload{
|
upload := &models.Upload{
|
||||||
|
@ -322,7 +322,7 @@ func (s *StorageServiceImpl) CreateUpload(hash []byte, mime string, uploaderID u
|
||||||
|
|
||||||
return upload, nil
|
return upload, nil
|
||||||
}
|
}
|
||||||
func (s *StorageServiceImpl) tusWorker() {
|
func (s *StorageServiceDefault) tusWorker() {
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
@ -392,7 +392,7 @@ func (s *StorageServiceImpl) tusWorker() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StorageServiceImpl) TusUploadExists(hash []byte) (bool, models.TusUpload) {
|
func (s *StorageServiceDefault) TusUploadExists(hash []byte) (bool, models.TusUpload) {
|
||||||
hashStr := hex.EncodeToString(hash)
|
hashStr := hex.EncodeToString(hash)
|
||||||
|
|
||||||
var upload models.TusUpload
|
var upload models.TusUpload
|
||||||
|
@ -401,7 +401,7 @@ func (s *StorageServiceImpl) TusUploadExists(hash []byte) (bool, models.TusUploa
|
||||||
return result.RowsAffected > 0, upload
|
return result.RowsAffected > 0, upload
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StorageServiceImpl) CreateTusUpload(hash []byte, uploadID string, uploaderID uint, uploaderIP string, protocol string) (*models.TusUpload, error) {
|
func (s *StorageServiceDefault) CreateTusUpload(hash []byte, uploadID string, uploaderID uint, uploaderIP string, protocol string) (*models.TusUpload, error) {
|
||||||
hashStr := hex.EncodeToString(hash)
|
hashStr := hex.EncodeToString(hash)
|
||||||
|
|
||||||
upload := &models.TusUpload{
|
upload := &models.TusUpload{
|
||||||
|
@ -421,7 +421,7 @@ func (s *StorageServiceImpl) CreateTusUpload(hash []byte, uploadID string, uploa
|
||||||
|
|
||||||
return upload, nil
|
return upload, nil
|
||||||
}
|
}
|
||||||
func (s *StorageServiceImpl) TusUploadProgress(uploadID string) error {
|
func (s *StorageServiceDefault) TusUploadProgress(uploadID string) error {
|
||||||
|
|
||||||
find := &models.TusUpload{UploadID: uploadID}
|
find := &models.TusUpload{UploadID: uploadID}
|
||||||
|
|
||||||
|
@ -440,7 +440,7 @@ func (s *StorageServiceImpl) TusUploadProgress(uploadID string) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (s *StorageServiceImpl) TusUploadCompleted(uploadID string) error {
|
func (s *StorageServiceDefault) TusUploadCompleted(uploadID string) error {
|
||||||
|
|
||||||
find := &models.TusUpload{UploadID: uploadID}
|
find := &models.TusUpload{UploadID: uploadID}
|
||||||
|
|
||||||
|
@ -455,7 +455,7 @@ func (s *StorageServiceImpl) TusUploadCompleted(uploadID string) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (s *StorageServiceImpl) DeleteTusUpload(uploadID string) error {
|
func (s *StorageServiceDefault) DeleteTusUpload(uploadID string) error {
|
||||||
result := s.db.Where(&models.TusUpload{UploadID: uploadID}).Delete(&models.TusUpload{})
|
result := s.db.Where(&models.TusUpload{UploadID: uploadID}).Delete(&models.TusUpload{})
|
||||||
|
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
|
@ -465,7 +465,7 @@ func (s *StorageServiceImpl) DeleteTusUpload(uploadID string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StorageServiceImpl) ScheduleTusUpload(uploadID string) error {
|
func (s *StorageServiceDefault) ScheduleTusUpload(uploadID string) error {
|
||||||
find := &models.TusUpload{UploadID: uploadID}
|
find := &models.TusUpload{UploadID: uploadID}
|
||||||
|
|
||||||
var upload models.TusUpload
|
var upload models.TusUpload
|
||||||
|
@ -498,7 +498,7 @@ func (s *StorageServiceImpl) ScheduleTusUpload(uploadID string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StorageServiceImpl) tusUploadTask(upload *models.TusUpload) error {
|
func (s *StorageServiceDefault) tusUploadTask(upload *models.TusUpload) error {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
tusUpload, err := s.tusStore.GetUpload(ctx, upload.UploadID)
|
tusUpload, err := s.tusStore.GetUpload(ctx, upload.UploadID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -605,7 +605,7 @@ func (s *StorageServiceImpl) tusUploadTask(upload *models.TusUpload) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StorageServiceImpl) getPrefixedHash(hash []byte) []byte {
|
func (s *StorageServiceDefault) getPrefixedHash(hash []byte) []byte {
|
||||||
return append([]byte{byte(types.HashTypeBlake3)}, hash...)
|
return append([]byte{byte(types.HashTypeBlake3)}, hash...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -620,7 +620,7 @@ func splitS3Ids(id string) (objectId, multipartId string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StorageServiceImpl) GetFile(hash []byte, start int64) (io.ReadCloser, int64, error) {
|
func (s *StorageServiceDefault) GetFile(hash []byte, start int64) (io.ReadCloser, int64, error) {
|
||||||
if exists, tusUpload := s.TusUploadExists(hash); exists {
|
if exists, tusUpload := s.TusUploadExists(hash); exists {
|
||||||
if tusUpload.Completed {
|
if tusUpload.Completed {
|
||||||
upload, err := s.tusStore.GetUpload(context.Background(), tusUpload.UploadID)
|
upload, err := s.tusStore.GetUpload(context.Background(), tusUpload.UploadID)
|
||||||
|
@ -675,6 +675,6 @@ func (s *StorageServiceImpl) GetFile(hash []byte, start int64) (io.ReadCloser, i
|
||||||
|
|
||||||
return object.Content, int64(upload.Size), nil
|
return object.Content, int64(upload.Size), nil
|
||||||
}
|
}
|
||||||
func (s *StorageServiceImpl) NewFile(hash []byte) *FileImpl {
|
func (s *StorageServiceDefault) NewFile(hash []byte) *FileImpl {
|
||||||
return NewFile(hash, s)
|
return NewFile(hash, s)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue