2023-05-10 11:07:56 +00:00
|
|
|
package controller
|
2023-05-04 08:18:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"git.lumeweb.com/LumeWeb/portal/cid"
|
2023-05-10 18:40:29 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/service/files"
|
2023-05-04 08:18:38 +00:00
|
|
|
"github.com/kataras/iris/v12"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2023-05-10 11:07:56 +00:00
|
|
|
type FilesController struct {
|
2023-05-04 08:18:38 +00:00
|
|
|
Ctx iris.Context
|
|
|
|
}
|
|
|
|
type UploadResponse struct {
|
|
|
|
Cid string `json:"cid"`
|
|
|
|
}
|
|
|
|
|
2023-05-10 11:07:56 +00:00
|
|
|
func (f *FilesController) PostUpload() {
|
2023-05-04 08:18:38 +00:00
|
|
|
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-17 13:52:25 +00:00
|
|
|
upload, err := files.Upload(file, meta.Size)
|
2023-05-04 08:18:38 +00:00
|
|
|
|
2023-05-04 13:11:31 +00:00
|
|
|
if internalError(ctx, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-10 18:40:29 +00:00
|
|
|
cidString, err := cid.EncodeString(upload.Hash, uint64(meta.Size))
|
2023-05-04 08:18:38 +00:00
|
|
|
|
|
|
|
if internalError(ctx, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-10 18:40:29 +00:00
|
|
|
_ = ctx.JSON(&UploadResponse{Cid: cidString})
|
2023-05-04 08:18:38 +00:00
|
|
|
}
|
|
|
|
|
2023-05-10 19:09:18 +00:00
|
|
|
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) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cidObject, _ := cid.Decode(cidString)
|
2023-05-10 18:40:29 +00:00
|
|
|
hashHex := cidObject.StringHash()
|
2023-05-08 14:10:57 +00:00
|
|
|
|
2023-05-10 18:40:29 +00:00
|
|
|
if internalError(ctx, err) {
|
2023-05-08 14:10:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-10 18:40:29 +00:00
|
|
|
download, err := files.Download(hashHex)
|
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 {
|
2023-05-10 18:40:29 +00:00
|
|
|
_, err = io.Copy(w, download)
|
|
|
|
_ = download.(io.Closer).Close()
|
2023-05-08 14:10:57 +00:00
|
|
|
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
|
|
|
}
|