diff --git a/core/amazon/awssdk/listobjects_v2.go b/core/amazon/awssdk/listobjects_v2.go index 81e5fb7..ac05147 100644 --- a/core/amazon/awssdk/listobjects_v2.go +++ b/core/amazon/awssdk/listobjects_v2.go @@ -23,7 +23,7 @@ type Item struct { Size int64 `xml:"Size"` } -func ListObjectsV2(w http.ResponseWriter, r *http.Request) { +func ListObjectsV2(w http.ResponseWriter, r *http.Request, uploadDirName string) { slog.Info("ListObjectsV2 is called.") path := strings.Split(r.URL.Path, "?list-type=2")[0] // It has been confirmed in the previous process controller.go that `?list-type=2` is included. @@ -34,9 +34,9 @@ func ListObjectsV2(w http.ResponseWriter, r *http.Request) { return } - rootDir := filepath.Join("upload", dir) + rootDir := filepath.Join(uploadDirName, dir) - items, err := ListObjects(rootDir) + items, err := ListObjects(rootDir, uploadDirName) if err != nil { slog.Error("Failed to list objects", "error", err) http.Error(w, "Failed to list objects", http.StatusInternalServerError) @@ -60,7 +60,7 @@ func ListObjectsV2(w http.ResponseWriter, r *http.Request) { } } -func ListObjects(rootDir string) ([]Item, error) { +func ListObjects(rootDir string, uploadDirName string) ([]Item, error) { var items []Item err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error { if err != nil { @@ -68,7 +68,7 @@ func ListObjects(rootDir string) ([]Item, error) { } if !info.IsDir() { items = append(items, Item{ - Key: filepath.ToSlash(path[len("upload/"):]), + Key: filepath.ToSlash(path[len(uploadDirName+"/"):]), Size: info.Size(), }) } diff --git a/core/amazon/awssdk/listobjects_v2_test.go b/core/amazon/awssdk/listobjects_v2_test.go index 84b8097..bb3ab64 100644 --- a/core/amazon/awssdk/listobjects_v2_test.go +++ b/core/amazon/awssdk/listobjects_v2_test.go @@ -1,11 +1,63 @@ package awssdk import ( + "os" + "path/filepath" "testing" "github.com/go-playground/assert/v2" ) +func TestListObjects(t *testing.T) { + // Create a temporary directory for testing + tempDir := "test-upload" + err := os.Mkdir(tempDir, 0777) + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tempDir) + + // Create test files and directories + err = os.MkdirAll(filepath.Join(tempDir, "dir1"), os.ModePerm) + if err != nil { + t.Fatal(err) + } + + err = os.WriteFile(filepath.Join(tempDir, "file1.txt"), []byte("file1 content"), os.ModePerm) + if err != nil { + t.Fatal(err) + } + + err = os.WriteFile(filepath.Join(tempDir, "dir1", "file2.txt"), []byte("file2 content"), os.ModePerm) + if err != nil { + t.Fatal(err) + } + + // Call the ListObjects function + items, err := ListObjects(filepath.Join(tempDir), "test-upload") + if err != nil { + t.Fatalf("ListObjects returned an error: %v", err) + } + + // Define expected results + expectedItems := []Item{ + {Key: "dir1/file2.txt", Size: int64(len("file2 content"))}, + {Key: "file1.txt", Size: int64(len("file1 content"))}, + } + + // Check the number of items + if len(items) != len(expectedItems) { + t.Fatalf("Expected %d items, got %d", len(expectedItems), len(items)) + } + + // Check each item + for i, item := range items { + if item.Key != expectedItems[i].Key || item.Size != expectedItems[i].Size { + t.Errorf("Expected item %v, got %v", expectedItems[i], item) + } + } +} + func TestIsTruncated(t *testing.T) { items1 := make([]Item, 1) items999 := make([]Item, 999) diff --git a/web/constant.go b/web/constant.go index bc400f6..27964bc 100644 --- a/web/constant.go +++ b/web/constant.go @@ -1,5 +1,6 @@ package web const ( - UPLOAD_DIR = "./upload" + UPLOAD_DIR_PATH = "./upload" + UPLOAD_DIR_NAME = "upload" ) diff --git a/web/controller.go b/web/controller.go index 86b3f16..2bc66bf 100644 --- a/web/controller.go +++ b/web/controller.go @@ -27,7 +27,7 @@ func CliSdkHandler(w http.ResponseWriter, r *http.Request) { if strings.Contains(userAgent, "aws-sdk") { if r.Method == "GET" { if r.URL.Query().Get("list-type") == "2" { - awssdk.ListObjectsV2(w, r) + awssdk.ListObjectsV2(w, r, UPLOAD_DIR_NAME) } else { awssdk.Get(w, r) } diff --git a/web/dir_controller.go b/web/dir_controller.go index 6c42db2..6b7ff75 100644 --- a/web/dir_controller.go +++ b/web/dir_controller.go @@ -15,7 +15,7 @@ func MkdirHandler(w http.ResponseWriter, r *http.Request) { currentPath := r.FormValue("currentPath") dirname := r.FormValue("dirname") - dir := filepath.Join(UPLOAD_DIR, currentPath, dirname) + dir := filepath.Join(UPLOAD_DIR_PATH, currentPath, dirname) err := os.Mkdir(dir, os.ModePerm) if err != nil { slog.Error("failed to mkdir", "target directory", dir, "error", err) @@ -34,7 +34,7 @@ func RmdirHandler(w http.ResponseWriter, r *http.Request) { } dirname := r.FormValue("dirname") - err := os.Remove(filepath.Join(UPLOAD_DIR, dirname)) + err := os.Remove(filepath.Join(UPLOAD_DIR_PATH, dirname)) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/web/rename_controller.go b/web/rename_controller.go index 63e48a5..8f1ea76 100644 --- a/web/rename_controller.go +++ b/web/rename_controller.go @@ -14,7 +14,7 @@ func RenameHandler(w http.ResponseWriter, r *http.Request) { oldFilename := r.FormValue("oldFilename") newFilename := r.FormValue("newFilename") - err := os.Rename(filepath.Join(UPLOAD_DIR, oldFilename), filepath.Join(UPLOAD_DIR, newFilename)) + err := os.Rename(filepath.Join(UPLOAD_DIR_PATH, oldFilename), filepath.Join(UPLOAD_DIR_PATH, newFilename)) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -31,7 +31,7 @@ func RenamedirHandler(w http.ResponseWriter, r *http.Request) { oldDirname := r.FormValue("oldDirname") newDirname := r.FormValue("newDirname") - err := os.Rename(filepath.Join(UPLOAD_DIR, oldDirname), filepath.Join(UPLOAD_DIR, newDirname)) + err := os.Rename(filepath.Join(UPLOAD_DIR_PATH, oldDirname), filepath.Join(UPLOAD_DIR_PATH, newDirname)) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/web/rm_controller.go b/web/rm_controller.go index efcc9a8..efeaf87 100644 --- a/web/rm_controller.go +++ b/web/rm_controller.go @@ -16,7 +16,7 @@ func RemoveHandler(w http.ResponseWriter, r *http.Request) { } path := r.FormValue("path")[len("/s3/"):] - err := os.Remove(filepath.Join(UPLOAD_DIR, path)) + err := os.Remove(filepath.Join(UPLOAD_DIR_PATH, path)) if err != nil { slog.Error("Failed to remove.", "error", err, "path", path) http.Error(w, err.Error(), http.StatusInternalServerError) diff --git a/web/s3_controller.go b/web/s3_controller.go index 4241034..fe59487 100644 --- a/web/s3_controller.go +++ b/web/s3_controller.go @@ -16,7 +16,7 @@ func S3Handler(w http.ResponseWriter, r *http.Request) { slog.Info("S3Handler is called.") path := r.URL.Path[len("/s3/"):] - s3Objects, err := util.GenerateS3Objects(r, UPLOAD_DIR, GetDirPath(path)) + s3Objects, err := util.GenerateS3Objects(r, UPLOAD_DIR_PATH, GetDirPath(path)) if err != nil { slog.Error("GenerateS3Objects error", "error", err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -59,7 +59,7 @@ func S3Handler(w http.ResponseWriter, r *http.Request) { func download(w http.ResponseWriter, path string) (n int64, httpStatus int, err error) { slog.Info("Start downloading file", "path", path) - file, err := os.Open(filepath.Join(UPLOAD_DIR, path)) + file, err := os.Open(filepath.Join(UPLOAD_DIR_PATH, path)) if err != nil { slog.Error("File open error", "error", err) return 0, http.StatusNotFound, err diff --git a/web/upload_controller.go b/web/upload_controller.go index e33a16a..93e58b0 100644 --- a/web/upload_controller.go +++ b/web/upload_controller.go @@ -37,7 +37,7 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) { } defer file.Close() - dst, err := os.Create(filepath.Join(UPLOAD_DIR, path, header.Filename)) + dst, err := os.Create(filepath.Join(UPLOAD_DIR_PATH, path, header.Filename)) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return