added copying and moving
This commit is contained in:
@@ -30,6 +30,8 @@ func StorageHandlerRoutes(r fiber.Router) fiber.Router {
|
||||
r.Get("/list-content/*", handler.ListContent)
|
||||
r.Get("/download-file/*", handler.DownloadFile)
|
||||
|
||||
r.Get("/move/*", handler.Move)
|
||||
r.Get("/copy/*", handler.Copy)
|
||||
r.Post("/upload-file/*", handler.UploadFile)
|
||||
r.Get("/create-folder/*", handler.CreateFolder)
|
||||
r.Delete("/delete-file/*", handler.DeleteFile)
|
||||
@@ -38,6 +40,50 @@ func StorageHandlerRoutes(r fiber.Router) fiber.Router {
|
||||
return r
|
||||
}
|
||||
|
||||
func (h *StorageHandler) Move(c fiber.Ctx) error {
|
||||
src_abs_path, err := h.storageService.AbsPath(h.config.Storage.RootFolder, c.Params("*"))
|
||||
if err != nil {
|
||||
return c.Status(responseErrors.GetErrorStatus(err)).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
||||
}
|
||||
|
||||
dest_abs_path, err := h.storageService.AbsPath(h.config.Storage.RootFolder, c.Query("dest_path"))
|
||||
if err != nil {
|
||||
return c.Status(responseErrors.GetErrorStatus(err)).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
||||
}
|
||||
|
||||
err = h.storageService.Move(src_abs_path, dest_abs_path)
|
||||
if err != nil {
|
||||
return c.Status(responseErrors.GetErrorStatus(err)).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
||||
}
|
||||
|
||||
return c.JSON(response.Make(nullable.GetNil(""), 0, i18n.T_(c, response.Message_OK)))
|
||||
}
|
||||
|
||||
func (h *StorageHandler) Copy(c fiber.Ctx) error {
|
||||
src_abs_path, err := h.storageService.AbsPath(h.config.Storage.RootFolder, c.Params("*"))
|
||||
if err != nil {
|
||||
return c.Status(responseErrors.GetErrorStatus(err)).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
||||
}
|
||||
|
||||
dest_abs_path, err := h.storageService.AbsPath(h.config.Storage.RootFolder, c.Query("dest_path"))
|
||||
if err != nil {
|
||||
return c.Status(responseErrors.GetErrorStatus(err)).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
||||
}
|
||||
|
||||
err = h.storageService.Copy(src_abs_path, dest_abs_path)
|
||||
if err != nil {
|
||||
return c.Status(responseErrors.GetErrorStatus(err)).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
||||
}
|
||||
|
||||
return c.JSON(response.Make(nullable.GetNil(""), 0, i18n.T_(c, response.Message_OK)))
|
||||
}
|
||||
|
||||
// accepted path looks like e.g. "/folder1/" or "folder1"
|
||||
func (h *StorageHandler) ListContent(c fiber.Ctx) error {
|
||||
// relative path defaults to root directory
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package storageRepo
|
||||
|
||||
import (
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"os"
|
||||
|
||||
@@ -11,6 +12,8 @@ import (
|
||||
type UIStorageRepo interface {
|
||||
EntryInfo(abs_path string) (os.FileInfo, error)
|
||||
ListContent(abs_path string) (*[]model.EntryInList, error)
|
||||
Move(src_abs_path string, dest_abs_path string) error
|
||||
Copy(src_abs_path string, dest_abs_path string) error
|
||||
OpenFile(abs_path string) (*os.File, error)
|
||||
UploadFile(c fiber.Ctx, abs_path string, f *multipart.FileHeader) error
|
||||
CreateFolder(abs_path string) error
|
||||
@@ -47,6 +50,35 @@ func (r *StorageRepo) ListContent(abs_path string) (*[]model.EntryInList, error)
|
||||
return &entries_in_list, nil
|
||||
}
|
||||
|
||||
func (r *StorageRepo) Move(src_abs_path string, dest_abs_path string) error {
|
||||
return os.Rename(src_abs_path, dest_abs_path)
|
||||
}
|
||||
|
||||
func (r *StorageRepo) Copy(src_abs_path string, dest_abs_path string) error {
|
||||
in, err := os.Open(src_abs_path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer in.Close()
|
||||
|
||||
info, err := in.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
out, err := os.OpenFile(dest_abs_path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, info.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
if _, err := io.Copy(out, in); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return out.Sync()
|
||||
}
|
||||
|
||||
func (r *StorageRepo) OpenFile(abs_path string) (*os.File, error) {
|
||||
return os.Open(abs_path)
|
||||
}
|
||||
|
||||
@@ -46,6 +46,35 @@ func (s *StorageService) DownloadFilePrep(abs_path string) (*os.File, string, in
|
||||
return f, filepath.Base(abs_path), info.Size(), nil
|
||||
}
|
||||
|
||||
func (s *StorageService) Move(src_abs_path string, dest_abs_path string) error {
|
||||
_, err := s.storageRepo.EntryInfo(src_abs_path)
|
||||
if err != nil {
|
||||
return responseErrors.ErrFileDoesNotExist
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
return responseErrors.ErrNameTaken
|
||||
} else if os.IsNotExist(err) {
|
||||
return s.storageRepo.Move(src_abs_path, dest_abs_path)
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
func (s *StorageService) Copy(src_abs_path string, dest_abs_path string) error {
|
||||
_, err := s.storageRepo.EntryInfo(src_abs_path)
|
||||
if err != nil {
|
||||
return responseErrors.ErrFileDoesNotExist
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
return responseErrors.ErrNameTaken
|
||||
} else if os.IsNotExist(err) {
|
||||
return s.storageRepo.Copy(src_abs_path, dest_abs_path)
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StorageService) UploadFile(c fiber.Ctx, abs_path string, f *multipart.FileHeader) error {
|
||||
info, err := s.storageRepo.EntryInfo(abs_path)
|
||||
if err != nil || !info.IsDir() {
|
||||
|
||||
Reference in New Issue
Block a user