Skip to content

Commit d26471b

Browse files
author
Francesco Pagnamenta
committed
getFileSystemByTargetPath function
1 parent 4498e1e commit d26471b

File tree

3 files changed

+50
-51
lines changed

3 files changed

+50
-51
lines changed

app/modules/status/helpers/system-helper.ts

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,54 @@ import _ from 'lodash'
99
// types
1010
import { FileSystemDataType, FileSystem, System } from '~/types/api-status'
1111

12+
type ResolvedFileSystem = {
13+
fileSystem: FileSystem
14+
path: string
15+
}
16+
17+
export const getFileSystemByTargetPath = (
18+
system: System,
19+
targetPath: string | null | undefined,
20+
username: string,
21+
): ResolvedFileSystem => {
22+
if (!system) {
23+
throw new Error('Filesystem(s) configuration error: system is missing')
24+
}
25+
26+
// If no target path, resolve from default
27+
if (targetPath == null || _.isEmpty(targetPath)) {
28+
const fileSystem = getDefaultFileSystemFromSystem(system)
29+
30+
if (!fileSystem) {
31+
throw new Error('Filesystem(s) configuration error: no default filesystem')
32+
}
33+
34+
let path = fileSystem.path
35+
36+
if (fileSystem.defaultWorkDir) {
37+
path = `${fileSystem.path}/${username}`
38+
}
39+
40+
return { fileSystem, path }
41+
}
42+
43+
// If target path is provided, search in the system
44+
const fileSystem = searchFileSystemByPath(system.fileSystems, targetPath, false)
45+
46+
if (!fileSystem) {
47+
throw new Error(`Filesystem(s) configuration error: no filesystem for path "${targetPath}"`)
48+
}
49+
50+
return { fileSystem, path: targetPath }
51+
}
52+
1253
export const searchSystemByName = (systems: System[], name: string, defaultValue: any = null) => {
1354
const system = systems.find((system: System) => {
1455
return system.name == name
1556
})
1657
return system || defaultValue
1758
}
1859

19-
2060
export const searchFileSystemByPath = (
2161
fileSystems: FileSystem[],
2262
path: string,
@@ -47,14 +87,14 @@ export const getDefaultFileSystemFromSystem = (system: System | null) => {
4787
if (system === null) {
4888
return null
4989
}
50-
if (system.fileSystems.length == 0){
90+
if (system.fileSystems.length == 0) {
5191
return null
5292
}
5393
const fileSystem = system.fileSystems.find((fileSystem: FileSystem) => {
5494
return fileSystem.defaultWorkDir == true
5595
})
56-
if (fileSystem === null){
57-
return system.fileSystems[0]
96+
if (fileSystem === null) {
97+
return system.fileSystems[0]
5898
}
5999
return fileSystem
60100
}

app/routes/_app.filesystems.systems.$systemName._index.tsx

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import logger from '~/logger/logger'
1313
// helpers
1414
import {
1515
searchSystemByName,
16-
searchFileSystemByPath,
17-
getDefaultFileSystemFromSystem,
16+
getFileSystemByTargetPath,
1817
} from '~/modules/status/helpers/system-helper'
1918
import { logInfoHttp } from '~/helpers/log-helper'
2019
import { getHealthyFileSystemSystems } from '~/helpers/system-helper'
@@ -50,8 +49,6 @@ export const loader: LoaderFunction = async ({ params, request }: LoaderFunction
5049
// Get url params
5150
const url = new URL(request.url)
5251
const targetPath = url.searchParams.get('targetPath')
53-
// Local variables
54-
let path = targetPath
5552
// Validate system name
5653
if (systemName === undefined || _.isEmpty(systemName)) {
5754
throw new Error('System not specified')
@@ -66,22 +63,7 @@ export const loader: LoaderFunction = async ({ params, request }: LoaderFunction
6663
// Get system
6764
const system = searchSystemByName(activeSystems, systemName)
6865
// Get file system & path
69-
let fileSystem = null
70-
if (targetPath === undefined || _.isEmpty(targetPath)) {
71-
fileSystem = getDefaultFileSystemFromSystem(system)
72-
if (fileSystem) {
73-
path = fileSystem.path
74-
if (fileSystem.defaultWorkDir) {
75-
path = `${fileSystem.path}/${auth.user.username}`
76-
}
77-
}
78-
} else {
79-
fileSystem = searchFileSystemByPath(system.fileSystems, path!, false)
80-
}
81-
// Validation
82-
if (system === null || fileSystem === null) {
83-
throw new Error('Filesystem(s) configuration error')
84-
}
66+
const { fileSystem, path } = getFileSystemByTargetPath(system, targetPath, auth.user.username)
8567
// Call api/s and fetch data
8668
let files: File[] = []
8769
let remoteFsError: any = null
@@ -96,13 +78,8 @@ export const loader: LoaderFunction = async ({ params, request }: LoaderFunction
9678
throw err
9779
}
9880
}
99-
let accountName = ''
100-
try {
101-
const userinfo = await getUserInfo(accessToken, system.name, request)
102-
accountName = userinfo.group.name
103-
} catch (err) {
104-
logger.error('Error determining account name from path', { error: err })
105-
}
81+
const userinfo = await getUserInfo(accessToken, system.name, request)
82+
const accountName = userinfo.group.name
10683
// Return response
10784
return {
10885
files,

app/routes/_app.filesystems.systems.$systemName.accounts.$accountName._index.tsx

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import logger from '~/logger/logger'
1313
// helpers
1414
import {
1515
searchSystemByName,
16-
searchFileSystemByPath,
17-
getDefaultFileSystemFromSystem,
16+
getFileSystemByTargetPath,
1817
} from '~/modules/status/helpers/system-helper'
1918
import { logInfoHttp } from '~/helpers/log-helper'
2019
import { getHealthyFileSystemSystems } from '~/helpers/system-helper'
@@ -51,8 +50,6 @@ export const loader: LoaderFunction = async ({ params, request }: LoaderFunction
5150
// Get url params
5251
const url = new URL(request.url)
5352
const targetPath = url.searchParams.get('targetPath')
54-
// Local variables
55-
let path = targetPath
5653
// Validate system name
5754
if (systemName === undefined || _.isEmpty(systemName)) {
5855
throw new Error('System not specified')
@@ -67,22 +64,7 @@ export const loader: LoaderFunction = async ({ params, request }: LoaderFunction
6764
// Get system
6865
const system = searchSystemByName(activeSystems, systemName)
6966
// Get file system & path
70-
let fileSystem = null
71-
if (targetPath === undefined || _.isEmpty(targetPath)) {
72-
fileSystem = getDefaultFileSystemFromSystem(system)
73-
if (fileSystem) {
74-
path = fileSystem.path
75-
if (fileSystem.defaultWorkDir) {
76-
path = `${fileSystem.path}/${auth.user.username}`
77-
}
78-
}
79-
} else {
80-
fileSystem = searchFileSystemByPath(system.fileSystems, path!, false)
81-
}
82-
// Validation
83-
if (system === null || fileSystem === null) {
84-
throw new Error('Filesystem(s) configuration error')
85-
}
67+
const { fileSystem, path } = getFileSystemByTargetPath(system, targetPath, auth.user.username)
8668
// Call api/s and fetch data
8769
let files: File[] = []
8870
let remoteFsError: any = null

0 commit comments

Comments
 (0)