working commit

This commit is contained in:
2026-01-28 13:06:04 +02:00
parent 6db319088e
commit d647854e98
13 changed files with 293 additions and 45 deletions
+87 -8
View File
@@ -3,7 +3,12 @@ package operator
import (
"io"
"net/http"
//"path"
"path"
"strconv"
"mstore/app/descr"
"mstore/pkg/auxtool"
"mstore/pkg/auxuuid"
)
// File exists
@@ -12,16 +17,23 @@ type FileExistsParams struct {
Source string
Dest string
}
type FileExistsResult struct{}
type FileExistsResult struct {
Descr *descr.File
}
func (oper *Operator) FileExists(param *FileExistsParams) (int, *FileExistsResult, error) {
var err error
//filename := path.Base(param.Filepath)
//dirname := path.Dir(filepath)
code := http.StatusNotFound
res := &FileExistsResult{}
code := http.StatusOK
filename := path.Base(param.Filepath)
collection := path.Dir(param.Filepath)
exist, file, err := oper.mdb.GetFileByCollection(collection, filename)
if exist {
code = http.StatusOK
res.Descr = file
}
return code, res, err
}
@@ -32,11 +44,78 @@ type PutFileParams struct {
Filepath string
Source io.ReadCloser
}
type PutFileResult struct{}
type PutFileResult struct {
Descr *descr.File
}
const defaultContentType = "application/octet-stream"
func (oper *Operator) PutFile(param *PutFileParams) (int, *PutFileResult, error) {
var err error
res := &PutFileResult{}
size, err := strconv.ParseInt(param.ContentLength, 10, 64)
if err != nil {
code := http.StatusLengthRequired
return code, res, err
}
contentType := param.ContentType
if contentType == "" {
contentType = defaultContentType
}
filename := path.Base(param.Filepath)
collection := path.Dir(param.Filepath)
oper.logg.Debugf("Put file %s %s", collection, filename)
tmpname, size, checksum, err := oper.store.WriteTempFile(param.Source)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
exists, fileDescr, err := oper.mdb.GetFileByCollection(collection, filename)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
now := auxtool.TimeNow()
if exists {
fileDescr.Size = size
fileDescr.Checksum = checksum
fileDescr.UpdatedAt = now
fileDescr.Type = contentType
err = oper.mdb.UpdateFileByID(fileDescr.ID, fileDescr)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
} else {
fileDescr = &descr.File{
ID: auxuuid.NewUUID(),
Name: filename,
Collection: collection,
Size: size,
Type: contentType,
Checksum: checksum,
CreatedAt: now,
UpdatedAt: now,
}
err = oper.mdb.InsertFile(fileDescr)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
}
err = oper.store.LinkFile(tmpname, collection, filename)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
res.Descr = fileDescr
code := http.StatusOK
return code, res, err
}