This commit is contained in:
Daniel Goc
2026-04-01 13:30:54 +02:00
parent 684f910090
commit b2acb8c922
14 changed files with 326 additions and 15 deletions

View File

@@ -0,0 +1,75 @@
package storageService
import (
"os"
"path/filepath"
"strings"
"git.ma-al.com/goc_daniel/b2b/app/model"
"git.ma-al.com/goc_daniel/b2b/app/repos/storageRepo"
"git.ma-al.com/goc_daniel/b2b/app/utils/responseErrors"
)
type StorageService struct {
storageRepo storageRepo.UIStorageRepo
}
func New() *StorageService {
return &StorageService{
storageRepo: storageRepo.New(),
}
}
func (s *StorageService) ListContent(absPath string) (*[]model.EntryInList, error) {
info, err := s.storageRepo.EntryInfo(absPath)
if err != nil || !info.IsDir() {
return nil, responseErrors.ErrFolderDoesNotExist
}
entries_in_list, err := s.storageRepo.ListContent(absPath)
return entries_in_list, err
}
func (s *StorageService) DownloadFilePrep(absPath string) (*os.File, string, int64, error) {
info, err := s.storageRepo.EntryInfo(absPath)
if err != nil || info.IsDir() {
return nil, "", 0, responseErrors.ErrFileDoesNotExist
}
f, err := s.storageRepo.OpenFile(absPath)
if err != nil {
return nil, "", 0, err
}
return f, filepath.Base(absPath), info.Size(), nil
}
func (s *StorageService) CreateFolder(absPath string, name string) error {
info, err := s.storageRepo.EntryInfo(absPath)
if err != nil || !info.IsDir() {
return responseErrors.ErrFolderDoesNotExist
}
if name == "" || name == "." filepath.Base(name) != name {
return responseErrors.ErrBadAttribute
}
absPath2, err := s.AbsPath(absPath, name)
if err != nil {
return err
}
return s.storageRepo.CreateFolder(absPath, name)
}
// AbsPath extracts an absolute path and validates it
func (s *StorageService) AbsPath(root string, relativePath string) (string, error) {
cleanName := filepath.Clean(relativePath)
fullPath := filepath.Join(root, cleanName)
if fullPath != root && !strings.HasPrefix(fullPath, root+string(os.PathSeparator)) {
return "", responseErrors.ErrAccessDenied
}
return fullPath, nil
}