getting to upload
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package storageService
|
||||
|
||||
import (
|
||||
"mime/multipart"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -20,56 +21,110 @@ func New() *StorageService {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StorageService) ListContent(absPath string) (*[]model.EntryInList, error) {
|
||||
info, err := s.storageRepo.EntryInfo(absPath)
|
||||
func (s *StorageService) ListContent(abs_path string) (*[]model.EntryInList, error) {
|
||||
info, err := s.storageRepo.EntryInfo(abs_path)
|
||||
if err != nil || !info.IsDir() {
|
||||
return nil, responseErrors.ErrFolderDoesNotExist
|
||||
}
|
||||
|
||||
entries_in_list, err := s.storageRepo.ListContent(absPath)
|
||||
entries_in_list, err := s.storageRepo.ListContent(abs_path)
|
||||
return entries_in_list, err
|
||||
}
|
||||
|
||||
func (s *StorageService) DownloadFilePrep(absPath string) (*os.File, string, int64, error) {
|
||||
info, err := s.storageRepo.EntryInfo(absPath)
|
||||
func (s *StorageService) DownloadFilePrep(abs_path string) (*os.File, string, int64, error) {
|
||||
info, err := s.storageRepo.EntryInfo(abs_path)
|
||||
if err != nil || info.IsDir() {
|
||||
return nil, "", 0, responseErrors.ErrFileDoesNotExist
|
||||
}
|
||||
|
||||
f, err := s.storageRepo.OpenFile(absPath)
|
||||
f, err := s.storageRepo.OpenFile(abs_path)
|
||||
if err != nil {
|
||||
return nil, "", 0, err
|
||||
}
|
||||
|
||||
return f, filepath.Base(absPath), info.Size(), nil
|
||||
return f, filepath.Base(abs_path), info.Size(), nil
|
||||
}
|
||||
|
||||
func (s *StorageService) CreateFolder(absPath string, name string) error {
|
||||
info, err := s.storageRepo.EntryInfo(absPath)
|
||||
func (s *StorageService) UploadFile(abs_path string, name string, f *multipart.FileHeader) error {
|
||||
info, err := s.storageRepo.EntryInfo(abs_path)
|
||||
if err != nil || !info.IsDir() {
|
||||
return responseErrors.ErrFolderDoesNotExist
|
||||
}
|
||||
|
||||
if name == "" || name == "." filepath.Base(name) != name {
|
||||
if name == "" || name == "." || name == ".." || filepath.Base(name) != name {
|
||||
return responseErrors.ErrBadAttribute
|
||||
}
|
||||
|
||||
absPath2, err := s.AbsPath(absPath, name)
|
||||
abs_file_path, err := s.AbsPath(abs_path, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if abs_file_path == abs_path {
|
||||
return responseErrors.ErrBadAttribute
|
||||
}
|
||||
|
||||
return s.storageRepo.CreateFolder(absPath, name)
|
||||
info, err = s.storageRepo.EntryInfo(abs_file_path)
|
||||
if err == nil {
|
||||
return responseErrors.ErrNameTaken
|
||||
} else if os.IsNotExist(err) {
|
||||
return s.storageRepo.UploadFile(abs_file_path, f)
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StorageService) CreateFolder(abs_path string, name string) error {
|
||||
info, err := s.storageRepo.EntryInfo(abs_path)
|
||||
if err != nil || !info.IsDir() {
|
||||
return responseErrors.ErrFolderDoesNotExist
|
||||
}
|
||||
|
||||
if name == "" || name == "." || name == ".." || filepath.Base(name) != name {
|
||||
return responseErrors.ErrBadAttribute
|
||||
}
|
||||
abs_folder_path, err := s.AbsPath(abs_path, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if abs_folder_path == abs_path {
|
||||
return responseErrors.ErrBadAttribute
|
||||
}
|
||||
|
||||
info, err = s.storageRepo.EntryInfo(abs_folder_path)
|
||||
if err == nil {
|
||||
return responseErrors.ErrNameTaken
|
||||
} else if os.IsNotExist(err) {
|
||||
return s.storageRepo.CreateFolder(abs_folder_path)
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StorageService) DeleteFile(abs_path string) error {
|
||||
info, err := s.storageRepo.EntryInfo(abs_path)
|
||||
if err != nil || info.IsDir() {
|
||||
return responseErrors.ErrFileDoesNotExist
|
||||
}
|
||||
|
||||
return s.storageRepo.DeleteFile(abs_path)
|
||||
}
|
||||
|
||||
func (s *StorageService) DeleteFolder(abs_path string) error {
|
||||
info, err := s.storageRepo.EntryInfo(abs_path)
|
||||
if err != nil || !info.IsDir() {
|
||||
return responseErrors.ErrFolderDoesNotExist
|
||||
}
|
||||
|
||||
return s.storageRepo.DeleteFolder(abs_path)
|
||||
}
|
||||
|
||||
// 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)
|
||||
clean_name := filepath.Clean(relativePath)
|
||||
full_path := filepath.Join(root, clean_name)
|
||||
|
||||
if fullPath != root && !strings.HasPrefix(fullPath, root+string(os.PathSeparator)) {
|
||||
if full_path != root && !strings.HasPrefix(full_path, root+string(os.PathSeparator)) {
|
||||
return "", responseErrors.ErrAccessDenied
|
||||
}
|
||||
|
||||
return fullPath, nil
|
||||
return full_path, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user