portal/controller/files.go

214 lines
4.5 KiB
Go
Raw Permalink Normal View History

package controller
import (
"errors"
"git.lumeweb.com/LumeWeb/portal/cid"
"git.lumeweb.com/LumeWeb/portal/controller/response"
"git.lumeweb.com/LumeWeb/portal/logger"
"git.lumeweb.com/LumeWeb/portal/middleware"
"git.lumeweb.com/LumeWeb/portal/service/auth"
"git.lumeweb.com/LumeWeb/portal/service/files"
"github.com/kataras/iris/v12"
2023-05-19 13:04:47 +00:00
"go.uber.org/zap"
"io"
)
2023-08-15 06:11:55 +00:00
var ErrStreamDone = errors.New("done")
type FilesController struct {
2023-06-09 08:05:19 +00:00
Controller
}
func (f *FilesController) BeginRequest(ctx iris.Context) {
middleware.VerifyJwt(ctx)
}
func (f *FilesController) EndRequest(ctx iris.Context) {
}
func (f *FilesController) PostUpload() {
ctx := f.Ctx
file, meta, err := f.Ctx.FormFile("file")
if internalErrorCustom(ctx, err, errors.New("invalid file data")) {
logger.Get().Debug("invalid file data", zap.Error(err))
return
}
upload, err := files.Upload(file, meta.Size, nil, auth.GetCurrentUserId(ctx))
2023-08-15 06:11:55 +00:00
if InternalError(ctx, err) {
logger.Get().Debug("failed uploading file", zap.Error(err))
2023-05-04 13:11:31 +00:00
return
}
2023-06-09 11:06:33 +00:00
err = files.Pin(upload.Hash, upload.AccountID)
2023-08-15 06:11:55 +00:00
if InternalError(ctx, err) {
2023-06-09 11:06:33 +00:00
logger.Get().Debug("failed pinning file", zap.Error(err))
return
}
cidString, err := cid.EncodeString(upload.Hash, uint64(meta.Size))
2023-08-15 06:11:55 +00:00
if InternalError(ctx, err) {
logger.Get().Debug("failed creating cid", zap.Error(err))
return
}
err = ctx.JSON(&response.UploadResponse{Cid: cidString})
2023-05-19 13:04:47 +00:00
if err != nil {
logger.Get().Error("failed to create response", zap.Error(err))
2023-05-19 13:04:47 +00:00
}
}
func (f *FilesController) GetDownloadBy(cidString string) {
2023-05-08 14:10:57 +00:00
ctx := f.Ctx
2023-08-15 06:11:55 +00:00
hashHex, valid := ValidateCid(cidString, true, ctx)
if !valid {
2023-05-08 14:10:57 +00:00
return
}
download, err := files.Download(hashHex)
2023-08-15 06:11:55 +00:00
if InternalError(ctx, err) {
logger.Get().Debug("failed fetching file", zap.Error(err))
return
}
2023-05-08 14:10:57 +00:00
2023-08-15 06:11:55 +00:00
err = PassThroughStream(download, ctx)
if err != ErrStreamDone && InternalError(ctx, err) {
logger.Get().Debug("failed streaming file", zap.Error(err))
2023-05-19 13:04:47 +00:00
}
2023-05-08 14:10:57 +00:00
}
2023-06-09 19:52:58 +00:00
func (f *FilesController) GetProofBy(cidString string) {
ctx := f.Ctx
2023-08-15 06:11:55 +00:00
hashHex, valid := ValidateCid(cidString, true, ctx)
2023-06-09 19:52:58 +00:00
if !valid {
return
}
proof, err := files.DownloadProof(hashHex)
2023-08-15 06:11:55 +00:00
if InternalError(ctx, err) {
2023-06-09 19:52:58 +00:00
logger.Get().Debug("failed fetching file proof", zap.Error(err))
return
}
2023-08-15 06:11:55 +00:00
err = PassThroughStream(proof, ctx)
if InternalError(ctx, err) {
2023-06-09 19:52:58 +00:00
logger.Get().Debug("failed streaming file proof", zap.Error(err))
}
}
func (f *FilesController) GetStatusBy(cidString string) {
ctx := f.Ctx
2023-08-15 06:11:55 +00:00
hashHex, valid := ValidateCid(cidString, false, ctx)
if !valid {
return
}
status := files.Status(hashHex)
var statusCode string
switch status {
case files.STATUS_UPLOADED:
statusCode = "uploaded"
break
case files.STATUS_UPLOADING:
statusCode = "uploading"
break
case files.STATUS_NOT_FOUND:
2023-06-10 06:50:20 +00:00
statusCode = "not_found"
break
}
2023-06-15 04:25:38 +00:00
f.respondJSON(&response.FileStatusResponse{Status: statusCode})
}
func (f *FilesController) PostPinBy(cidString string) {
ctx := f.Ctx
2023-08-15 06:11:55 +00:00
hashHex, valid := ValidateCid(cidString, true, ctx)
if !valid {
return
}
err := files.Pin(hashHex, auth.GetCurrentUserId(ctx))
2023-08-15 06:11:55 +00:00
if InternalError(ctx, err) {
logger.Get().Error(err.Error())
return
}
f.Ctx.StatusCode(iris.StatusCreated)
}
2023-06-10 05:58:45 +00:00
func (f *FilesController) GetUploadLimit() {
f.respondJSON(&response.UploadLimit{Limit: f.Ctx.Application().ConfigurationReadOnly().GetPostMaxMemory()})
}
2023-08-15 06:11:55 +00:00
func ValidateCid(cidString string, validateStatus bool, ctx iris.Context) (string, bool) {
_, err := cid.Valid(cidString)
2023-08-15 06:11:55 +00:00
if SendError(ctx, err, iris.StatusBadRequest) {
logger.Get().Debug("invalid cid", zap.Error(err))
return "", false
}
cidObject, _ := cid.Decode(cidString)
hashHex := cidObject.StringHash()
if validateStatus {
status := files.Status(hashHex)
if status == files.STATUS_NOT_FOUND {
err := errors.New("cid not found")
2023-08-15 06:11:55 +00:00
SendError(ctx, errors.New("cid not found"), iris.StatusNotFound)
logger.Get().Debug("cid not found", zap.Error(err))
return "", false
}
}
return hashHex, true
}
2023-08-15 06:11:55 +00:00
func PassThroughStream(stream io.Reader, ctx iris.Context) error {
closed := false
err := ctx.StreamWriter(func(w io.Writer) error {
if closed {
2023-08-15 06:11:55 +00:00
return ErrStreamDone
}
count, err := io.CopyN(w, stream, 1024)
if count == 0 || err == io.EOF {
err = stream.(io.Closer).Close()
if err != nil {
logger.Get().Error("failed closing stream", zap.Error(err))
return err
}
closed = true
return nil
}
if err != nil {
return err
}
return nil
})
2023-08-15 06:11:55 +00:00
if err == ErrStreamDone {
err = nil
}
return err
}