Skip to content

Commit

Permalink
add TestListObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
tttol committed Jul 16, 2024
1 parent 7fc7a80 commit 87cf719
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 15 deletions.
10 changes: 5 additions & 5 deletions core/amazon/awssdk/listobjects_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -60,15 +60,15 @@ 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 {
return err
}
if !info.IsDir() {
items = append(items, Item{
Key: filepath.ToSlash(path[len("upload/"):]),
Key: filepath.ToSlash(path[len(uploadDirName+"/"):]),
Size: info.Size(),
})
}
Expand Down
52 changes: 52 additions & 0 deletions core/amazon/awssdk/listobjects_v2_test.go
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
3 changes: 2 additions & 1 deletion web/constant.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package web

const (
UPLOAD_DIR = "./upload"
UPLOAD_DIR_PATH = "./upload"
UPLOAD_DIR_NAME = "upload"
)
2 changes: 1 addition & 1 deletion web/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions web/dir_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions web/rename_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion web/rm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions web/s3_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion web/upload_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 87cf719

Please sign in to comment.