refactor: add UploadPinned and have DNSLinkExists ensure the upload exists, the dnslink exists, and the upload is pinned
This commit is contained in:
parent
82ae0d3ef7
commit
53f9a8fb32
|
@ -1,11 +1,14 @@
|
||||||
package account
|
package account
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"crypto/ed25519"
|
"crypto/ed25519"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.lumeweb.com/LumeWeb/portal/metadata"
|
||||||
|
|
||||||
"git.lumeweb.com/LumeWeb/portal/mailer"
|
"git.lumeweb.com/LumeWeb/portal/mailer"
|
||||||
|
|
||||||
"gorm.io/gorm/clause"
|
"gorm.io/gorm/clause"
|
||||||
|
@ -28,6 +31,7 @@ type AccountServiceParams struct {
|
||||||
Config *config.Manager
|
Config *config.Manager
|
||||||
Identity ed25519.PrivateKey
|
Identity ed25519.PrivateKey
|
||||||
Mailer *mailer.Mailer
|
Mailer *mailer.Mailer
|
||||||
|
Metadata metadata.MetadataService
|
||||||
}
|
}
|
||||||
|
|
||||||
var Module = fx.Module("account",
|
var Module = fx.Module("account",
|
||||||
|
@ -41,10 +45,11 @@ type AccountServiceDefault struct {
|
||||||
config *config.Manager
|
config *config.Manager
|
||||||
identity ed25519.PrivateKey
|
identity ed25519.PrivateKey
|
||||||
mailer *mailer.Mailer
|
mailer *mailer.Mailer
|
||||||
|
metadata metadata.MetadataService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAccountService(params AccountServiceParams) *AccountServiceDefault {
|
func NewAccountService(params AccountServiceParams) *AccountServiceDefault {
|
||||||
return &AccountServiceDefault{db: params.Db, config: params.Config, identity: params.Identity, mailer: params.Mailer}
|
return &AccountServiceDefault{db: params.Db, config: params.Config, identity: params.Identity, mailer: params.Mailer, metadata: params.Metadata}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *AccountServiceDefault) EmailExists(email string) (bool, *models.User, error) {
|
func (s *AccountServiceDefault) EmailExists(email string) (bool, *models.User, error) {
|
||||||
|
@ -540,14 +545,48 @@ func (s AccountServiceDefault) OTPDisable(userId uint) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s AccountServiceDefault) DNSLinkExists(hash []byte) (bool, *models.DNSLink, error) {
|
func (s AccountServiceDefault) DNSLinkExists(hash []byte) (bool, *models.DNSLink, error) {
|
||||||
dnsLink := &models.DNSLink{}
|
upload, err := s.metadata.GetUpload(context.Background(), hash)
|
||||||
exists, model, err := s.exists(dnsLink, map[string]interface{}{"upload": hash})
|
if err != nil {
|
||||||
|
return false, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
exists, model, err := s.exists(&models.DNSLink{}, map[string]interface{}{"upload_id": upload.ID})
|
||||||
if !exists || err != nil {
|
if !exists || err != nil {
|
||||||
return false, nil, err
|
return false, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pinned, err := s.UploadPinned(hash)
|
||||||
|
if err != nil {
|
||||||
|
return false, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !pinned {
|
||||||
|
return false, nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
return true, model.(*models.DNSLink), nil
|
return true, model.(*models.DNSLink), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s AccountServiceDefault) UploadPinned(hash []byte) (bool, error) {
|
||||||
|
upload, err := s.metadata.GetUpload(context.Background(), hash)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var pin models.Pin
|
||||||
|
result := s.db.Model(&models.Pin{}).Where(&models.Pin{UploadID: upload.ID}).First(&pin)
|
||||||
|
|
||||||
|
if result.Error != nil {
|
||||||
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, result.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
func GenerateSecurityToken() string {
|
func GenerateSecurityToken() string {
|
||||||
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
|
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
|
||||||
b := make([]byte, 6)
|
b := make([]byte, 6)
|
||||||
|
|
Loading…
Reference in New Issue