refactor: have the Download method check for a tus upload thats still in progress and use it if a upload item does not exist

This commit is contained in:
Derrick Hammer 2023-05-16 18:42:03 -04:00
parent 4548de5c60
commit 55d8dda6e8
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 31 additions and 10 deletions

View File

@ -2,6 +2,7 @@ package files
import ( import (
"bufio" "bufio"
"context"
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
@ -9,6 +10,7 @@ import (
"git.lumeweb.com/LumeWeb/portal/db" "git.lumeweb.com/LumeWeb/portal/db"
"git.lumeweb.com/LumeWeb/portal/model" "git.lumeweb.com/LumeWeb/portal/model"
"git.lumeweb.com/LumeWeb/portal/renterd" "git.lumeweb.com/LumeWeb/portal/renterd"
"git.lumeweb.com/LumeWeb/portal/shared"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
"io" "io"
"lukechampine.com/blake3" "lukechampine.com/blake3"
@ -134,16 +136,35 @@ func Upload(r io.ReadSeeker, file *os.File) (model.Upload, error) {
return upload, nil return upload, nil
} }
func Download(hash string) (io.Reader, error) { func Download(hash string) (io.Reader, error) {
result := db.Get().Table("uploads").Where(&model.Upload{Hash: hash}).Row() uploadItem := db.Get().Table("uploads").Where(&model.Upload{Hash: hash}).Row()
tusItem := db.Get().Table("tus").Where(&model.Tus{Hash: hash}).Row()
if result.Err() != nil { if uploadItem.Err() == nil {
return nil, result.Err() fetch, err := client.R().SetDoNotParseResponse(true).Get(fmt.Sprintf("/worker/objects/%s", hash))
if err != nil {
return nil, err
}
return fetch.RawBody(), nil
} else if tusItem.Err() == nil {
var tusData model.Tus
err := tusItem.Scan(&tusData)
if err != nil {
return nil, err
}
upload, err := shared.GetTusStore().GetUpload(context.Background(), tusData.Id)
if err != nil {
return nil, err
}
reader, err := upload.GetReader(context.Background())
if err != nil {
return nil, err
}
return reader, nil
} else {
return nil, errors.New("invalid file")
} }
fetch, err := client.R().SetDoNotParseResponse(true).Get(fmt.Sprintf("/worker/objects/%s", hash))
if err != nil {
return nil, err
}
return fetch.RawBody(), nil
} }