working commit

This commit is contained in:
2026-02-01 21:03:43 +02:00
parent 9c18f62997
commit 18e2a61c8a
10 changed files with 410 additions and 111 deletions
+97 -25
View File
@@ -1,9 +1,11 @@
package operator
import (
"fmt"
"io"
"net/http"
"path"
"path/filepath"
"strconv"
"mstore/app/descr"
@@ -11,7 +13,7 @@ import (
"mstore/pkg/auxuuid"
)
// File exists
// FileExists
type FileExistsParams struct {
Filepath string
Source string
@@ -19,37 +21,52 @@ type FileExistsParams struct {
}
type FileExistsResult struct {
ContentType string
ContentLength string
ContentSize string
ContentDigest string
}
func cleanFilepath(filename string) (string, error) {
filename = "/" + filename
return filepath.Clean(filename), nil
}
func (oper *Operator) FileExists(param *FileExistsParams) (int, *FileExistsResult, error) {
var err error
code := http.StatusOK
res := &FileExistsResult{}
filename := path.Base(param.Filepath)
collection := path.Dir(param.Filepath)
xfilepath, err := cleanFilepath(param.Filepath)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
filename := path.Base(xfilepath)
collection := path.Dir(xfilepath)
exist, fileDescr, err := oper.mdb.GetFileByCollection(collection, filename)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
if !exist {
code = http.StatusNotFound
return code, res, err
}
res = &FileExistsResult{
ContentLength: strconv.FormatInt(fileDescr.Size, 10),
ContentSize: strconv.FormatInt(fileDescr.Size, 10),
ContentType: fileDescr.Type,
ContentDigest: fileDescr.Checksum,
}
return code, res, err
}
// Put file
// PutFile
type PutFileParams struct {
ContentType string
ContentLength string
Filepath string
Source io.ReadCloser
ContentType string
ContentSize string
Filepath string
Source io.ReadCloser
}
type PutFileResult struct{}
@@ -59,7 +76,12 @@ func (oper *Operator) PutFile(param *PutFileParams) (int, *PutFileResult, error)
var err error
res := &PutFileResult{}
size, err := strconv.ParseInt(param.ContentLength, 10, 64)
if param.ContentSize == "" {
code := http.StatusLengthRequired
err = fmt.Errorf("Content-Size is empty")
return code, res, err
}
size, err := strconv.ParseInt(param.ContentSize, 10, 64)
if err != nil {
code := http.StatusLengthRequired
return code, res, err
@@ -70,10 +92,15 @@ func (oper *Operator) PutFile(param *PutFileParams) (int, *PutFileResult, error)
}
// TODO: convert file path to a unified and secure state
xfilepath, err := cleanFilepath(param.Filepath)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
filename := path.Base(xfilepath)
collection := path.Dir(xfilepath)
filename := path.Base(param.Filepath)
collection := path.Dir(param.Filepath)
oper.logg.Debugf("Put file %s %s", collection, filename)
oper.logg.Debugf("Put file [%s] [%s]", collection, filename)
tmpname, size, checksum, err := oper.store.WriteTempFile(param.Source)
if err != nil {
@@ -125,13 +152,13 @@ func (oper *Operator) PutFile(param *PutFileParams) (int, *PutFileResult, error)
return code, res, err
}
// Get file
// GetFile
type GetFileParams struct {
Filepath string
}
type GetFileResult struct {
ContentType string
ContentLength string
ContentSize string
ContentDigest string
Source io.ReadCloser
}
@@ -142,10 +169,15 @@ func (oper *Operator) GetFile(param *GetFileParams) (int, *GetFileResult, error)
// TODO: convert file path to a unified and secure state
filename := path.Base(param.Filepath)
collection := path.Dir(param.Filepath)
xfilepath, err := cleanFilepath(param.Filepath)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
filename := path.Base(xfilepath)
collection := path.Dir(xfilepath)
oper.logg.Debugf("Gut file %s %s", collection, filename)
oper.logg.Debugf("Get file [%s] [%s]", collection, filename)
descrExists, fileDescr, err := oper.mdb.GetFileByCollection(collection, filename)
if err != nil {
@@ -162,7 +194,7 @@ func (oper *Operator) GetFile(param *GetFileParams) (int, *GetFileResult, error)
return code, res, err
}
res = &GetFileResult{
ContentLength: strconv.FormatInt(fileDescr.Size, 10),
ContentSize: strconv.FormatInt(fileDescr.Size, 10),
ContentType: fileDescr.Type,
ContentDigest: fileDescr.Checksum,
Source: reader,
@@ -171,7 +203,7 @@ func (oper *Operator) GetFile(param *GetFileParams) (int, *GetFileResult, error)
return code, res, err
}
// Delete file
// DeleteFile
type DeleteFileParams struct {
Filepath string
}
@@ -182,8 +214,13 @@ func (oper *Operator) DeleteFile(param *DeleteFileParams) (int, *DeleteFileResul
res := &DeleteFileResult{}
code := http.StatusOK
filename := path.Base(param.Filepath)
collection := path.Dir(param.Filepath)
xfilepath, err := cleanFilepath(param.Filepath)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
filename := path.Base(xfilepath)
collection := path.Dir(xfilepath)
exist, _, err := oper.mdb.GetFileByCollection(collection, filename)
if err != nil {
@@ -202,7 +239,42 @@ func (oper *Operator) DeleteFile(param *DeleteFileParams) (int, *DeleteFileResul
code = http.StatusInternalServerError
return code, res, err
}
return code, res, err
}
// ListFiles
type ListFilesParams struct {
Filepath string
}
type ListFilesResult struct {
FileDescrs []descr.File
}
func (oper *Operator) ListFiles(param *ListFilesParams) (int, *ListFilesResult, error) {
var err error
res := &ListFilesResult{
FileDescrs: make([]descr.File, 0),
}
// TODO: convert file path to a unified and secure state
xfilepath, err := cleanFilepath(param.Filepath)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
collection := xfilepath
oper.logg.Debugf("List files by %s", collection)
fileDescrs, err := oper.mdb.ListAllFiles()
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
for _, item := range fileDescrs {
res.FileDescrs = append(res.FileDescrs, item)
}
code := http.StatusOK
return code, res, err
}