Compare commits

..

5 Commits

6 changed files with 55 additions and 28 deletions

View File

@ -26,7 +26,7 @@ func (f *FilesController) PostUpload() {
return return
} }
upload, err := files.Upload(file, meta.Size) upload, err := files.Upload(file, meta.Size, nil)
if internalError(ctx, err) { if internalError(ctx, err) {
shared.GetLogger().Debug("failed uploading file", zap.Error(err)) shared.GetLogger().Debug("failed uploading file", zap.Error(err))

4
go.mod
View File

@ -18,9 +18,9 @@ require (
gitlab.com/NebulousLabs/encoding v0.0.0-20200604091946-456c3dc907fe gitlab.com/NebulousLabs/encoding v0.0.0-20200604091946-456c3dc907fe
go.sia.tech/core v0.1.12-0.20230503202148-581dd00ac1d2 go.sia.tech/core v0.1.12-0.20230503202148-581dd00ac1d2
go.sia.tech/jape v0.9.0 go.sia.tech/jape v0.9.0
go.sia.tech/renterd v0.3.0-beta.0.20230516200305-8097423dbe64 go.sia.tech/renterd v0.3.0-beta.0.20230520152334-e004ada9c4e9
go.sia.tech/siad v1.5.10-0.20230228235644-3059c0b930ca go.sia.tech/siad v1.5.10-0.20230228235644-3059c0b930ca
go.sia.tech/web/renterd v0.15.0 go.sia.tech/web/renterd v0.17.0
go.uber.org/zap v1.24.0 go.uber.org/zap v1.24.0
golang.org/x/crypto v0.8.0 golang.org/x/crypto v0.8.0
golang.org/x/term v0.8.0 golang.org/x/term v0.8.0

4
go.sum
View File

@ -1206,12 +1206,16 @@ go.sia.tech/mux v1.2.0 h1:ofa1Us9mdymBbGMY2XH/lSpY8itFsKIo/Aq8zwe+GHU=
go.sia.tech/mux v1.2.0/go.mod h1:Yyo6wZelOYTyvrHmJZ6aQfRoer3o4xyKQ4NmQLJrBSo= go.sia.tech/mux v1.2.0/go.mod h1:Yyo6wZelOYTyvrHmJZ6aQfRoer3o4xyKQ4NmQLJrBSo=
go.sia.tech/renterd v0.3.0-beta.0.20230516200305-8097423dbe64 h1:qA+aREwc+i8Q56F9VL9wdxagmtoPKcAd6MVZKOt0JRI= go.sia.tech/renterd v0.3.0-beta.0.20230516200305-8097423dbe64 h1:qA+aREwc+i8Q56F9VL9wdxagmtoPKcAd6MVZKOt0JRI=
go.sia.tech/renterd v0.3.0-beta.0.20230516200305-8097423dbe64/go.mod h1:jFxggAqLQ9fs85iLpC7s2Xnit3rTx7AajW37LgR1vQs= go.sia.tech/renterd v0.3.0-beta.0.20230516200305-8097423dbe64/go.mod h1:jFxggAqLQ9fs85iLpC7s2Xnit3rTx7AajW37LgR1vQs=
go.sia.tech/renterd v0.3.0-beta.0.20230520152334-e004ada9c4e9 h1:jfxmpr/8UG9IDoU/vA4Jqq2cqWT9SsDJv06uZ48W/qs=
go.sia.tech/renterd v0.3.0-beta.0.20230520152334-e004ada9c4e9/go.mod h1:ln0uIpeEvgd0lhDx1yB2+u6WXUMermu1QO6lI96xJKI=
go.sia.tech/siad v1.5.10-0.20230228235644-3059c0b930ca h1:aZMg2AKevn7jKx+wlusWQfwSM5pNU9aGtRZme29q3O4= go.sia.tech/siad v1.5.10-0.20230228235644-3059c0b930ca h1:aZMg2AKevn7jKx+wlusWQfwSM5pNU9aGtRZme29q3O4=
go.sia.tech/siad v1.5.10-0.20230228235644-3059c0b930ca/go.mod h1:h/1afFwpxzff6/gG5i1XdAgPK7dEY6FaibhK7N5F86Y= go.sia.tech/siad v1.5.10-0.20230228235644-3059c0b930ca/go.mod h1:h/1afFwpxzff6/gG5i1XdAgPK7dEY6FaibhK7N5F86Y=
go.sia.tech/web/renterd v0.14.0 h1:74WDPNYXk71d8uT86rkQAa7AlDp8+VDRsQ2oyhwPplg= go.sia.tech/web/renterd v0.14.0 h1:74WDPNYXk71d8uT86rkQAa7AlDp8+VDRsQ2oyhwPplg=
go.sia.tech/web/renterd v0.14.0/go.mod h1:jr4PVQW1KU8JpAzmJRfFecDeJ5SPIRrKM3OKZ+FvGvE= go.sia.tech/web/renterd v0.14.0/go.mod h1:jr4PVQW1KU8JpAzmJRfFecDeJ5SPIRrKM3OKZ+FvGvE=
go.sia.tech/web/renterd v0.15.0 h1:GeaXbvrgcq1RseRMmHFlHW8LkfPLfANwYvr2XKwpHQI= go.sia.tech/web/renterd v0.15.0 h1:GeaXbvrgcq1RseRMmHFlHW8LkfPLfANwYvr2XKwpHQI=
go.sia.tech/web/renterd v0.15.0/go.mod h1:jr4PVQW1KU8JpAzmJRfFecDeJ5SPIRrKM3OKZ+FvGvE= go.sia.tech/web/renterd v0.15.0/go.mod h1:jr4PVQW1KU8JpAzmJRfFecDeJ5SPIRrKM3OKZ+FvGvE=
go.sia.tech/web/renterd v0.17.0 h1:s1k/R9Mbuxq1aRFN+C0ASmLP7/BvQRCpPBqbYFBv7wc=
go.sia.tech/web/renterd v0.17.0/go.mod h1:jr4PVQW1KU8JpAzmJRfFecDeJ5SPIRrKM3OKZ+FvGvE=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE= go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=

View File

@ -6,7 +6,7 @@ import (
type Tus struct { type Tus struct {
gorm.Model gorm.Model
UploadID string `gorm:"primaryKey"` ID uint64 `gorm:"primaryKey"`
Id string UploadID string
Hash string Hash string
} }

View File

@ -1,6 +1,7 @@
package files package files
import ( import (
"bytes"
"context" "context"
"encoding/hex" "encoding/hex"
"errors" "errors"
@ -25,7 +26,7 @@ func Init() {
client.SetDisableWarn(true) client.SetDisableWarn(true)
} }
func Upload(r io.ReadSeeker, size int64) (model.Upload, error) { func Upload(r io.ReadSeeker, size int64, hash []byte) (model.Upload, error) {
var upload model.Upload var upload model.Upload
tree, hashBytes, err := bao.ComputeTree(r, size) tree, hashBytes, err := bao.ComputeTree(r, size)
@ -35,6 +36,13 @@ func Upload(r io.ReadSeeker, size int64) (model.Upload, error) {
return upload, err return upload, err
} }
if hash != nil {
if bytes.Compare(hashBytes[:], hash) != 0 {
shared.GetLogger().Error("File hash does not match provided file hash")
return upload, err
}
}
hashHex := hex.EncodeToString(hashBytes[:]) hashHex := hex.EncodeToString(hashBytes[:])
_, err = r.Seek(0, io.SeekStart) _, err = r.Seek(0, io.SeekStart)
@ -50,6 +58,11 @@ func Upload(r io.ReadSeeker, size int64) (model.Upload, error) {
shared.GetLogger().Error("Failed to query uploads table", zap.Error(err)) shared.GetLogger().Error("Failed to query uploads table", zap.Error(err))
return upload, err return upload, err
} }
if result.RowsAffected > 0 && upload.ID > 0 {
shared.GetLogger().Info("Upload already exists")
return upload, nil
}
} }
objectExistsResult, err := client.R().Get(fmt.Sprintf("/worker/objects/%s", hashHex)) objectExistsResult, err := client.R().Get(fmt.Sprintf("/worker/objects/%s", hashHex))
@ -122,7 +135,7 @@ func Download(hash string) (io.Reader, error) {
return nil, err return nil, err
} }
upload, err := shared.GetTusStore().GetUpload(context.Background(), tusData.Id) upload, err := shared.GetTusStore().GetUpload(context.Background(), tusData.UploadID)
if err != nil { if err != nil {
shared.GetLogger().Error("Failed querying tus upload", zap.Error(err)) shared.GetLogger().Error("Failed querying tus upload", zap.Error(err))
return nil, err return nil, err

View File

@ -79,7 +79,7 @@ func Init() *tusd.Handler {
}, },
PreFinishResponseCallback: func(hook tusd.HookEvent) error { PreFinishResponseCallback: func(hook tusd.HookEvent) error {
tusEntry := &model.Tus{ tusEntry := &model.Tus{
Id: hook.Upload.ID, UploadID: hook.Upload.ID,
Hash: hook.Upload.MetaData[HASH_META_HEADER], Hash: hook.Upload.MetaData[HASH_META_HEADER],
} }
@ -147,32 +147,26 @@ func tusWorker(upload *tusd.Upload) error {
return err return err
} }
_, err = files.Upload(file.(io.ReadSeeker), info.Size) hashHex := info.MetaData[HASH_META_HEADER]
hashBytes, err := hex.DecodeString(hashHex)
if err != nil { if err != nil {
err1 := terminateUpload(*upload) shared.GetLogger().Error("failed decoding hash", zap.Error(err))
if err1 != nil { tErr := terminateUpload(*upload)
return err1
if tErr != nil {
return tErr
} }
return err return err
} }
hash := info.MetaData[HASH_META_HEADER] _, err = files.Upload(file.(io.ReadSeeker), info.Size, hashBytes)
tErr := terminateUpload(*upload)
var tusUpload model.Tus if tErr != nil {
ret := db.Get().Where(&model.Tus{Hash: hash}).First(&tusUpload) return tErr
if ret.Error != nil && ret.Error.Error() != "record not found" {
shared.GetLogger().Error("failed fetching tus entry", zap.Error(err))
err1 := terminateUpload(*upload)
if err1 != nil {
return err1
} }
return err
}
_ = db.Get().Delete(&tusUpload)
err = terminateUpload(*upload)
if err != nil { if err != nil {
return err return err
@ -182,10 +176,26 @@ func tusWorker(upload *tusd.Upload) error {
} }
func terminateUpload(upload tusd.Upload) error { func terminateUpload(upload tusd.Upload) error {
info, _ := upload.GetInfo(context.Background())
err := shared.GetTusComposer().Terminater.AsTerminatableUpload(upload).Terminate(context.Background()) err := shared.GetTusComposer().Terminater.AsTerminatableUpload(upload).Terminate(context.Background())
if err != nil { if err != nil {
shared.GetLogger().Error("failed deleting tus upload", zap.Error(err)) shared.GetLogger().Error("failed deleting tus upload", zap.Error(err))
}
tusUpload := &model.Tus{UploadID: info.ID}
ret := db.Get().Where(tusUpload).First(&tusUpload)
if ret.Error != nil && ret.Error.Error() != "record not found" {
shared.GetLogger().Error("failed fetching tus entry", zap.Error(err))
err = ret.Error
}
err1 := db.Get().Where(&tusUpload).Delete(&tusUpload)
_ = err1
if err != nil {
return err return err
} }