2023-05-10 18:28:32 +00:00
|
|
|
package files
|
|
|
|
|
|
|
|
import (
|
2023-05-22 15:00:24 +00:00
|
|
|
"bytes"
|
2023-05-16 22:42:03 +00:00
|
|
|
"context"
|
2023-05-10 18:28:32 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"git.lumeweb.com/LumeWeb/portal/bao"
|
|
|
|
"git.lumeweb.com/LumeWeb/portal/db"
|
|
|
|
"git.lumeweb.com/LumeWeb/portal/model"
|
|
|
|
"git.lumeweb.com/LumeWeb/portal/renterd"
|
2023-05-16 22:42:03 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/shared"
|
2023-05-10 18:28:32 +00:00
|
|
|
"github.com/go-resty/resty/v2"
|
2023-05-19 13:04:47 +00:00
|
|
|
"go.uber.org/zap"
|
2023-05-10 18:28:32 +00:00
|
|
|
"io"
|
2023-05-19 13:04:47 +00:00
|
|
|
"strings"
|
2023-05-10 18:28:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var client *resty.Client
|
|
|
|
|
|
|
|
func Init() {
|
|
|
|
client = resty.New()
|
|
|
|
client.SetBaseURL(renterd.GetApiAddr() + "/api")
|
|
|
|
client.SetBasicAuth("", renterd.GetAPIPassword())
|
|
|
|
client.SetDisableWarn(true)
|
|
|
|
}
|
|
|
|
|
2023-05-22 15:00:24 +00:00
|
|
|
func Upload(r io.ReadSeeker, size int64, hash []byte) (model.Upload, error) {
|
2023-05-10 18:28:32 +00:00
|
|
|
var upload model.Upload
|
|
|
|
|
2023-05-17 13:52:25 +00:00
|
|
|
tree, hashBytes, err := bao.ComputeTree(r, size)
|
2023-05-15 19:47:34 +00:00
|
|
|
|
2023-05-10 18:28:32 +00:00
|
|
|
if err != nil {
|
2023-05-19 13:04:47 +00:00
|
|
|
shared.GetLogger().Error("Failed to hash file", zap.Error(err))
|
2023-05-10 18:28:32 +00:00
|
|
|
return upload, err
|
|
|
|
}
|
|
|
|
|
2023-05-22 15:00:24 +00:00
|
|
|
if hash != nil {
|
|
|
|
if bytes.Compare(hashBytes[:], hash) != 0 {
|
|
|
|
shared.GetLogger().Error("File hash does not match provided file hash")
|
|
|
|
return upload, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-10 18:28:32 +00:00
|
|
|
hashHex := hex.EncodeToString(hashBytes[:])
|
|
|
|
|
2023-05-17 13:52:25 +00:00
|
|
|
_, err = r.Seek(0, io.SeekStart)
|
2023-05-15 19:47:34 +00:00
|
|
|
|
2023-05-10 18:28:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return upload, err
|
|
|
|
}
|
|
|
|
|
2023-05-11 19:25:31 +00:00
|
|
|
result := db.Get().Where(&model.Upload{Hash: hashHex}).First(&upload)
|
2023-05-10 18:28:32 +00:00
|
|
|
if (result.Error != nil && result.Error.Error() != "record not found") || result.RowsAffected > 0 {
|
|
|
|
err := result.Row().Scan(&upload)
|
|
|
|
if err != nil {
|
2023-05-19 13:04:47 +00:00
|
|
|
shared.GetLogger().Error("Failed to query uploads table", zap.Error(err))
|
2023-05-10 18:28:32 +00:00
|
|
|
return upload, err
|
|
|
|
}
|
2023-05-22 15:02:47 +00:00
|
|
|
|
|
|
|
if result.RowsAffected > 0 && upload.ID > 0 {
|
|
|
|
shared.GetLogger().Info("Upload already exists")
|
|
|
|
return upload, nil
|
|
|
|
}
|
2023-05-10 18:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
objectExistsResult, err := client.R().Get(fmt.Sprintf("/worker/objects/%s", hashHex))
|
|
|
|
|
|
|
|
if err != nil {
|
2023-05-19 13:04:47 +00:00
|
|
|
shared.GetLogger().Error("Failed query object", zap.Error(err))
|
2023-05-10 18:28:32 +00:00
|
|
|
return upload, err
|
|
|
|
}
|
|
|
|
|
2023-05-19 13:05:40 +00:00
|
|
|
statusCode := objectExistsResult.StatusCode()
|
|
|
|
|
|
|
|
if statusCode == 500 {
|
|
|
|
bodyErr := objectExistsResult.String()
|
|
|
|
if !strings.Contains(bodyErr, "no slabs found") {
|
|
|
|
shared.GetLogger().Error("Failed fetching object", zap.String("error", objectExistsResult.String()))
|
|
|
|
return upload, errors.New(fmt.Sprintf("error fetching file: %s", objectExistsResult.String()))
|
|
|
|
}
|
2023-05-17 17:33:22 +00:00
|
|
|
|
2023-05-19 13:05:40 +00:00
|
|
|
statusCode = 404
|
2023-05-10 18:28:32 +00:00
|
|
|
}
|
|
|
|
|
2023-05-19 13:05:40 +00:00
|
|
|
if statusCode != 404 {
|
|
|
|
msg := "file already exists in network, but missing in database"
|
|
|
|
shared.GetLogger().Error(msg)
|
|
|
|
return upload, errors.New(msg)
|
2023-05-10 18:28:32 +00:00
|
|
|
}
|
|
|
|
|
2023-05-17 13:52:25 +00:00
|
|
|
ret, err := client.R().SetBody(r).Put(fmt.Sprintf("/worker/objects/%s", hashHex))
|
2023-05-10 18:28:32 +00:00
|
|
|
if ret.StatusCode() != 200 {
|
2023-05-19 13:04:47 +00:00
|
|
|
shared.GetLogger().Error("Failed uploading object", zap.String("error", ret.String()))
|
|
|
|
err = errors.New(ret.String())
|
2023-05-10 18:28:32 +00:00
|
|
|
return upload, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, err = client.R().SetBody(tree).Put(fmt.Sprintf("/worker/objects/%s.obao", hashHex))
|
|
|
|
if ret.StatusCode() != 200 {
|
2023-05-19 13:04:47 +00:00
|
|
|
shared.GetLogger().Error("Failed uploading proof", zap.String("error", ret.String()))
|
|
|
|
err = errors.New(ret.String())
|
2023-05-10 18:28:32 +00:00
|
|
|
return upload, err
|
|
|
|
}
|
|
|
|
|
|
|
|
upload = model.Upload{
|
|
|
|
Hash: hashHex,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = db.Get().Create(&upload).Error; err != nil {
|
2023-05-19 13:04:47 +00:00
|
|
|
shared.GetLogger().Error("Failed adding upload to db", zap.Error(err))
|
2023-05-10 18:28:32 +00:00
|
|
|
return upload, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return upload, nil
|
|
|
|
}
|
2023-05-10 18:41:12 +00:00
|
|
|
func Download(hash string) (io.Reader, error) {
|
2023-05-16 22:42:03 +00:00
|
|
|
uploadItem := db.Get().Table("uploads").Where(&model.Upload{Hash: hash}).Row()
|
|
|
|
tusItem := db.Get().Table("tus").Where(&model.Tus{Hash: hash}).Row()
|
2023-05-10 18:41:12 +00:00
|
|
|
|
2023-05-16 22:42:03 +00:00
|
|
|
if uploadItem.Err() == nil {
|
|
|
|
fetch, err := client.R().SetDoNotParseResponse(true).Get(fmt.Sprintf("/worker/objects/%s", hash))
|
|
|
|
if err != nil {
|
2023-05-19 13:04:47 +00:00
|
|
|
shared.GetLogger().Error("Failed downloading object", zap.Error(err))
|
2023-05-16 22:42:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-10 18:41:12 +00:00
|
|
|
|
2023-05-16 22:42:03 +00:00
|
|
|
return fetch.RawBody(), nil
|
|
|
|
} else if tusItem.Err() == nil {
|
|
|
|
var tusData model.Tus
|
|
|
|
err := tusItem.Scan(&tusData)
|
|
|
|
if err != nil {
|
2023-05-19 13:04:47 +00:00
|
|
|
shared.GetLogger().Error("Failed querying upload from db", zap.Error(err))
|
2023-05-16 22:42:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-10 18:41:12 +00:00
|
|
|
|
2023-05-22 14:59:16 +00:00
|
|
|
upload, err := shared.GetTusStore().GetUpload(context.Background(), tusData.UploadID)
|
2023-05-16 22:42:03 +00:00
|
|
|
if err != nil {
|
2023-05-19 13:04:47 +00:00
|
|
|
shared.GetLogger().Error("Failed querying tus upload", zap.Error(err))
|
2023-05-16 22:42:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
reader, err := upload.GetReader(context.Background())
|
|
|
|
if err != nil {
|
2023-05-19 13:04:47 +00:00
|
|
|
shared.GetLogger().Error("Failed reading tus upload", zap.Error(err))
|
2023-05-16 22:42:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reader, nil
|
|
|
|
} else {
|
2023-05-19 13:04:47 +00:00
|
|
|
shared.GetLogger().Error("invalid file")
|
2023-05-16 22:42:03 +00:00
|
|
|
return nil, errors.New("invalid file")
|
|
|
|
}
|
2023-05-10 18:41:12 +00:00
|
|
|
}
|