2024-01-15 04:52:54 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-01-19 20:51:31 +00:00
|
|
|
"context"
|
2024-01-16 05:58:51 +00:00
|
|
|
"encoding/hex"
|
2024-01-16 06:36:19 +00:00
|
|
|
"errors"
|
2024-01-15 04:52:54 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
2024-01-19 20:51:31 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
2024-01-19 21:51:41 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/api/middleware"
|
2024-01-16 05:40:50 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/db/models"
|
2024-01-15 04:52:54 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/interfaces"
|
2024-01-19 20:51:31 +00:00
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/config"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/credentials"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
|
|
|
"github.com/go-co-op/gocron/v2"
|
|
|
|
"github.com/google/uuid"
|
2024-01-21 04:36:55 +00:00
|
|
|
"github.com/imroc/req/v3"
|
2024-01-19 20:51:31 +00:00
|
|
|
tusd "github.com/tus/tusd/v2/pkg/handler"
|
|
|
|
s3store "github.com/tus/tusd/v2/pkg/s3store"
|
|
|
|
"go.uber.org/zap"
|
2024-01-15 04:52:54 +00:00
|
|
|
"io"
|
|
|
|
"lukechampine.com/blake3"
|
2024-01-22 23:53:31 +00:00
|
|
|
"strings"
|
2024-01-19 20:51:31 +00:00
|
|
|
"time"
|
2024-01-15 04:52:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ interfaces.StorageService = (*StorageServiceImpl)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
type StorageServiceImpl struct {
|
2024-01-19 20:51:31 +00:00
|
|
|
portal interfaces.Portal
|
2024-01-21 04:36:55 +00:00
|
|
|
httpApi *req.Client
|
2024-01-19 20:51:31 +00:00
|
|
|
tus *tusd.Handler
|
|
|
|
tusStore tusd.DataStore
|
2024-01-22 23:53:31 +00:00
|
|
|
s3Client *s3.Client
|
2024-01-19 20:51:31 +00:00
|
|
|
}
|
|
|
|
|
2024-01-19 21:51:41 +00:00
|
|
|
func (s *StorageServiceImpl) Tus() *tusd.Handler {
|
|
|
|
return s.tus
|
|
|
|
}
|
|
|
|
|
2024-01-19 20:51:31 +00:00
|
|
|
func (s *StorageServiceImpl) Start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StorageServiceImpl) Portal() interfaces.Portal {
|
|
|
|
return s.portal
|
2024-01-15 04:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewStorageService(portal interfaces.Portal) interfaces.StorageService {
|
|
|
|
return &StorageServiceImpl{
|
|
|
|
portal: portal,
|
2024-01-15 13:38:05 +00:00
|
|
|
httpApi: nil,
|
2024-01-15 04:52:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-19 20:51:31 +00:00
|
|
|
func (s StorageServiceImpl) PutFileSmall(file io.ReadSeeker, bucket string, generateProof bool) ([]byte, error) {
|
|
|
|
hash, err := s.GetHashSmall(file)
|
|
|
|
hashStr, err := encoding.NewMultihash(s.getPrefixedHash(hash)).ToBase64Url()
|
2024-01-15 04:52:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-16 05:48:06 +00:00
|
|
|
_, err = file.Seek(0, io.SeekStart)
|
2024-01-15 04:52:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-15 19:25:12 +00:00
|
|
|
err = s.createBucketIfNotExists(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-15 04:52:54 +00:00
|
|
|
resp, err := s.httpApi.R().
|
|
|
|
SetPathParam("path", hashStr).
|
2024-01-21 03:51:00 +00:00
|
|
|
SetQueryParam("bucket", bucket).
|
2024-01-16 05:48:06 +00:00
|
|
|
SetBody(file).Put("/api/worker/objects/{path}")
|
2024-01-15 04:52:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-16 06:36:19 +00:00
|
|
|
if resp.IsError() {
|
|
|
|
if resp.Error() != nil {
|
|
|
|
return nil, resp.Error().(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New(resp.String())
|
|
|
|
|
2024-01-15 04:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hash[:], nil
|
|
|
|
}
|
2024-01-19 20:51:31 +00:00
|
|
|
func (s StorageServiceImpl) PutFile(file io.Reader, bucket string, hash []byte) error {
|
|
|
|
hashStr, err := encoding.NewMultihash(s.getPrefixedHash(hash)).ToBase64Url()
|
|
|
|
err = s.createBucketIfNotExists(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := s.httpApi.R().
|
|
|
|
SetPathParam("path", hashStr).
|
2024-01-21 03:51:00 +00:00
|
|
|
SetQueryParam("bucket", bucket).
|
2024-01-19 20:51:31 +00:00
|
|
|
SetBody(file).Put("/api/worker/objects/{path}")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.IsError() {
|
|
|
|
if resp.Error() != nil {
|
|
|
|
return resp.Error().(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New(resp.String())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-22 23:51:09 +00:00
|
|
|
func (s *StorageServiceImpl) BuildUploadBufferTus(basePath string, preUploadCb interfaces.TusPreUploadCreateCallback, preFinishCb interfaces.TusPreFinishResponseCallback) (*tusd.Handler, tusd.DataStore, *s3.Client, error) {
|
2024-01-19 20:51:31 +00:00
|
|
|
customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
|
|
|
|
if service == s3.ServiceID {
|
|
|
|
return aws.Endpoint{
|
|
|
|
URL: s.portal.Config().GetString("core.storage.s3.endpoint"),
|
|
|
|
SigningRegion: s.portal.Config().GetString("core.storage.s3.region"),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
|
|
|
|
})
|
|
|
|
|
|
|
|
cfg, err := config.LoadDefaultConfig(context.TODO(),
|
|
|
|
config.WithRegion("us-east-1"),
|
|
|
|
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(
|
|
|
|
s.portal.Config().GetString("core.storage.s3.accessKey"),
|
|
|
|
s.portal.Config().GetString("core.storage.s3.secretKey"),
|
|
|
|
"",
|
|
|
|
)),
|
|
|
|
config.WithEndpointResolverWithOptions(customResolver),
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-01-22 23:51:09 +00:00
|
|
|
return nil, nil, nil, nil
|
2024-01-19 20:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s3Client := s3.NewFromConfig(cfg)
|
|
|
|
|
|
|
|
store := s3store.New(s.portal.Config().GetString("core.storage.s3.bufferBucket"), s3Client)
|
|
|
|
|
|
|
|
locker := NewMySQLLocker(s)
|
|
|
|
|
|
|
|
composer := tusd.NewStoreComposer()
|
|
|
|
store.UseIn(composer)
|
|
|
|
composer.UseLocker(locker)
|
2024-01-15 13:38:05 +00:00
|
|
|
|
2024-01-19 20:51:31 +00:00
|
|
|
handler, err := tusd.NewHandler(tusd.Config{
|
|
|
|
BasePath: basePath,
|
|
|
|
StoreComposer: composer,
|
|
|
|
DisableDownload: true,
|
|
|
|
NotifyCompleteUploads: true,
|
|
|
|
NotifyTerminatedUploads: true,
|
2024-01-20 11:27:11 +00:00
|
|
|
NotifyCreatedUploads: true,
|
2024-01-19 20:51:31 +00:00
|
|
|
PreUploadCreateCallback: preUploadCb,
|
|
|
|
})
|
|
|
|
|
2024-01-22 23:51:09 +00:00
|
|
|
return handler, store, s3Client, err
|
2024-01-19 20:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StorageServiceImpl) Init() error {
|
2024-01-21 04:36:55 +00:00
|
|
|
client := req.NewClient()
|
2024-01-15 13:38:05 +00:00
|
|
|
|
|
|
|
client.SetBaseURL(s.portal.Config().GetString("core.sia.url"))
|
2024-01-21 04:36:55 +00:00
|
|
|
client.SetCommonBasicAuth("", s.portal.Config().GetString("core.sia.key"))
|
2024-01-21 05:19:04 +00:00
|
|
|
client.SetTimeout(24 * time.Hour)
|
2024-01-15 13:38:05 +00:00
|
|
|
|
|
|
|
s.httpApi = client
|
2024-01-19 20:51:31 +00:00
|
|
|
|
|
|
|
preUpload := func(hook tusd.HookEvent) (tusd.HTTPResponse, tusd.FileInfoChanges, error) {
|
|
|
|
blankResp := tusd.HTTPResponse{}
|
|
|
|
blankChanges := tusd.FileInfoChanges{}
|
|
|
|
|
|
|
|
hash, ok := hook.Upload.MetaData["hash"]
|
|
|
|
if !ok {
|
|
|
|
return blankResp, blankChanges, errors.New("missing hash")
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedHash, err := encoding.MultihashFromBase64Url(hash)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return blankResp, blankChanges, err
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, _ := s.FileExists(decodedHash.HashBytes())
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
return blankResp, blankChanges, errors.New("file already exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, _ = s.TusUploadExists(decodedHash.HashBytes())
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
return blankResp, blankChanges, errors.New("file is already being uploaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
return blankResp, blankChanges, nil
|
|
|
|
}
|
|
|
|
|
2024-01-22 23:53:31 +00:00
|
|
|
tus, store, s3client, err := s.BuildUploadBufferTus("/s5/upload/tus", preUpload, nil)
|
2024-01-19 20:51:31 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.tus = tus
|
|
|
|
s.tusStore = store
|
2024-01-22 23:53:31 +00:00
|
|
|
s.s3Client = s3client
|
2024-01-19 20:51:31 +00:00
|
|
|
|
2024-01-19 22:47:14 +00:00
|
|
|
s.portal.CronService().RegisterService(s)
|
|
|
|
|
2024-01-19 20:51:31 +00:00
|
|
|
go s.tusWorker()
|
|
|
|
|
|
|
|
return nil
|
2024-01-15 13:38:05 +00:00
|
|
|
}
|
2024-01-19 22:47:14 +00:00
|
|
|
func (s *StorageServiceImpl) LoadInitialTasks(cron interfaces.CronService) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-15 19:25:12 +00:00
|
|
|
func (s *StorageServiceImpl) createBucketIfNotExists(bucket string) error {
|
|
|
|
resp, err := s.httpApi.R().
|
|
|
|
SetPathParam("bucket", bucket).
|
|
|
|
Get("/api/bus/bucket/{bucket}")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-21 04:36:55 +00:00
|
|
|
if resp.StatusCode != 404 {
|
2024-01-15 19:25:12 +00:00
|
|
|
if resp.IsError() && resp.Error() != nil {
|
|
|
|
return resp.Error().(error)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resp, err := s.httpApi.R().
|
|
|
|
SetBody(map[string]string{
|
2024-01-15 19:36:06 +00:00
|
|
|
"name": bucket,
|
2024-01-15 19:25:12 +00:00
|
|
|
}).
|
2024-01-15 19:33:51 +00:00
|
|
|
Post("/api/bus/buckets")
|
2024-01-15 19:25:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.IsError() && resp.Error() != nil {
|
|
|
|
return resp.Error().(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-16 05:40:50 +00:00
|
|
|
|
2024-01-16 06:01:57 +00:00
|
|
|
func (s *StorageServiceImpl) FileExists(hash []byte) (bool, models.Upload) {
|
2024-01-16 05:58:51 +00:00
|
|
|
hashStr := hex.EncodeToString(hash)
|
2024-01-16 05:40:50 +00:00
|
|
|
|
2024-01-16 06:01:57 +00:00
|
|
|
var upload models.Upload
|
2024-01-16 06:29:29 +00:00
|
|
|
result := s.portal.Database().Model(&models.Upload{}).Where(&models.Upload{Hash: hashStr}).First(&upload)
|
2024-01-16 05:40:50 +00:00
|
|
|
|
2024-01-16 06:01:57 +00:00
|
|
|
return result.RowsAffected > 0, upload
|
2024-01-16 05:40:50 +00:00
|
|
|
}
|
2024-01-16 05:48:06 +00:00
|
|
|
|
2024-01-19 20:51:31 +00:00
|
|
|
func (s *StorageServiceImpl) GetHashSmall(file io.ReadSeeker) ([]byte, error) {
|
2024-01-16 05:48:06 +00:00
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
|
|
|
|
_, err := io.Copy(buf, file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-16 06:08:39 +00:00
|
|
|
hash := blake3.Sum256(buf.Bytes())
|
2024-01-16 05:48:06 +00:00
|
|
|
|
|
|
|
return hash[:], nil
|
|
|
|
}
|
2024-01-22 23:52:37 +00:00
|
|
|
func (s *StorageServiceImpl) GetHash(file io.Reader) ([]byte, int64, error) {
|
2024-01-19 20:51:31 +00:00
|
|
|
hasher := blake3.New(64, nil)
|
|
|
|
|
2024-01-22 23:52:37 +00:00
|
|
|
totalBytes, err := io.Copy(hasher, file)
|
2024-01-19 20:51:31 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2024-01-22 23:52:37 +00:00
|
|
|
return nil, 0, err
|
2024-01-19 20:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hash := hasher.Sum(nil)
|
|
|
|
|
2024-01-22 23:52:37 +00:00
|
|
|
return hash[:32], totalBytes, nil
|
2024-01-19 20:51:31 +00:00
|
|
|
}
|
|
|
|
|
2024-01-17 19:46:22 +00:00
|
|
|
func (s *StorageServiceImpl) CreateUpload(hash []byte, uploaderID uint, uploaderIP string, size uint64, protocol string) (*models.Upload, error) {
|
|
|
|
hashStr := hex.EncodeToString(hash)
|
|
|
|
|
|
|
|
upload := &models.Upload{
|
|
|
|
Hash: hashStr,
|
|
|
|
UserID: uploaderID,
|
|
|
|
UploaderIP: uploaderIP,
|
|
|
|
Protocol: protocol,
|
|
|
|
Size: size,
|
|
|
|
}
|
|
|
|
|
|
|
|
result := s.portal.Database().Create(upload)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
return nil, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return upload, nil
|
|
|
|
}
|
2024-01-19 20:51:31 +00:00
|
|
|
func (s *StorageServiceImpl) tusWorker() {
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case info := <-s.tus.CreatedUploads:
|
|
|
|
hash, ok := info.Upload.MetaData["hash"]
|
2024-01-20 11:41:51 +00:00
|
|
|
errorResponse := tusd.HTTPResponse{StatusCode: 400, Header: nil}
|
2024-01-19 20:51:31 +00:00
|
|
|
if !ok {
|
|
|
|
s.portal.Logger().Error("Missing hash in metadata")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-01-20 11:57:57 +00:00
|
|
|
uploaderID, ok := info.Context.Value(middleware.S5AuthUserIDKey).(uint64)
|
2024-01-19 20:51:31 +00:00
|
|
|
if !ok {
|
2024-01-20 11:41:51 +00:00
|
|
|
errorResponse.Body = "Missing user id in context"
|
|
|
|
info.Upload.StopUpload(errorResponse)
|
2024-01-19 20:51:31 +00:00
|
|
|
s.portal.Logger().Error("Missing user id in context")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
uploaderIP := info.HTTPRequest.RemoteAddr
|
|
|
|
|
|
|
|
decodedHash, err := encoding.MultihashFromBase64Url(hash)
|
|
|
|
|
|
|
|
if err != nil {
|
2024-01-20 11:41:51 +00:00
|
|
|
errorResponse.Body = "Could not decode hash"
|
|
|
|
info.Upload.StopUpload(errorResponse)
|
2024-01-19 20:51:31 +00:00
|
|
|
s.portal.Logger().Error("Could not decode hash", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-01-20 11:57:57 +00:00
|
|
|
_, err = s.CreateTusUpload(decodedHash.HashBytes(), info.Upload.ID, uint(uploaderID), uploaderIP, info.Context.Value("protocol").(string))
|
2024-01-19 20:51:31 +00:00
|
|
|
if err != nil {
|
2024-01-20 11:41:51 +00:00
|
|
|
errorResponse.Body = "Could not create tus upload"
|
|
|
|
info.Upload.StopUpload(errorResponse)
|
2024-01-19 20:51:31 +00:00
|
|
|
s.portal.Logger().Error("Could not create tus upload", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case info := <-s.tus.UploadProgress:
|
|
|
|
err := s.TusUploadProgress(info.Upload.ID)
|
|
|
|
if err != nil {
|
|
|
|
s.portal.Logger().Error("Could not update tus upload", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case info := <-s.tus.TerminatedUploads:
|
|
|
|
err := s.DeleteTusUpload(info.Upload.ID)
|
|
|
|
if err != nil {
|
|
|
|
s.portal.Logger().Error("Could not delete tus upload", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
case info := <-s.tus.CompleteUploads:
|
2024-01-22 23:02:15 +00:00
|
|
|
if !(!info.Upload.SizeIsDeferred && info.Upload.Offset == info.Upload.Size) {
|
2024-01-22 22:49:42 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
err := s.TusUploadCompleted(info.Upload.ID)
|
|
|
|
if err != nil {
|
|
|
|
s.portal.Logger().Error("Could not complete tus upload", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = s.ScheduleTusUpload(info.Upload.ID, 0)
|
2024-01-19 20:51:31 +00:00
|
|
|
if err != nil {
|
|
|
|
s.portal.Logger().Error("Could not schedule tus upload", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-20 12:05:27 +00:00
|
|
|
func (s *StorageServiceImpl) TusUploadExists(hash []byte) (bool, models.TusUpload) {
|
2024-01-19 20:51:31 +00:00
|
|
|
hashStr := hex.EncodeToString(hash)
|
|
|
|
|
2024-01-20 12:05:27 +00:00
|
|
|
var upload models.TusUpload
|
|
|
|
result := s.portal.Database().Model(&models.TusUpload{}).Where(&models.TusUpload{Hash: hashStr}).First(&upload)
|
2024-01-19 20:51:31 +00:00
|
|
|
|
|
|
|
return result.RowsAffected > 0, upload
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StorageServiceImpl) CreateTusUpload(hash []byte, uploadID string, uploaderID uint, uploaderIP string, protocol string) (*models.TusUpload, error) {
|
|
|
|
hashStr := hex.EncodeToString(hash)
|
|
|
|
|
|
|
|
upload := &models.TusUpload{
|
|
|
|
Hash: hashStr,
|
|
|
|
UploadID: uploadID,
|
|
|
|
UploaderID: uploaderID,
|
|
|
|
UploaderIP: uploaderIP,
|
|
|
|
Uploader: models.User{},
|
|
|
|
Protocol: protocol,
|
|
|
|
}
|
|
|
|
|
|
|
|
result := s.portal.Database().Create(upload)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
return nil, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return upload, nil
|
|
|
|
}
|
|
|
|
func (s *StorageServiceImpl) TusUploadProgress(uploadID string) error {
|
|
|
|
|
|
|
|
find := &models.TusUpload{UploadID: uploadID}
|
|
|
|
|
|
|
|
var upload models.TusUpload
|
|
|
|
result := s.portal.Database().Model(&models.TusUpload{}).Where(find).First(&upload)
|
|
|
|
|
|
|
|
if result.RowsAffected == 0 {
|
|
|
|
return errors.New("upload not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
result = s.portal.Database().Model(&models.TusUpload{}).Where(find).Update("updated_at", time.Now())
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-22 22:49:03 +00:00
|
|
|
func (s *StorageServiceImpl) TusUploadCompleted(uploadID string) error {
|
|
|
|
|
|
|
|
find := &models.TusUpload{UploadID: uploadID}
|
|
|
|
|
|
|
|
var upload models.TusUpload
|
|
|
|
result := s.portal.Database().Model(&models.TusUpload{}).Where(find).First(&upload)
|
|
|
|
|
|
|
|
if result.RowsAffected == 0 {
|
|
|
|
return errors.New("upload not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
result = s.portal.Database().Model(&models.TusUpload{}).Where(find).Update("completed", true)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-19 20:51:31 +00:00
|
|
|
func (s *StorageServiceImpl) DeleteTusUpload(uploadID string) error {
|
2024-01-21 06:06:14 +00:00
|
|
|
result := s.portal.Database().Where(&models.TusUpload{UploadID: uploadID}).Delete(&models.TusUpload{})
|
2024-01-19 20:51:31 +00:00
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StorageServiceImpl) ScheduleTusUpload(uploadID string, attempt int) error {
|
|
|
|
find := &models.TusUpload{UploadID: uploadID}
|
|
|
|
|
|
|
|
var upload models.TusUpload
|
|
|
|
result := s.portal.Database().Model(&models.TusUpload{}).Where(find).First(&upload)
|
|
|
|
|
|
|
|
if result.RowsAffected == 0 {
|
|
|
|
return errors.New("upload not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
job, task := s.buildNewTusUploadTask(&upload)
|
|
|
|
|
|
|
|
if attempt > 0 {
|
|
|
|
job = gocron.OneTimeJob(gocron.OneTimeJobStartDateTime(time.Now().Add(time.Duration(attempt) * time.Minute)))
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := s.portal.Cron().NewJob(job, task, gocron.WithEventListeners(gocron.AfterJobRunsWithError(func(jobID uuid.UUID, jobName string, err error) {
|
|
|
|
s.portal.Logger().Error("Error running job", zap.Error(err))
|
|
|
|
err = s.ScheduleTusUpload(uploadID, attempt+1)
|
|
|
|
if err != nil {
|
|
|
|
s.portal.Logger().Error("Error rescheduling job", zap.Error(err))
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
gocron.AfterJobRuns(func(jobID uuid.UUID, jobName string) {
|
|
|
|
s.portal.Logger().Info("Job finished", zap.String("jobName", jobName), zap.String("uploadID", uploadID))
|
|
|
|
err := s.DeleteTusUpload(uploadID)
|
|
|
|
if err != nil {
|
|
|
|
s.portal.Logger().Error("Error deleting tus upload", zap.Error(err))
|
|
|
|
}
|
|
|
|
})))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StorageServiceImpl) buildNewTusUploadTask(upload *models.TusUpload) (job gocron.JobDefinition, task gocron.Task) {
|
|
|
|
job = gocron.OneTimeJob(gocron.OneTimeJobStartImmediately())
|
|
|
|
|
|
|
|
task = gocron.NewTask(
|
|
|
|
func(upload *models.TusUpload) error {
|
|
|
|
ctx := context.Background()
|
|
|
|
tusUpload, err := s.tusStore.GetUpload(ctx, upload.UploadID)
|
|
|
|
if err != nil {
|
|
|
|
s.portal.Logger().Error("Could not get upload", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reader, err := tusUpload.GetReader(ctx)
|
|
|
|
if err != nil {
|
|
|
|
s.portal.Logger().Error("Could not get tus file", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-22 23:52:37 +00:00
|
|
|
hash, byteCount, err := s.GetHash(reader)
|
2024-01-19 20:51:31 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
s.portal.Logger().Error("Could not compute hash", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dbHash, err := hex.DecodeString(upload.Hash)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
s.portal.Logger().Error("Could not decode hash", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(hash, dbHash) {
|
|
|
|
s.portal.Logger().Error("Hashes do not match", zap.Any("upload", upload), zap.Any("hash", hash), zap.Any("dbHash", dbHash))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-20 17:26:31 +00:00
|
|
|
reader, err = tusUpload.GetReader(ctx)
|
|
|
|
if err != nil {
|
|
|
|
s.portal.Logger().Error("Could not get tus file", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-21 05:48:50 +00:00
|
|
|
err = s.PutFile(reader, upload.Protocol, dbHash)
|
2024-01-19 20:51:31 +00:00
|
|
|
|
2024-01-21 05:08:49 +00:00
|
|
|
if err != nil {
|
|
|
|
s.portal.Logger().Error("Could not upload file", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-22 23:54:19 +00:00
|
|
|
s3InfoId, _ := splitS3Ids(upload.UploadID)
|
|
|
|
s3InfoId = s3InfoId + ".info"
|
|
|
|
|
|
|
|
_, err = s.s3Client.DeleteObject(ctx, &s3.DeleteObjectInput{
|
|
|
|
Bucket: aws.String(s.portal.Config().GetString("core.storage.s3.bufferBucket")),
|
|
|
|
Key: aws.String(s3InfoId),
|
|
|
|
})
|
|
|
|
|
2024-01-22 23:25:11 +00:00
|
|
|
if err != nil {
|
2024-01-22 23:54:19 +00:00
|
|
|
s.portal.Logger().Error("Could not delete upload metadata", zap.Error(err))
|
2024-01-22 23:25:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-22 23:52:37 +00:00
|
|
|
_, err = s.CreateUpload(dbHash, upload.UploaderID, upload.UploaderIP, uint64(byteCount), upload.Protocol)
|
2024-01-22 23:25:11 +00:00
|
|
|
if err != nil {
|
|
|
|
s.portal.Logger().Error("Could not create upload", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-19 20:51:31 +00:00
|
|
|
return nil
|
|
|
|
}, upload)
|
|
|
|
|
|
|
|
return job, task
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StorageServiceImpl) getPrefixedHash(hash []byte) []byte {
|
|
|
|
return append([]byte{byte(types.HashTypeBlake3)}, hash...)
|
|
|
|
}
|
2024-01-22 23:54:19 +00:00
|
|
|
|
|
|
|
func splitS3Ids(id string) (objectId, multipartId string) {
|
|
|
|
index := strings.Index(id, "+")
|
|
|
|
if index == -1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
objectId = id[:index]
|
|
|
|
multipartId = id[index+1:]
|
|
|
|
return
|
|
|
|
}
|