Skip to content

Commit

Permalink
Fs: added subPath parameter to getNewFs
Browse files Browse the repository at this point in the history
This is needed since we may switch to different Fs in the middle of a path.
  • Loading branch information
warpdesign committed Feb 23, 2023
1 parent 3969b99 commit 6952b2d
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/components/dialogs/PrefsDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ const PrefsDialog = observer(({ isOpen, onClose }: PrefsProps) => {

// TODO: we could have a default folder that's not using FsLocal
const [isFolderValid, setIsFolderValid] = useState(
() => FsLocal.canread(defaultFolder) && FolderExists(defaultFolder),
() => FsLocal.canread(defaultFolder, '') && FolderExists(defaultFolder),
)

const checkPath: (path: string) => void = debounce((path: string) => {
const isValid = FsLocal.canread(path) && FolderExists(path)
const isValid = FsLocal.canread(path, '') && FolderExists(path)

if (path !== settingsState.defaultFolder) {
setIsFolderValid(isValid)
Expand Down
6 changes: 3 additions & 3 deletions src/services/Fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface Fs {
// runtime api
API: new (path: string, onFsChange: (filename: string) => void) => FsApi
// static members
canread(str: string): boolean
canread(basePath: string, subPath: string): boolean
serverpart(str: string): string
credentials(str: string): Credentials
displaypath(str: string): { fullPath: string; shortPath: string }
Expand Down Expand Up @@ -144,8 +144,8 @@ export interface FsApi {
loginOptions: Credentials
}

export function getFS(path: string): Fs {
const newfs = interfaces.find((filesystem) => filesystem.canread(path))
export function getFS(basePath: string, subPath: string): Fs {
const newfs = interfaces.find((filesystem) => filesystem.canread(basePath, subPath))
// if (!newfs) {
// newfs = FsGeneric;
// `
Expand Down
4 changes: 2 additions & 2 deletions src/services/plugins/FsLocal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,8 @@ export const FsLocal: Fs = {
needsRefresh: false,
readonly: false,
},
canread(str: string): boolean {
return !!str.match(localStart)
canread(basePath: string): boolean {
return !!basePath.match(localStart)
},
serverpart(str: string): string {
return 'local'
Expand Down
4 changes: 2 additions & 2 deletions src/services/plugins/FsVirtual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,8 @@ export const FsVirtual: Fs = {
needsRefresh: false,
readonly: false,
},
canread(str: string): boolean {
return !!str.match(virtualStart)
canread(basePath: string): boolean {
return !!basePath.match(virtualStart)
},
serverpart(str: string): string {
return 'virtual'
Expand Down
4 changes: 2 additions & 2 deletions src/services/plugins/FsWsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ export const FsWsl: Fs = {
needsRefresh: false,
readonly: false,
},
canread(str: string): boolean {
return isWin && !!str.match(wslStart)
canread(basePath: string): boolean {
return isWin && !!basePath.match(wslStart)
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
serverpart(str: string): string {
Expand Down
8 changes: 5 additions & 3 deletions src/services/plugins/FsZip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ export class ZipApi implements FsApi {
this.path = ''
this.onFsChange = onFsChange
this.zip = new Zip(path)
debugger
}

// local fs doesn't require login
Expand Down Expand Up @@ -601,8 +600,11 @@ export const FsZip: Fs = {
needsRefresh: false,
readonly: true,
},
canread(str: string): boolean {
return str.replace(/\/$/, '').split(/\.zip/gi).length === 2
canread(basePath: string, subPath: string): boolean {
return (
basePath.replace(/\/$/, '').split(/\.zip/gi).length === 2 &&
(subPath !== '..' || !basePath.match(/\.zip$/i))
)
},
serverpart(str: string): string {
return 'zip'
Expand Down
21 changes: 12 additions & 9 deletions src/state/fileState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export class FileState {

this.viewId = viewId
this.path = path
this.getNewFS(path)
this.getNewFS(path, '')
}

private saveContext(): void {
Expand Down Expand Up @@ -250,10 +250,12 @@ export class FileState {
this.reload()
}

private getNewFS(path: string, skipContext = false): Fs {
const newfs = getFS(path)
private getNewFS(path: string, newDir: string, skipContext = false): Fs {
const newfs = getFS(path, newDir)

if (newfs) {
// only create a new FS if supported FS is different
// than current one
if (!this.fs || newfs.name !== this.fs.name) {
!skipContext && this.api && this.saveContext()

// we need to free events in any case
Expand Down Expand Up @@ -526,13 +528,15 @@ export class FileState {
}

cd(path: string, path2 = '', skipHistory = false, skipContext = false): Promise<string> {
// first updates fs (eg. was local fs, is now ftp)
if (this.path !== path) {
if (this.getNewFS(path, skipContext)) {
// Since we may change Fs in the middle of a path (for example:
// path == '/foo/archive/zip', path2 == '..', fs == FsZip)
// In this particular case, going up a directory should
// switch to FsLocal.
if (this.path !== path || path2 === '..') {
if (this.getNewFS(path, path2, skipContext)) {
this.server = this.fs.serverpart(path)
this.credentials = this.fs.credentials(path)
} else {
// this.navHistory(0);
return Promise.reject({
message: i18n.i18next.t('ERRORS.CANNOT_READ_FOLDER', { folder: path }),
code: 'NO_FS',
Expand Down Expand Up @@ -659,7 +663,6 @@ export class FileState {
}

openDirectory(file: { dir: string; fullname: string }): Promise<string | void> {
console.log(file.dir, file.fullname)
return this.cd(file.dir, file.fullname)
}

Expand Down

0 comments on commit 6952b2d

Please sign in to comment.