2023-05-04 08:18:38 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"git.lumeweb.com/LumeWeb/portal/bao"
|
|
|
|
"git.lumeweb.com/LumeWeb/portal/cid"
|
|
|
|
"git.lumeweb.com/LumeWeb/portal/db"
|
|
|
|
"git.lumeweb.com/LumeWeb/portal/model"
|
|
|
|
"git.lumeweb.com/LumeWeb/portal/renterd"
|
|
|
|
"github.com/go-resty/resty/v2"
|
|
|
|
"github.com/kataras/iris/v12"
|
|
|
|
"io"
|
|
|
|
"lukechampine.com/blake3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FilesService struct {
|
|
|
|
Ctx iris.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
var client *resty.Client
|
|
|
|
|
|
|
|
type UploadResponse struct {
|
|
|
|
Cid string `json:"cid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitFiles() {
|
|
|
|
client = resty.New()
|
|
|
|
client.SetBaseURL(renterd.GetApiAddr() + "/api")
|
|
|
|
client.SetBasicAuth("", renterd.GetAPIPassword())
|
2023-05-06 07:56:38 +00:00
|
|
|
client.SetDisableWarn(true)
|
2023-05-04 08:18:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FilesService) PostUpload() {
|
|
|
|
ctx := f.Ctx
|
|
|
|
|
2023-05-04 12:16:44 +00:00
|
|
|
file, meta, err := f.Ctx.FormFile("file")
|
2023-05-04 08:18:38 +00:00
|
|
|
if internalErrorCustom(ctx, err, errors.New("invalid file data")) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-04 13:11:31 +00:00
|
|
|
buf, err := io.ReadAll(file)
|
|
|
|
if internalError(ctx, err) {
|
|
|
|
return
|
|
|
|
}
|
2023-05-04 08:18:38 +00:00
|
|
|
|
|
|
|
if internalErrorCustom(ctx, err, errors.New("failed to read file data")) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-04 13:11:31 +00:00
|
|
|
hashBytes := blake3.Sum256(buf)
|
2023-05-04 08:18:38 +00:00
|
|
|
hashHex := hex.EncodeToString(hashBytes[:])
|
2023-05-06 08:38:09 +00:00
|
|
|
fileCid, err := cid.Encode(hashBytes, uint64(meta.Size))
|
2023-05-04 08:18:38 +00:00
|
|
|
|
|
|
|
if internalError(ctx, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-04 13:11:31 +00:00
|
|
|
_, err = file.Seek(0, io.SeekStart)
|
|
|
|
if internalError(ctx, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-04 08:18:38 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-05-04 13:11:31 +00:00
|
|
|
tree, err := bao.ComputeBaoTree(bytes.NewReader(buf))
|
2023-05-04 08:18:38 +00:00
|
|
|
if internalError(ctx, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
objectExistsResult, err := client.R().Get(fmt.Sprintf("/worker/objects/%s", hashHex))
|
|
|
|
|
|
|
|
if internalError(ctx, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if objectExistsResult.StatusCode() != 404 {
|
|
|
|
ctx.JSON(&UploadResponse{Cid: fileCid})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-04 13:11:31 +00:00
|
|
|
if internalError(ctx, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, err := client.R().SetBody(buf).Put(fmt.Sprintf("/worker/objects/%s", hashHex))
|
2023-05-04 11:58:06 +00:00
|
|
|
if ret.StatusCode() != 200 {
|
|
|
|
err = errors.New(string(ret.Body()))
|
|
|
|
}
|
2023-05-04 08:18:38 +00:00
|
|
|
if internalError(ctx, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-04 11:58:06 +00:00
|
|
|
ret, err = client.R().SetBody(tree).Put(fmt.Sprintf("/worker/objects/%s.obao", hashHex))
|
|
|
|
if ret.StatusCode() != 200 {
|
|
|
|
err = errors.New(string(ret.Body()))
|
|
|
|
}
|
2023-05-04 08:18:38 +00:00
|
|
|
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})
|
|
|
|
}
|
|
|
|
|
2023-05-08 14:10:57 +00:00
|
|
|
func (f *FilesService) GetDownload() {
|
|
|
|
ctx := f.Ctx
|
|
|
|
|
|
|
|
cidString := ctx.URLParam("cid")
|
|
|
|
|
|
|
|
_, err := cid.Valid(cidString)
|
|
|
|
if sendError(ctx, err, iris.StatusBadRequest) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cidObject, _ := cid.Decode(cidString)
|
|
|
|
hashHex := hex.EncodeToString(cidObject.Hash[:])
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
2023-05-08 14:16:47 +00:00
|
|
|
if internalError(ctx, err) {
|
|
|
|
return
|
|
|
|
}
|
2023-05-08 14:10:57 +00:00
|
|
|
|
|
|
|
err = ctx.StreamWriter(func(w io.Writer) error {
|
|
|
|
_, err = io.Copy(w, fetch.RawBody())
|
|
|
|
_ = fetch.RawBody().Close()
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
internalError(ctx, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendErrorCustom(ctx iris.Context, err error, customError error, irisError int) bool {
|
2023-05-04 08:18:38 +00:00
|
|
|
if err != nil {
|
|
|
|
if customError != nil {
|
|
|
|
err = customError
|
|
|
|
}
|
2023-05-08 14:10:57 +00:00
|
|
|
ctx.StopWithError(irisError, err)
|
2023-05-04 08:18:38 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
func internalError(ctx iris.Context, err error) bool {
|
2023-05-08 14:10:57 +00:00
|
|
|
return sendErrorCustom(ctx, err, nil, iris.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
func internalErrorCustom(ctx iris.Context, err error, customError error) bool {
|
|
|
|
return sendErrorCustom(ctx, err, customError, iris.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
func sendError(ctx iris.Context, err error, irisError int) bool {
|
|
|
|
return sendErrorCustom(ctx, err, nil, irisError)
|
2023-05-04 08:18:38 +00:00
|
|
|
}
|