Skip to content

Commit 7f1da2a

Browse files
committed
Fs: added subPath parameter to getNewFs
This is needed since we may switch to different Fs in the middle of a path.
1 parent 24b474d commit 7f1da2a

File tree

7 files changed

+28
-23
lines changed

7 files changed

+28
-23
lines changed

src/components/dialogs/PrefsDialog.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ const PrefsDialog = observer(({ isOpen, onClose }: PrefsProps) => {
4141

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

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

5050
if (path !== settingsState.defaultFolder) {
5151
setIsFolderValid(isValid)

src/services/Fs.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export interface Fs {
4747
// runtime api
4848
API: new (path: string, onFsChange: (filename: string) => void) => FsApi
4949
// static members
50-
canread(str: string): boolean
50+
canread(basePath: string, subPath: string): boolean
5151
serverpart(str: string): string
5252
credentials(str: string): Credentials
5353
displaypath(str: string): { fullPath: string; shortPath: string }
@@ -144,8 +144,8 @@ export interface FsApi {
144144
loginOptions: Credentials
145145
}
146146

147-
export function getFS(path: string): Fs {
148-
const newfs = interfaces.find((filesystem) => filesystem.canread(path))
147+
export function getFS(basePath: string, subPath: string): Fs {
148+
const newfs = interfaces.find((filesystem) => filesystem.canread(basePath, subPath))
149149
// if (!newfs) {
150150
// newfs = FsGeneric;
151151
// `

src/services/plugins/FsLocal.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,8 @@ export const FsLocal: Fs = {
500500
needsRefresh: false,
501501
readonly: false,
502502
},
503-
canread(str: string): boolean {
504-
return !!str.match(localStart)
503+
canread(basePath: string): boolean {
504+
return !!basePath.match(localStart)
505505
},
506506
serverpart(str: string): string {
507507
return 'local'

src/services/plugins/FsVirtual.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,8 @@ export const FsVirtual: Fs = {
494494
needsRefresh: false,
495495
readonly: false,
496496
},
497-
canread(str: string): boolean {
498-
return !!str.match(virtualStart)
497+
canread(basePath: string): boolean {
498+
return !!basePath.match(virtualStart)
499499
},
500500
serverpart(str: string): string {
501501
return 'virtual'

src/services/plugins/FsWsl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ export const FsWsl: Fs = {
124124
needsRefresh: false,
125125
readonly: false,
126126
},
127-
canread(str: string): boolean {
128-
return isWin && !!str.match(wslStart)
127+
canread(basePath: string): boolean {
128+
return isWin && !!basePath.match(wslStart)
129129
},
130130
// eslint-disable-next-line @typescript-eslint/no-unused-vars
131131
serverpart(str: string): string {

src/services/plugins/FsZip.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ export class ZipApi implements FsApi {
129129
this.path = ''
130130
this.onFsChange = onFsChange
131131
this.zip = new Zip(path)
132-
debugger
133132
}
134133

135134
// local fs doesn't require login
@@ -601,8 +600,11 @@ export const FsZip: Fs = {
601600
needsRefresh: false,
602601
readonly: true,
603602
},
604-
canread(str: string): boolean {
605-
return str.replace(/\/$/, '').split(/\.zip/gi).length === 2
603+
canread(basePath: string, subPath: string): boolean {
604+
return (
605+
basePath.replace(/\/$/, '').split(/\.zip/gi).length === 2 &&
606+
(subPath !== '..' || !basePath.match(/\.zip$/i))
607+
)
606608
},
607609
serverpart(str: string): string {
608610
return 'zip'

src/state/fileState.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export class FileState {
218218

219219
this.viewId = viewId
220220
this.path = path
221-
this.getNewFS(path)
221+
this.getNewFS(path, '')
222222
}
223223

224224
private saveContext(): void {
@@ -250,10 +250,12 @@ export class FileState {
250250
this.reload()
251251
}
252252

253-
private getNewFS(path: string, skipContext = false): Fs {
254-
const newfs = getFS(path)
253+
private getNewFS(path: string, newDir: string, skipContext = false): Fs {
254+
const newfs = getFS(path, newDir)
255255

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

259261
// we need to free events in any case
@@ -526,13 +528,15 @@ export class FileState {
526528
}
527529

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

661665
openDirectory(file: { dir: string; fullname: string }): Promise<string | void> {
662-
console.log(file.dir, file.fullname)
663666
return this.cd(file.dir, file.fullname)
664667
}
665668

0 commit comments

Comments
 (0)