Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default function DirectoryPicker({
initialPath,
}: Readonly<DirectoryPickerProperties>) {
const [currentPath, setCurrentPath] = useState('')
const [parentPath, setParentPath] = useState('')
const [entries, setEntries] = useState<FilesystemEntry[]>([])
const [selectedEntry, setSelectedEntry] = useState<string | null>(null)
const [loading, setLoading] = useState(false)
Expand All @@ -32,11 +33,11 @@ export default function DirectoryPicker({
setSelectedEntry(null)
try {
const result = await filesystemService.browse(path)
setEntries(result)
setCurrentPath(path)
setEntries(result.entries)
setCurrentPath(result.resolvedPath)
setParentPath(result.parentPath)
} catch (error_) {
const status = error_ instanceof ApiError ? error_.status : 0
if (status === 403) {
if (error_ instanceof ApiError && error_.status === 403) {
setError('Access denied')
} else {
setError(error_ instanceof Error ? error_.message : 'Failed to load directories')
Expand All @@ -46,6 +47,10 @@ export default function DirectoryPicker({
}
}, [])

const handleNavigateUp = () => {
loadEntries(parentPath)
}

useEffect(() => {
if (isOpen) {
setSelectedEntry(null)
Expand All @@ -55,23 +60,7 @@ export default function DirectoryPicker({

if (!isOpen) return null

const isRoot = !currentPath
const canGoUp = !isRoot

const handleNavigateUp = () => {
if (/^[a-zA-Z]:[/\\]?$/.test(currentPath) || currentPath === '/') {
loadEntries('')
return
}
const parentPath = currentPath.replace(/[\\/][^\\/]*$/, '')
if (!parentPath || parentPath === currentPath) {
loadEntries('')
} else if (/^[a-zA-Z]:$/.test(parentPath)) {
loadEntries(`${parentPath}\\`)
} else {
loadEntries(parentPath)
}
}
const canGoUp = parentPath !== ''

const handleClick = (entry: FilesystemEntry) => {
setSelectedEntry(entry.path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,15 @@ export default function CloneProjectModal({
const [showPicker, setShowPicker] = useState(false)

useEffect(() => {
if (isOpen && isLocal) {
if (initialPath) {
setLocation(initialPath)
} else {
filesystemService
.getDefaultPath()
.then(setLocation)
.catch(() => setLocation(''))
}
} else if (isOpen) {
setLocation(initialPath ?? '')
if (!isOpen || !isLocal) {
if (isOpen) setLocation(initialPath ?? '')
return
}

filesystemService
.resolveNearestAccessiblePath(initialPath ?? '')
.then(setLocation)
.catch(() => setLocation(''))
}, [isOpen, isLocal, initialPath])

if (!isOpen) return null
Expand Down
19 changes: 8 additions & 11 deletions src/main/frontend/app/routes/projectlanding/new-project-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,15 @@ export default function NewProjectModal({
const [showPicker, setShowPicker] = useState(false)

useEffect(() => {
if (isOpen && isLocal) {
if (initialPath) {
setLocation(initialPath)
} else {
filesystemService
.getDefaultPath()
.then(setLocation)
.catch(() => setLocation(''))
}
} else if (isOpen) {
setLocation(initialPath ?? '')
if (!isOpen || !isLocal) {
if (isOpen) setLocation(initialPath ?? '')
return
}

filesystemService
.resolveNearestAccessiblePath(initialPath ?? '')
.then(setLocation)
.catch(() => setLocation(''))
}, [isOpen, isLocal, initialPath])

if (!isOpen) return null
Expand Down
10 changes: 5 additions & 5 deletions src/main/frontend/app/services/filesystem-service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { apiFetch } from '~/utils/api'
import type { FilesystemEntry } from '~/types/filesystem.types'
import type { BrowseResult } from '~/types/filesystem.types'

export const filesystemService = {
async browse(path = ''): Promise<FilesystemEntry[]> {
async browse(path = ''): Promise<BrowseResult> {
return apiFetch(`/filesystem/browse?path=${encodeURIComponent(path)}`)
},

async getDefaultPath(): Promise<string> {
const result = await apiFetch<{ path: string }>('/filesystem/default-path')
return result.path
async resolveNearestAccessiblePath(path: string): Promise<string> {
const result = await this.browse(path)
return result.resolvedPath
},
}
6 changes: 6 additions & 0 deletions src/main/frontend/app/types/filesystem.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
export type EntryType = 'DIRECTORY' | 'FILE'

export interface BrowseResult {
resolvedPath: string
parentPath: string
entries: FilesystemEntry[]
}

export interface FilesystemEntry {
name: string
path: string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.frankframework.flow.filesystem;

import java.util.List;

public record BrowseResult(String resolvedPath, String parentPath, List<FilesystemEntry> entries) {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.frankframework.flow.filesystem;

import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.List;

Expand All @@ -17,6 +18,17 @@
*/
List<FilesystemEntry> listDirectory(String path) throws IOException;

/**
* Returns entries for the given path. If the path does not exist, walks up to
* the nearest accessible ancestor. Falls back to roots if none found.
*/
default BrowseResult browse(String path) throws IOException {
if (path == null || path.isBlank()) {
return new BrowseResult("", "", listRoots());
}
return browseNearestAccessible(path);
}

String readFile(String path) throws IOException;

String readFileType(String path) throws IOException;
Expand Down Expand Up @@ -53,4 +65,24 @@
default String toRelativePath(String absolutePath) {
return absolutePath;
}


private BrowseResult browseNearestAccessible(String path) throws IOException {
try {
return new BrowseResult(path, parentPath(path), listDirectory(path));
} catch (NoSuchFileException e) {

Check warning on line 73 in src/main/java/org/frankframework/flow/filesystem/FileSystemStorage.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace "e" with an unnamed pattern.

See more on https://sonarcloud.io/project/issues?id=frankframework_flow&issues=AZ39N_eAK2gr5jB46JUU&open=AZ39N_eAK2gr5jB46JUU&pullRequest=471
String parent = parentPath(path);
return parent.isEmpty() ? new BrowseResult("", "", listRoots()) : browseNearestAccessible(parent);
}
}

private static String parentPath(String path) {
String normalized = path.replace('/', '\\');
if (normalized.matches("[a-zA-Z]:[/\\\\]?")) return "";
int lastSep = normalized.lastIndexOf('\\');
if (lastSep < 0) return "";
String parent = normalized.substring(0, lastSep);
if (parent.matches("[a-zA-Z]:")) return parent + "\\";
return parent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.util.List;
import java.util.Map;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -22,21 +21,13 @@ public FilesystemController(FileSystemStorage fileSystemStorage) {
}

@GetMapping("/browse")
public ResponseEntity<List<FilesystemEntry>> browse(@RequestParam(required = false, defaultValue = "") String path)
public ResponseEntity<BrowseResult> browse(@RequestParam(required = false, defaultValue = "") String path)
throws IOException {

List<FilesystemEntry> entries;
if (path.isBlank()) {
entries = fileSystemStorage.listRoots();
} else {
try {
entries = fileSystemStorage.listDirectory(path);
} catch (AccessDeniedException e) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
try {
return ResponseEntity.ok(fileSystemStorage.browse(path));
} catch (AccessDeniedException e) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}

return ResponseEntity.ok(entries);
}

@GetMapping("/default-path")
Expand Down
Loading
Loading