package handler import ( "io" "mstore/app/operator" "mstore/app/router" ) const zeroContentLength = "0" func (hand *Handler) FileExists(rctx *router.Context) { filepath, _ := rctx.GetSubpath("filepath") params := &operator.FileExistsParams{ Filepath: filepath, } ctx := rctx.GetContext() code, res, err := hand.oper.FileExists(ctx, params) if err != nil { hand.logg.Errorf("FileExists error: %v", err) rctx.SetStatus(code) return } // TODO rctx.SetHeader("Content-Type", res.ContentType) rctx.SetHeader("Content-Length", res.ContentLength) rctx.SetHeader("Content-Digest", res.ContentDigest) //rctx.SetHeader("Content-Length", zeroContentLength) rctx.SetStatus(code) } func (hand *Handler) PutFile(rctx *router.Context) { contentLength := rctx.GetHeader("Content-Length") contentType := rctx.GetHeader("Content-Type") filepath, _ := rctx.GetSubpath("filepath") params := &operator.PutFileParams{ Filepath: filepath, ContentLength: contentLength, ContentType: contentType, Source: rctx.Request.Body, } ctx := rctx.GetContext() code, _, err := hand.oper.PutFile(ctx, 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) { filepath, _ := rctx.GetSubpath("filepath") params := &operator.GetFileParams{ Filepath: filepath, } ctx := rctx.GetContext() code, res, err := hand.oper.GetFile(ctx, 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.ContentLength) 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) { filepath, _ := rctx.GetSubpath("filepath") params := &operator.DeleteFileParams{ Filepath: filepath, } ctx := rctx.GetContext() code, _, err := hand.oper.DeleteFile(ctx, params) if err != nil { hand.logg.Errorf("GetFile error: %v", err) } rctx.SetStatus(code) } func (hand *Handler) ListFiles(rctx *router.Context) { filepath, _ := rctx.GetSubpath("filepath") params := &operator.ListFilesParams{ Filepath: filepath, } ctx := rctx.GetContext() code, res, err := hand.oper.ListFiles(ctx, params) if err != nil { hand.logg.Errorf("ListFiles error: %v", err) rctx.SetStatus(code) return } rctx.SetStatus(code) rctx.SendJSON(res) }