diff --git a/internal/handlers/check.go b/internal/handlers/check.go index f836450..54d2150 100644 --- a/internal/handlers/check.go +++ b/internal/handlers/check.go @@ -178,9 +178,13 @@ func CheckMyWork(w http.ResponseWriter, r *http.Request) { // make sure the file exists in the filesystem case "File Path", "Supplemental File": filename := strings.ReplaceAll(cell, `\`, `/`) - filename = strings.TrimLeft(filename, "/") - if len(filename) > 3 && filename[0:3] != "mnt" { - filename = fmt.Sprintf("/mnt/islandora_staging/%s", filename) + // we're testing with /tmp files + if len(filename) < 6 || filename[0:5] != "/tmp/" { + // but need to make sure we're mapping /mnt/islandora_staging into /data in docker + filename = strings.TrimLeft(filename, "/") + if len(filename) > 3 && filename[0:3] != "mnt" { + filename = fmt.Sprintf("/mnt/islandora_staging/%s", filename) + } } filename = strings.ReplaceAll(filename, "/mnt/islandora_staging", "/data") @@ -251,7 +255,13 @@ func fileExists(filename string) bool { if os.IsNotExist(err) { return false } - return !info.IsDir() + if !info.Mode().IsRegular() { + return false + } + + mode := info.Mode().Perm() + // Check if the file is globally readable + return mode&0004 != 0 } func authRequest(w http.ResponseWriter, r *http.Request) bool { diff --git a/internal/handlers/check_test.go b/internal/handlers/check_test.go index d46f93d..d31a191 100644 --- a/internal/handlers/check_test.go +++ b/internal/handlers/check_test.go @@ -10,6 +10,28 @@ import ( ) func TestCheckMyWork(t *testing.T) { + files := []struct { + name string + permissions os.FileMode + expectAccess bool + }{ + {"/tmp/test_readable.txt", 0644, true}, // Readable globally + {"/tmp/test_writable.txt", 0666, true}, // Writable globally + {"/tmp/test_private.txt", 0600, false}, // Not accessible globally + } + + // Create test files + for _, file := range files { + if err := os.WriteFile(file.name, []byte("test content"), file.permissions); err != nil { + t.Fatalf("Failed to create test file %s: %v", file.name, err) + } + } + defer func() { + for _, file := range files { + _ = os.Remove(file.name) + } + }() + tests := []struct { name string method string @@ -112,6 +134,37 @@ func TestCheckMyWork(t *testing.T) { statusCode: http.StatusOK, response: `{"D2":"Missing source file"}`, }, + { + name: "OK file", + method: http.MethodPost, + body: [][]string{ + {"Title", "Object Model", "Full Title", "File Path"}, + {"foo", "Image", "foo", "/tmp/test_readable.txt"}, + }, + statusCode: http.StatusOK, + response: `{}`, + }, + { + name: "OK file (rw)", + method: http.MethodPost, + body: [][]string{ + {"Title", "Object Model", "Full Title", "File Path"}, + {"foo", "Image", "foo", "/tmp/test_writable.txt"}, + }, + statusCode: http.StatusOK, + response: `{}`, + }, + + { + name: "Unreadable file", + method: http.MethodPost, + body: [][]string{ + {"Title", "Object Model", "Full Title", "File Path"}, + {"foo", "Image", "foo", "/tmp/test_private.txt"}, + }, + statusCode: http.StatusOK, + response: `{"D2":"File does not exist in islandora_staging"}`, + }, { name: "Missing file OK", method: http.MethodPost,