refactor: create new files package with Upload
This commit is contained in:
parent
2dae0c8687
commit
a93add8f70
8
main.go
8
main.go
|
@ -7,12 +7,14 @@ import (
|
||||||
"git.lumeweb.com/LumeWeb/portal/db"
|
"git.lumeweb.com/LumeWeb/portal/db"
|
||||||
_ "git.lumeweb.com/LumeWeb/portal/docs"
|
_ "git.lumeweb.com/LumeWeb/portal/docs"
|
||||||
"git.lumeweb.com/LumeWeb/portal/renterd"
|
"git.lumeweb.com/LumeWeb/portal/renterd"
|
||||||
|
"git.lumeweb.com/LumeWeb/portal/service/files"
|
||||||
"git.lumeweb.com/LumeWeb/portal/validator"
|
"git.lumeweb.com/LumeWeb/portal/validator"
|
||||||
"github.com/iris-contrib/swagger"
|
"github.com/iris-contrib/swagger"
|
||||||
"github.com/iris-contrib/swagger/swaggerFiles"
|
"github.com/iris-contrib/swagger/swaggerFiles"
|
||||||
"github.com/kataras/iris/v12"
|
"github.com/kataras/iris/v12"
|
||||||
"github.com/kataras/iris/v12/mvc"
|
"github.com/kataras/iris/v12/mvc"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Embed a directory of static files for serving from the app's root path
|
// Embed a directory of static files for serving from the app's root path
|
||||||
|
@ -45,7 +47,7 @@ func main() {
|
||||||
|
|
||||||
renterd.Ready()
|
renterd.Ready()
|
||||||
|
|
||||||
controller.InitFiles()
|
files.Init()
|
||||||
|
|
||||||
// Create a new Iris app instance
|
// Create a new Iris app instance
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
@ -74,6 +76,10 @@ func main() {
|
||||||
app.Handle(new(controller.FilesController))
|
app.Handle(new(controller.FilesController))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
tus := initTus()
|
||||||
|
|
||||||
|
app.Any(API_PATH+"{fileparam:path}", iris.FromStd(http.StripPrefix(API_PATH, tus)))
|
||||||
|
|
||||||
swaggerConfig := swagger.Config{
|
swaggerConfig := swagger.Config{
|
||||||
// The url pointing to API definition.
|
// The url pointing to API definition.
|
||||||
URL: "http://localhost:8080/swagger/doc.json",
|
URL: "http://localhost:8080/swagger/doc.json",
|
||||||
|
|
|
@ -0,0 +1,97 @@
|
||||||
|
package files
|
||||||
|
|
||||||
|
import (
|
||||||
|
"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"
|
||||||
|
"github.com/go-resty/resty/v2"
|
||||||
|
"io"
|
||||||
|
"lukechampine.com/blake3"
|
||||||
|
)
|
||||||
|
|
||||||
|
var client *resty.Client
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
client = resty.New()
|
||||||
|
client.SetBaseURL(renterd.GetApiAddr() + "/api")
|
||||||
|
client.SetBasicAuth("", renterd.GetAPIPassword())
|
||||||
|
client.SetDisableWarn(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Upload(r io.ReadSeeker) (model.Upload, error) {
|
||||||
|
var upload model.Upload
|
||||||
|
|
||||||
|
hasher := blake3.New(0, nil)
|
||||||
|
|
||||||
|
_, err := io.Copy(hasher, r)
|
||||||
|
if err != nil {
|
||||||
|
return upload, err
|
||||||
|
}
|
||||||
|
|
||||||
|
hashBytes := hasher.Sum(nil)
|
||||||
|
hashHex := hex.EncodeToString(hashBytes[:])
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return upload, err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = r.Seek(0, io.SeekStart)
|
||||||
|
if err != nil {
|
||||||
|
return upload, err
|
||||||
|
}
|
||||||
|
|
||||||
|
result := db.Get().Where("hash = ?", hashHex).First(&upload)
|
||||||
|
if (result.Error != nil && result.Error.Error() != "record not found") || result.RowsAffected > 0 {
|
||||||
|
err := result.Row().Scan(&upload)
|
||||||
|
if err != nil {
|
||||||
|
return upload, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
objectExistsResult, err := client.R().Get(fmt.Sprintf("/worker/objects/%s", hashHex))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return upload, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if objectExistsResult.StatusCode() != 404 {
|
||||||
|
return upload, errors.New("file already exists in network, but missing in database")
|
||||||
|
}
|
||||||
|
|
||||||
|
tree, err := bao.ComputeBaoTree(r)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return upload, err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = r.Seek(0, io.SeekStart)
|
||||||
|
if err != nil {
|
||||||
|
return upload, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ret, err := client.R().SetBody(r).Put(fmt.Sprintf("/worker/objects/%s", hashHex))
|
||||||
|
if ret.StatusCode() != 200 {
|
||||||
|
err = errors.New(string(ret.Body()))
|
||||||
|
return upload, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ret, err = client.R().SetBody(tree).Put(fmt.Sprintf("/worker/objects/%s.obao", hashHex))
|
||||||
|
if ret.StatusCode() != 200 {
|
||||||
|
err = errors.New(string(ret.Body()))
|
||||||
|
return upload, err
|
||||||
|
}
|
||||||
|
|
||||||
|
upload = model.Upload{
|
||||||
|
Hash: hashHex,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = db.Get().Create(&upload).Error; err != nil {
|
||||||
|
return upload, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return upload, nil
|
||||||
|
}
|
Loading…
Reference in New Issue