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
+26 -7
View File
@@ -22,7 +22,7 @@ func (hand *Handler) FileExists(rctx *router.Context) {
return
}
rctx.SetHeader("Content-Type", res.ContentType)
rctx.SetHeader("Content-Length", res.ContentLength)
rctx.SetHeader("Content-Size", res.ContentSize)
rctx.SetHeader("Content-Digest", res.ContentDigest)
rctx.SetStatus(code)
@@ -31,15 +31,17 @@ func (hand *Handler) FileExists(rctx *router.Context) {
func (hand *Handler) PutFile(rctx *router.Context) {
hand.logg.Debugf("Handle PutFile")
contentLength := rctx.GetHeader("Content-Length")
contentSize := rctx.GetHeader("Content-Size")
contentType := rctx.GetHeader("Content-Type")
filepath := rctx.PathMap["filepath"]
hand.logg.Debugf("%s", contentSize)
params := &operator.PutFileParams{
Filepath: filepath,
ContentLength: contentLength,
ContentType: contentType,
Source: rctx.Request.Body,
Filepath: filepath,
ContentSize: contentSize,
ContentType: contentType,
Source: rctx.Request.Body,
}
code, _, err := hand.oper.PutFile(params)
if err != nil {
@@ -67,7 +69,7 @@ func (hand *Handler) GetFile(rctx *router.Context) {
}
rctx.SetStatus(code)
rctx.SetHeader("Content-Type", res.ContentType)
rctx.SetHeader("Content-Length", res.ContentLength)
rctx.SetHeader("Content-Size", res.ContentSize)
rctx.SetHeader("Content-Digest", res.ContentDigest)
if res.Source != nil {
@@ -93,3 +95,20 @@ func (hand *Handler) DeleteFile(rctx *router.Context) {
}
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)
}
+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
}
+1 -1
View File
@@ -56,7 +56,7 @@ func pathCompiler(path string) (string, error) {
}
const (
defaultRegexp = `[a-zA-Z0-9_][/\-\.a-zA-Z0-9_%%=:~]+`
defaultRegexp = `[a-zA-Z0-9_\.][/\-\.a-zA-Z0-9_%%=:~]+`
)
func convertRegexp(src []byte) []byte {
+1
View File
@@ -61,6 +61,7 @@ func (svc *Service) Build() error {
svc.rout.Put("/v3/api/file/{filepath}", svc.hand.PutFile)
svc.rout.Get("/v3/api/file/{filepath}", svc.hand.GetFile)
svc.rout.Delete("/v3/api/file/{filepath}", svc.hand.DeleteFile)
svc.rout.Get("/v3/api/files/{filepath}", svc.hand.ListFiles)
selector := svc.rout.Selector()
for _, item := range selector.Routes {
+41 -9
View File
@@ -25,11 +25,34 @@ func NewStorage(basepath string) *Storage {
return res
}
const filesubdir = "files"
const tmpsubdir = "tmps"
func (store *Storage) makeCollecionpath(collection string) string {
return filepath.Join(store.basepath, filesubdir, collection)
}
func (store *Storage) makeFilepath(collection, filename string) string {
return filepath.Join(store.basepath, filesubdir, collection, filename)
}
func (store *Storage) makeTmppath(tmpname string) string {
return filepath.Join(store.basepath, tmpsubdir, tmpname)
}
func (store *Storage) makeTmpsubdir() string {
return filepath.Join(store.basepath, tmpsubdir)
}
func (store *Storage) makeFilesubdir(collection, filename string) string {
return filepath.Join(store.basepath, filesubdir)
}
func (store *Storage) GetFileReader(collection, filename string) (io.ReadCloser, error) {
var err error
var res io.ReadCloser
filename = filepath.Join(store.basepath, collection, filename)
filename = store.makeFilepath(collection, filename)
file, err := os.OpenFile(filename, os.O_RDONLY, 0)
if err != nil {
return res, err
@@ -40,15 +63,16 @@ func (store *Storage) GetFileReader(collection, filename string) (io.ReadCloser,
func (store *Storage) HardlinkFile(tmpname, collection, filename string) error {
var err error
dirname := filepath.Join(store.basepath, collection)
dirname := store.makeCollecionpath(collection)
err = os.MkdirAll(dirname, 0750)
if err != nil {
return err
}
filename = filepath.Join(store.basepath, collection, filename)
filename = store.makeFilepath(collection, filename)
os.Remove(filename) // TODO
tmpname = store.makeTmppath(tmpname)
err = os.Link(tmpname, filename)
if err != nil {
return err
@@ -67,11 +91,17 @@ func (store *Storage) WriteTempFile(source io.Reader) (string, int64, string, er
tmpname := auxuuid.NewUUID()
tmpname = fmt.Sprintf("file-%s.tmp", tmpname)
tmppath := filepath.Join(store.basepath, tmpname)
tmppath := store.makeTmppath(tmpname)
tmpdirpath := store.makeTmpsubdir()
err = os.MkdirAll(tmpdirpath, 0750)
if err != nil {
return tmpname, size, digest, err
}
file, err := os.OpenFile(tmppath, os.O_WRONLY|os.O_CREATE, 0640)
if err != nil {
return tmppath, size, digest, err
return tmpname, size, digest, err
}
defer file.Close()
@@ -80,23 +110,25 @@ func (store *Storage) WriteTempFile(source io.Reader) (string, int64, string, er
size, err = io.Copy(writer, source)
if err != nil {
return tmppath, size, digest, err
return tmpname, size, digest, err
}
digest = hex.EncodeToString(hasher.Sum(nil))
digest = fmt.Sprintf("sha256:%s", digest)
return tmppath, size, digest, err
return tmpname, size, digest, err
}
func (store *Storage) DeleteFile(collection, filename string) error {
var err error
filename = filepath.Join(store.basepath, collection, filename)
filename = store.makeFilepath(collection, filename)
err = os.Remove(filename)
if err != nil {
return err
}
dirname := filepath.Join(store.basepath, collection)
// TODO: more safe removing
dirname := store.makeCollecionpath(collection)
err = os.RemoveAll(dirname)
if err != nil {
return err