refactor: change files controller to use new files service api

This commit is contained in:
Derrick Hammer 2023-05-10 14:40:29 -04:00
parent 118c679f76
commit 73bc836cbc
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 12 additions and 112 deletions

View File

@ -1,38 +1,20 @@
package controller package controller
import ( import (
"bytes"
"encoding/hex"
"errors" "errors"
"fmt"
"git.lumeweb.com/LumeWeb/portal/bao"
"git.lumeweb.com/LumeWeb/portal/cid" "git.lumeweb.com/LumeWeb/portal/cid"
"git.lumeweb.com/LumeWeb/portal/db" "git.lumeweb.com/LumeWeb/portal/service/files"
"git.lumeweb.com/LumeWeb/portal/model"
"git.lumeweb.com/LumeWeb/portal/renterd"
"github.com/go-resty/resty/v2"
"github.com/kataras/iris/v12" "github.com/kataras/iris/v12"
"io" "io"
"lukechampine.com/blake3"
) )
type FilesController struct { type FilesController struct {
Ctx iris.Context Ctx iris.Context
} }
var client *resty.Client
type UploadResponse struct { type UploadResponse struct {
Cid string `json:"cid"` Cid string `json:"cid"`
} }
func InitFiles() {
client = resty.New()
client.SetBaseURL(renterd.GetApiAddr() + "/api")
client.SetBasicAuth("", renterd.GetAPIPassword())
client.SetDisableWarn(true)
}
func (f *FilesController) PostUpload() { func (f *FilesController) PostUpload() {
ctx := f.Ctx ctx := f.Ctx
@ -41,86 +23,19 @@ func (f *FilesController) PostUpload() {
return return
} }
buf, err := io.ReadAll(file) upload, err := files.Upload(file)
if internalError(ctx, err) {
return
}
if internalErrorCustom(ctx, err, errors.New("failed to read file data")) {
return
}
hashBytes := blake3.Sum256(buf)
hashHex := hex.EncodeToString(hashBytes[:])
fileCid, err := cid.EncodeFixed(hashBytes, uint64(meta.Size))
if internalError(ctx, err) { if internalError(ctx, err) {
return return
} }
_, err = file.Seek(0, io.SeekStart) cidString, err := cid.EncodeString(upload.Hash, uint64(meta.Size))
if internalError(ctx, err) {
return
}
var upload model.Upload
result := db.Get().Where("hash = ?", hashHex).First(&upload)
if (result.Error != nil && result.Error.Error() != "record not found") || result.RowsAffected > 0 {
ctx.JSON(&UploadResponse{Cid: fileCid})
return
}
_, err = file.Seek(0, io.SeekStart)
if internalError(ctx, err) {
return
}
tree, err := bao.ComputeBaoTree(bytes.NewReader(buf))
if internalError(ctx, err) {
return
}
objectExistsResult, err := client.R().Get(fmt.Sprintf("/worker/objects/%s", hashHex))
if internalError(ctx, err) { if internalError(ctx, err) {
return return
} }
if objectExistsResult.StatusCode() != 404 { _ = ctx.JSON(&UploadResponse{Cid: cidString})
ctx.JSON(&UploadResponse{Cid: fileCid})
return
}
if internalError(ctx, err) {
return
}
ret, err := client.R().SetBody(buf).Put(fmt.Sprintf("/worker/objects/%s", hashHex))
if ret.StatusCode() != 200 {
err = errors.New(string(ret.Body()))
}
if internalError(ctx, err) {
return
}
ret, err = client.R().SetBody(tree).Put(fmt.Sprintf("/worker/objects/%s.obao", hashHex))
if ret.StatusCode() != 200 {
err = errors.New(string(ret.Body()))
}
if internalError(ctx, err) {
return
}
upload = model.Upload{
Hash: hashHex,
}
if err := db.Get().Create(&upload).Error; err != nil {
if internalError(ctx, err) {
return
}
}
ctx.JSON(&UploadResponse{Cid: fileCid})
} }
func (f *FilesController) GetDownload() { func (f *FilesController) GetDownload() {
@ -134,38 +49,23 @@ func (f *FilesController) GetDownload() {
} }
cidObject, _ := cid.Decode(cidString) cidObject, _ := cid.Decode(cidString)
hashHex := hex.EncodeToString(cidObject.Hash[:]) hashHex := cidObject.StringHash()
result := db.Get().Table("uploads").Where("hash = ?", hashHex).Row()
if result.Err() != nil {
sendError(ctx, result.Err(), iris.StatusNotFound)
return
}
fetch, err := client.R().SetDoNotParseResponse(true).Get(fmt.Sprintf("/worker/objects/%s", hashHex))
if err != nil {
if fetch.StatusCode() == 404 {
sendError(ctx, err, iris.StatusNotFound)
return
}
internalError(ctx, err)
return
}
ctx.Header("Transfer-Encoding", "chunked")
if internalError(ctx, err) { if internalError(ctx, err) {
return return
} }
download, err := files.Download(hashHex)
if internalError(ctx, err) {
return
}
err = ctx.StreamWriter(func(w io.Writer) error { err = ctx.StreamWriter(func(w io.Writer) error {
_, err = io.Copy(w, fetch.RawBody()) _, err = io.Copy(w, download)
_ = fetch.RawBody().Close() _ = download.(io.Closer).Close()
return err return err
}) })
internalError(ctx, err) internalError(ctx, err)
} }
func sendErrorCustom(ctx iris.Context, err error, customError error, irisError int) bool { func sendErrorCustom(ctx iris.Context, err error, customError error, irisError int) bool {