portal/controller/files.go

97 lines
2.3 KiB
Go
Raw Normal View History

package controller
import (
"errors"
"git.lumeweb.com/LumeWeb/portal/cid"
"git.lumeweb.com/LumeWeb/portal/service/files"
2023-05-19 13:04:47 +00:00
"git.lumeweb.com/LumeWeb/portal/shared"
"github.com/kataras/iris/v12"
2023-05-19 13:04:47 +00:00
"go.uber.org/zap"
"io"
)
type FilesController struct {
Ctx iris.Context
}
type UploadResponse struct {
Cid string `json:"cid"`
}
func (f *FilesController) PostUpload() {
ctx := f.Ctx
file, meta, err := f.Ctx.FormFile("file")
if internalErrorCustom(ctx, err, errors.New("invalid file data")) {
2023-05-19 13:04:47 +00:00
shared.GetLogger().Debug("invalid file data", zap.Error(err))
return
}
upload, err := files.Upload(file, meta.Size, nil)
2023-05-04 13:11:31 +00:00
if internalError(ctx, err) {
2023-05-19 13:04:47 +00:00
shared.GetLogger().Debug("failed uploading file", zap.Error(err))
2023-05-04 13:11:31 +00:00
return
}
cidString, err := cid.EncodeString(upload.Hash, uint64(meta.Size))
if internalError(ctx, err) {
2023-05-19 13:04:47 +00:00
shared.GetLogger().Debug("failed creating cid", zap.Error(err))
return
}
2023-05-19 13:04:47 +00:00
err = ctx.JSON(&UploadResponse{Cid: cidString})
if err != nil {
shared.GetLogger().Error("failed to create response", zap.Error(err))
}
}
func (f *FilesController) GetDownloadBy(cidString string) {
2023-05-08 14:10:57 +00:00
ctx := f.Ctx
_, err := cid.Valid(cidString)
if sendError(ctx, err, iris.StatusBadRequest) {
2023-05-19 13:04:47 +00:00
shared.GetLogger().Debug("invalid cid", zap.Error(err))
2023-05-08 14:10:57 +00:00
return
}
cidObject, _ := cid.Decode(cidString)
hashHex := cidObject.StringHash()
download, err := files.Download(hashHex)
if internalError(ctx, err) {
2023-05-19 13:04:47 +00:00
shared.GetLogger().Debug("failed fetching file", zap.Error(err))
return
}
2023-05-08 14:10:57 +00:00
err = ctx.StreamWriter(func(w io.Writer) error {
_, err = io.Copy(w, download)
_ = download.(io.Closer).Close()
2023-05-08 14:10:57 +00:00
return err
})
2023-05-19 13:04:47 +00:00
if internalError(ctx, err) {
shared.GetLogger().Debug("failed streaming file", zap.Error(err))
}
2023-05-08 14:10:57 +00:00
}
func sendErrorCustom(ctx iris.Context, err error, customError error, irisError int) bool {
if err != nil {
if customError != nil {
err = customError
}
2023-05-08 14:10:57 +00:00
ctx.StopWithError(irisError, err)
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)
}