/* * Copyright 2026 Oleg Borodin * * This work is published and licensed under a Creative Commons * Attribution-NonCommercial-NoDerivatives 4.0 International License. * * Distribution of this work is permitted, but commercial use and * modifications are strictly prohibited. */ package operator import ( "context" "fmt" "io" "net/http" "path" "path/filepath" "slices" "strconv" "strings" "mstore/app/descr" "mstore/pkg/auxtool" "mstore/pkg/auxuuid" ) // FileInfo type FileInfoParams struct { Filepath string Source string Dest string } type FileInfoResult struct { ContentCollection string ContentName string ContentType string ContentSize string ContentDigest string ContentCreatedAt string ContentCreatedBy string ContentUpdatedAt string ContentUpdatedBy string } func cleanFilepath(filename string) (string, error) { filename = "/" + filename return filepath.Clean(filename), nil } func (oper *Operator) FileInfo(ctx context.Context, operID string, param *FileInfoParams) (int, *FileInfoResult, error) { var err error code := http.StatusOK res := &FileInfoResult{} 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.GetFileByCollectionName(ctx, collection, filename) if err != nil { code := http.StatusInternalServerError return code, res, err } if !exist { code = http.StatusNotFound return code, res, err } res = &FileInfoResult{ ContentCollection: fileDescr.Collection, ContentName: fileDescr.Name, ContentSize: strconv.FormatInt(fileDescr.Size, 10), ContentType: fileDescr.Type, ContentDigest: fileDescr.Checksum, ContentCreatedAt: fileDescr.CreatedAt, ContentCreatedBy: fileDescr.CreatedBy, ContentUpdatedAt: fileDescr.UpdatedAt, ContentUpdatedBy: fileDescr.UpdatedBy, } return code, res, err } // PutFile type PutFileParams struct { ContentType string ContentSize string Filepath string Source io.ReadCloser } type PutFileResult struct{} const defaultContentType = "application/octet-stream" // TODO: checking catalog and file names conflict func (oper *Operator) PutFile(ctx context.Context, operID string, param *PutFileParams) (int, *PutFileResult, error) { var err error res := &PutFileResult{} if param.ContentSize == "" { code := http.StatusLengthRequired err = fmt.Errorf("Required Content-Size header is empty") return code, res, err } size, err := strconv.ParseInt(param.ContentSize, 10, 64) if err != nil { code := http.StatusLengthRequired return code, res, err } contentType := param.ContentType if contentType == "" { contentType = defaultContentType } // 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) tmpname, size, checksum, err := oper.store.WriteTempFile(param.Source) if err != nil { code := http.StatusInternalServerError return code, res, err } descrExists, fileDescr, err := oper.mdb.GetFileByCollectionName(ctx, collection, filename) if err != nil { code := http.StatusInternalServerError return code, res, err } now := auxtool.TimeNow() if descrExists { fileDescr.Size = size fileDescr.Checksum = checksum fileDescr.UpdatedAt = now fileDescr.Type = contentType fileDescr.UpdatedBy = operID err = oper.mdb.UpdateFileByID(ctx, 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, CreatedBy: operID, UpdatedBy: operID, } err = oper.mdb.InsertFile(ctx, fileDescr) if err != nil { code := http.StatusInternalServerError return code, res, err } } err = oper.store.HardlinkFile(tmpname, collection, filename) if err != nil { code := http.StatusInternalServerError return code, res, err } code := http.StatusOK return code, res, err } // GetFile type GetFileParams struct { Filepath string } type GetFileResult struct { ContentType string ContentSize string ContentDigest string Source io.ReadCloser ContentCreatedAt string ContentCreatedBy string ContentUpdatedAt string ContentUpdatedBy string } func (oper *Operator) GetFile(ctx context.Context, operID string, param *GetFileParams) (int, *GetFileResult, error) { var err error res := &GetFileResult{} // 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) descrExists, fileDescr, err := oper.mdb.GetFileByCollectionName(ctx, collection, filename) if err != nil { code := http.StatusInternalServerError return code, res, err } if !descrExists { code := http.StatusNotFound return code, res, err } reader, err := oper.store.GetFileReader(collection, filename) if err != nil { code := http.StatusInternalServerError return code, res, err } res = &GetFileResult{ ContentSize: strconv.FormatInt(fileDescr.Size, 10), ContentType: fileDescr.Type, ContentDigest: fileDescr.Checksum, Source: reader, ContentCreatedAt: fileDescr.CreatedAt, ContentCreatedBy: fileDescr.CreatedBy, ContentUpdatedAt: fileDescr.UpdatedAt, ContentUpdatedBy: fileDescr.UpdatedBy, } code := http.StatusOK return code, res, err } // DeleteFile type DeleteFileParams struct { Filepath string } type DeleteFileResult struct{} func (oper *Operator) DeleteFile(ctx context.Context, operID string, param *DeleteFileParams) (int, *DeleteFileResult, error) { var err error res := &DeleteFileResult{} code := http.StatusOK 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.GetFileByCollectionName(ctx, collection, filename) if err != nil { code = http.StatusInternalServerError return code, res, err } if exist { err = oper.mdb.DeleteFileByCollectionName(ctx, collection, filename) if err != nil { code = http.StatusInternalServerError return code, res, err } } err = oper.store.DeleteFile(collection, filename) if err != nil { code = http.StatusInternalServerError return code, res, err } return code, res, err } // ListFiles type ListFilesParams struct { Filepath string } type ListFilesResult struct { Files []descr.File `json:"files,omitempty"` } func (oper *Operator) ListFiles(ctx context.Context, operID string, param *ListFilesParams) (int, *ListFilesResult, error) { var err error res := &ListFilesResult{ Files: make([]descr.File, 0), } param.Filepath, err = cleanFilepath(param.Filepath) if err != nil { code := http.StatusInternalServerError return code, res, err } fileDescrs, err := oper.mdb.ListAllFiles(ctx) if err != nil { code := http.StatusInternalServerError return code, res, err } rFilepath := filepath.Join("/", param.Filepath) for _, item := range fileDescrs { cFilepath := filepath.Join("/", item.Collection, item.Name) if strings.HasPrefix(cFilepath, rFilepath) { res.Files = append(res.Files, item) } } code := http.StatusOK return code, res, err } // ListCollections type ListCollectionsParams struct { Path string } type ListCollectionsResult struct { Collections []string `json:"collection,omitempty"` } func (oper *Operator) ListCollections(ctx context.Context, operID string, param *ListCollectionsParams) (int, *ListCollectionsResult, error) { var err error res := &ListCollectionsResult{ Collections: make([]string, 0), } param.Path, err = cleanFilepath(param.Path) if err != nil { code := http.StatusInternalServerError return code, res, err } fileDescrs, err := oper.mdb.ListAllFiles(ctx) if err != nil { code := http.StatusInternalServerError return code, res, err } cmap := make(map[string]bool) for _, item := range fileDescrs { cPath := filepath.Join("/", item.Collection) pattern := filepath.Join("/", param.Path) if strings.HasPrefix(cPath, pattern) { _, exists := cmap[cPath] if !exists { cmap[cPath] = true } } } for key, _ := range cmap { res.Collections = append(res.Collections, key) } slices.Sort(res.Collections) code := http.StatusOK return code, res, err } // DeleteColletion type DeleteColletionParams struct { Path string IsPattern bool `params:"isPattern"` } type DeleteColletionResult struct { Files []descr.File `json:"collection,omitempty"` } func (oper *Operator) DeleteColletion(ctx context.Context, operID string, param *DeleteColletionParams) (int, *DeleteColletionResult, error) { var err error res := &DeleteColletionResult{ Files: make([]descr.File, 0), } param.Path, err = cleanFilepath(param.Path) if err != nil { code := http.StatusInternalServerError return code, res, err } fileDescrs, err := oper.mdb.ListFilesByCollection(ctx, param.Path) if err != nil { code := http.StatusInternalServerError return code, res, err } // TODO: transaction for _, file := range fileDescrs { err = oper.store.DeleteFile(file.Collection, file.Name) if err != nil { oper.logg.Warningf("%v", err) err = nil } err = oper.mdb.DeleteFileByCollectionName(ctx, file.Collection, file.Name) if err != nil { code := http.StatusInternalServerError return code, res, err } res.Files = append(res.Files, file) } code := http.StatusOK return code, res, err }