76 lines
1.9 KiB
Go
76 lines
1.9 KiB
Go
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
|
|
}
|