Files
mstore/app/handler/file.go
T
2026-02-01 22:50:01 +02:00

116 lines
2.6 KiB
Go

package handler
import (
"io"
"mstore/app/operator"
"mstore/app/router"
)
func (hand *Handler) FileExists(rctx *router.Context) {
hand.logg.Debugf("Handle FileExists")
filepath := rctx.PathMap["filepath"]
params := &operator.FileExistsParams{
Filepath: filepath,
}
code, res, err := hand.oper.FileExists(params)
if err != nil {
hand.logg.Errorf("FileExists error: %v", err)
rctx.SetStatus(code)
return
}
rctx.SetHeader("X-Content-Type", res.ContentType)
rctx.SetHeader("X-Content-Size", res.ContentSize)
rctx.SetHeader("X-Content-Digest", res.ContentDigest)
rctx.SetHeader("Content-Length", "0")
rctx.SetStatus(code)
}
func (hand *Handler) PutFile(rctx *router.Context) {
hand.logg.Debugf("Handle PutFile")
contentSize := rctx.GetHeader("Content-Size")
contentType := rctx.GetHeader("Content-Type")
filepath := rctx.PathMap["filepath"]
hand.logg.Debugf("%s", contentSize)
params := &operator.PutFileParams{
Filepath: filepath,
ContentSize: contentSize,
ContentType: contentType,
Source: rctx.Request.Body,
}
code, _, err := hand.oper.PutFile(params)
if err != nil {
hand.logg.Errorf("PutFile error: %v", err)
rctx.SetStatus(code)
return
}
rctx.SetStatus(code)
}
func (hand *Handler) GetFile(rctx *router.Context) {
hand.logg.Debugf("Handle GetFile")
filepath := rctx.PathMap["filepath"]
params := &operator.GetFileParams{
Filepath: filepath,
}
hand.logg.Debugf("filepath: %s", filepath)
code, res, err := hand.oper.GetFile(params)
if err != nil {
hand.logg.Errorf("PutFile error: %v", err)
rctx.SetStatus(code)
return
}
rctx.SetHeader("Content-Type", res.ContentType)
rctx.SetHeader("Content-Length", res.ContentSize)
rctx.SetHeader("Content-Digest", res.ContentDigest)
rctx.SetStatus(code)
if res.Source != nil {
defer res.Source.Close()
_, err = io.Copy(rctx.Writer, res.Source)
if err != nil {
hand.logg.Errorf("GetFile error: %v", err)
return
}
}
}
func (hand *Handler) DeleteFile(rctx *router.Context) {
hand.logg.Debugf("Handle DeleteFile")
filepath := rctx.PathMap["filepath"]
params := &operator.DeleteFileParams{
Filepath: filepath,
}
code, _, err := hand.oper.DeleteFile(params)
if err != nil {
hand.logg.Errorf("GetFile error: %v", err)
}
rctx.SetStatus(code)
}
func (hand *Handler) ListFiles(rctx *router.Context) {
hand.logg.Debugf("Handle ListFiles")
filepath := rctx.PathMap["filepath"]
params := &operator.ListFilesParams{
Filepath: filepath,
}
code, res, err := hand.oper.ListFiles(params)
if err != nil {
hand.logg.Errorf("ListFiles error: %v", err)
rctx.SetStatus(code)
return
}
rctx.SetStatus(code)
rctx.SendJSON(res)
}